ConfusedLuigi
ConfusedLuigi

Reputation: 87

Python - Make S3 images public as they are uploaded?

I'm using this code to upload images to S3 from python but I need them to be public right away instead of changing permissions manually.

im2.save(out_im2, 'PNG')
conn = boto.connect_s3(keys)

b = conn.get_bucket('example')
k = b.new_key('example.png')
k.set_contents_from_string(out_im2.getvalue())

I already tried this code:

b.set_acl('public-read-write')

Upvotes: 1

Views: 294

Answers (1)

Chris Simon
Chris Simon

Reputation: 6505

Using ACLs

Try:

k.set_acl('public-read')

According to the boto documentation this is how you set a canned ACL on an individual item.

Change the acl on the bucket (as you were doing with b.set_acl, will only affecting permissions on bucket operations (such as List Objects in a Bucket), not object operations, such as reading an object (which is what you want). See Amazon S3 ACL Overview - Permissions for more details on what the different permissions mean when applied to buckets and objects.

Using Bucket Policies

However, depending on the key structure for your images, you may find it simpler to setup a bucket policy that grants read access to all keys in a given path. Then you don't have to do anything special with any individual objects.

Here is a sample policy that grants read-only access.

And this blog post discusses the differences and combinations between IAM Policies, Bucket Policies and ACLs.

Upvotes: 3

Related Questions