Reputation: 6744
I am trying to use the Python requests package to upload an image file to my Amazon AWS S3 bucket.
The code I have opens the bucket, downloads an image file, resizes the image, saves the image locally, then tries to upload the saved image to the S3 bucket.
It all works fine except that the uploaded jpg file is corrupt in some way in as much as it can no longer be viewed as an image. I have checked that the original file that is being uploaded is not corrupt.
My code is:
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(settings.AWS_STORAGE_BUCKET_NAME)
for key in bucket.list(prefix='media/userphotos'):
file_name=key.name
full_path_filename = 'https://' + settings.AWS_STORAGE_BUCKET_NAME + '.s3.amazonaws.com/' + file_name
fd_img = urlopen(full_path_filename);
img = Image.open(fd_img)
img = resizeimage.resize_width(img, 800)
new_filename = full_path_filename.replace('userphotos', 'webversion')
# Save temporarily before uploading to S3 bucket
img.save('temp.jpg', img.format)
the_file = {'media': open('temp.jpg', 'rb')}
r = requests.put(new_filename, files=the_file, auth=S3Auth(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY))
fd_img.close()
UPDATE
I have just noticed that while the jpg file cannot be opened with a web browser or with Preview on my Mac it can be opened successfully with Adobe Photoshop! Clearly the image is in the file but there is something about the jpg file created by requests.put()
which is doing something to the file that stops it being readable by web browsers. Strange!
Upvotes: 3
Views: 1292
Reputation: 41
Do this instead:
requests.put(url, data=open(filename, 'rb'))
I noticed using "files" as documented in requests library prepends a bunch of garbage to the file. You inspect that with xxd <filename>
Upvotes: 4