Add metadata to upload image to S3 using Python

I successfully add an image to the bucket on S3, but the problem is that 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)) 

It is currently loading as an โ€œapplication / octet streamโ€.

+6
source share
2 answers

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

 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.

+14
source

I found that setting metadata in front of you set_contents_from_file works for me. I use GCS, although it may be different ..

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

Source: https://habr.com/ru/post/908090/


All Articles