Python - 在上传时制作 S3 图像 public?

Python - Make S3 images public as they are uploaded?

我正在使用此代码将图像从 python 上传到 S3,但我需要它们立即 public 而不是手动更改权限。

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())

我已经尝试过此代码:

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

使用 ACL

尝试:

k.set_acl('public-read')

根据 the boto documentation,这是在单个项目上设置固定 ACL 的方法。

更改存储桶上的 acl(就像您对 b.set_acl 所做的那样,只会影响存储桶操作的权限(例如列出存储桶中的对象),而不影响对象操作,例如读取对象 (这就是您想要的)。请参阅 Amazon S3 ACL Overview - Permissions 了解更多关于应用于存储桶和对象时不同权限的含义的详细信息。

使用存储桶策略

但是,根据图像的密钥结构,您可能会发现设置存储桶策略以授予对给定路径中所有密钥的读取访问权限更简单。这样您就不必对任何单个对象执行任何特殊操作。

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

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