Created
December 4, 2017 02:49
-
-
Save nmolivo/519ca4503484a9771549cb7b978b083b to your computer and use it in GitHub Desktop.
Storing images in an AWS S3 bucket from their URLs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mapping_dict ={} | |
for i, img_url in enumerate(image_list[0:10000]): | |
img_name = "img_%05d" % (i,) | |
mapping_dict[img_name] = img_url | |
if (img_url == np.nan) | (str(img_url) == "nan"): | |
continue | |
else: | |
# Uses the creds in ~/.aws/credentials | |
s3_image_filename = img_name | |
internet_image_url = img_url | |
# Given an Internet-accessible URL, download the image and upload it to S3, | |
# without needing to persist the image to disk locally | |
req_for_image = requests.get(internet_image_url, stream=True) | |
file_object_from_req = req_for_image.raw | |
req_data = file_object_from_req.read() | |
# Do the actual upload to s3 | |
s3.Bucket(bucket_name_to_upload_image_to).put_object(Key=s3_image_filename, Body=req_data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment