captDaylight
captDaylight

Reputation: 2234

Add metadata to image upload to S3 with Python

I'm successfully adding an image to a bucket on S3, but the problem is I'm not sure how to set the content-type to 'image/png'. Here is my code

image = Image.open(self.image)
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)     
out_im2 = cStringIO.StringIO()
image.save(out_im2, 'PNG')
b = conn.get_bucket('new_test_bucket')
k = b.new_key(self.title+'.png')
k.set_contents_from_filename(str(self.image))

Currently it is being uploaded as 'application/octet-stream'.

Upvotes: 7

Views: 4071

Answers (2)

tkone
tkone

Reputation: 22728

This is how my image upload code (using boto) works:

k.set_metadata('Content-Type', mime)
k.set_contents_from_file(data, policy='public-read')
k.set_acl('public-read')

Well at least part of it.

Upvotes: 14

Matt
Matt

Reputation: 4989

I found that setting the metadata before you set_contents_from_file works for me. I am using GCS though, so maybe that's different..

bucket = conn.get_bucket('plots')
k = bucket.new_key('000006')
k.set_metadata('Content-Type', 'image/png')
k.set_contents_from_stream(buff)

Upvotes: 0

Related Questions