{"action":"created","issue":{"url":"https://api.github.com/repos/amigocloud/amigocloud_samples/issues/4","repository_url":"https://api.github.com/repos/amigocloud/amigocloud_samples","labels_url":"https://api.github.com/repos/amigocloud/amigocloud_samples/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/amigocloud/amigocloud_samples/issues/4/comments","events_url":"https://api.github.com/repos/amigocloud/amigocloud_samples/issues/4/events","html_url":"https://github.com/amigocloud/amigocloud_samples/issues/4","id":361418628,"node_id":"MDU6SXNzdWUzNjE0MTg2Mjg=","number":4,"title":"How to read in an JPG that isn't in a zip file for uploading?","user":{"login":"3vivekb","id":4524205,"node_id":"MDQ6VXNlcjQ1MjQyMDU=","avatar_url":"https://avatars0.githubusercontent.com/u/4524205?v=4","gravatar_id":"","url":"https://api.github.com/users/3vivekb","html_url":"https://github.com/3vivekb","followers_url":"https://api.github.com/users/3vivekb/followers","following_url":"https://api.github.com/users/3vivekb/following{/other_user}","gists_url":"https://api.github.com/users/3vivekb/gists{/gist_id}","starred_url":"https://api.github.com/users/3vivekb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/3vivekb/subscriptions","organizations_url":"https://api.github.com/users/3vivekb/orgs","repos_url":"https://api.github.com/users/3vivekb/repos","events_url":"https://api.github.com/users/3vivekb/events{/privacy}","received_events_url":"https://api.github.com/users/3vivekb/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-09-18T18:02:37Z","updated_at":"2018-09-18T19:51:50Z","closed_at":null,"author_association":"NONE","body":"How do I read in an image file that is a jpg into the right format that is compatible with uploading?\r\nI don't want to pull it from a zipfile.\r\n\r\n```\r\nimport glob\r\nallFiles = glob.glob(\"./All Photos/*.JPG\")\r\nfor file_ in allFiles:\r\n    print(file_)\r\n    time = os.path.getmtime(file_)\r\n    timestamp = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%dT%H:%M:%SZ')\r\n    print(timestamp)\r\n    img_name = file_.split(' ')[-1]\r\n    meter = img_name.split('.')[0]\r\n    img = BytesIO(Image.open(file_)) #THIS IS WRONG\r\n    print(meter)\r\n    \r\n    try:\r\n        target_amigo_id = get_amigo_id_for_a_water_meter(meter)\r\n#         gallery_url_of_target_dataset = 'https://www.amigocloud.com/api/v1/related_tables/8404' #PROD\r\n        gallery_url_of_target_dataset = 'https://www.amigocloud.com/api/v1/related_tables/8405' #COPY\r\n        ac.upload_file(gallery_url_of_target_dataset + '/upload',\r\n                       gallery_url_of_target_dataset + '/chunked_upload', file_obj=img,\r\n                       extra_data={'source_amigo_id': target_amigo_id,\r\n                           'filename': img_name, 'datetime':timestamp})\r\n```"},"comment":{"url":"https://api.github.com/repos/amigocloud/amigocloud_samples/issues/comments/422527666","html_url":"https://github.com/amigocloud/amigocloud_samples/issues/4#issuecomment-422527666","issue_url":"https://api.github.com/repos/amigocloud/amigocloud_samples/issues/4","id":422527666,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMjUyNzY2Ng==","user":{"login":"DanielCaldwell","id":2634673,"node_id":"MDQ6VXNlcjI2MzQ2NzM=","avatar_url":"https://avatars0.githubusercontent.com/u/2634673?v=4","gravatar_id":"","url":"https://api.github.com/users/DanielCaldwell","html_url":"https://github.com/DanielCaldwell","followers_url":"https://api.github.com/users/DanielCaldwell/followers","following_url":"https://api.github.com/users/DanielCaldwell/following{/other_user}","gists_url":"https://api.github.com/users/DanielCaldwell/gists{/gist_id}","starred_url":"https://api.github.com/users/DanielCaldwell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DanielCaldwell/subscriptions","organizations_url":"https://api.github.com/users/DanielCaldwell/orgs","repos_url":"https://api.github.com/users/DanielCaldwell/repos","events_url":"https://api.github.com/users/DanielCaldwell/events{/privacy}","received_events_url":"https://api.github.com/users/DanielCaldwell/received_events","type":"User","site_admin":false},"created_at":"2018-09-18T19:51:50Z","updated_at":"2018-09-18T19:51:50Z","author_association":"MEMBER","body":"Hi Vivek, \r\n\r\nIt seems that you can just pass in a file path rather than the file stream. The API will open the file and upload it appropriately.  \r\n\r\nCode comment about the file_obj: \r\nhttps://github.com/amigocloud/python-amigocloud/blob/master/amigocloud/amigocloud.py#L226\r\n\r\nWhere it will open the file if it is just a string type (a path): \r\nhttps://github.com/amigocloud/python-amigocloud/blob/master/amigocloud/amigocloud.py#L236\r\n\r\n\r\n\r\nHere is some sample code I wrote that worked with the latest python api: \r\n\r\n```\r\nimport amigocloud\r\nimport glob\r\nimport os\r\nfrom datetime import datetime\r\n\r\nUSER_ID = <PROJECT_OWNER_ID>\r\nPROJECT_ID = <PROJECT_ID>\r\nPRIMARY_DATASET_ID = <PRIMARY_DATASET_ID>\r\nGALLERY_RELATED_TABLE_ID = <RELATED_TABLE_ID>\r\n\r\n# connect to AmigoCloud\r\nac = amigocloud.AmigoCloud(token='<YOUR TOKEN>')\r\n\r\nstatename_to_amigo_id = dict()\r\n\r\ndef get_amigo_id_for_a_statename(statename):\r\n\r\n    lower_statename = statename.lower()\r\n    if lower_statename in statename_to_amigo_id.keys():\r\n        return statename_to_amigo_id[lower_statename]\r\n    else:\r\n\r\n        query = \"SELECT amigo_id from dataset_{} where lower(name) = '{}'\".format(\r\n            PRIMARY_DATASET_ID, lower_statename\r\n        )\r\n\r\n        print(\"Querying for State ({}) : {} \".format(lower_statename, query))\r\n\r\n        sql_url = '/users/{user_id}/projects/{project_id}/sql'.format(\r\n            user_id=USER_ID, project_id=PROJECT_ID\r\n        )\r\n\r\n        query_amigo_id_response = ac.get(sql_url, {'query': query, 'offset': 0,\r\n                                    'limit': 1,\r\n                                    'dataset_id': PRIMARY_DATASET_ID})\r\n\r\n        if query_amigo_id_response[\"count\"] != 1:\r\n            raise RuntimeError(\"Invalid number of records returned\")\r\n        else:\r\n            amigo_id = query_amigo_id_response[\"data\"][0][\"amigo_id\"]\r\n            statename_to_amigo_id[lower_statename] = amigo_id\r\n            return amigo_id\r\n\r\n\r\n# find the images\r\njpg_files = glob.glob(\"./Photos/*.jpg\")\r\ncap_jpg_files = glob.glob(\"./Photos/*.JPG\")\r\n\r\nallFiles = jpg_files + cap_jpg_files\r\n\r\nfor file_ in allFiles:\r\n    print(file_)\r\n\r\n    filename = os.path.basename(file_)\r\n    current_statename = filename.split(' - ')[0]\r\n\r\n    print(current_statename)\r\n\r\n    current_amigo_id = get_amigo_id_for_a_statename(current_statename)\r\n    print (current_amigo_id)\r\n\r\n    # get time from image\r\n    time = os.path.getmtime(file_)\r\n    timestamp = datetime.utcfromtimestamp(time).strftime('%Y-%m-%dT%H:%M:%SZ')\r\n    print(timestamp)\r\n\r\n    target_amigo_id = current_amigo_id\r\n    gallery_url_of_target_dataset = 'https://www.amigocloud.com/api/v1/related_tables/{}'.format(\r\n        GALLERY_RELATED_TABLE_ID\r\n    )\r\n    ac.upload_file(gallery_url_of_target_dataset + '/upload',\r\n                   gallery_url_of_target_dataset + '/chunked_upload',\r\n                   file_obj=file_,\r\n                   extra_data={'source_amigo_id': target_amigo_id, 'filename': filename, 'datetime': timestamp})\r\n```\r\n\r\nTo run this I uploaded this dataset to my project: \r\n\r\n[ne_10m_us_states.zip](https://github.com/amigocloud/amigocloud_samples/files/2394283/ne_10m_us_states.zip)\r\n\r\nThen I added a gallery to that dataset. For the photos that I uploaded, here is a test set: \r\n\r\n[Photos.zip](https://github.com/amigocloud/amigocloud_samples/files/2394286/Photos.zip)\r\n\r\nThe photos are named : <statename> - <placename>.jpg \r\nsome have a capitalized extension so I added an extra couple of lines to grab them all. \r\n\r\nDoes this help?  \r\n"}}
{"actor":{"display_login":"DanielCaldwell"}}