简体   繁体   English

Python - 将S3图像上传后公开?

[英]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. 我正在使用此代码将图像从python上传到S3,但我需要立即将它们公开,而不是手动更改权限。

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

Using ACLs 使用ACL

Try: 尝试:

k.set_acl('public-read')

According to the boto documentation this is how you set a canned ACL on an individual item. 根据boto文档,这是您在单个项目上设置固定ACL的方式。

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. 更改存储桶上的acl(与b.set_acl ,只会影响存储桶操作的权限(例如存储桶中的列表对象),而不会影响对象操作,例如读取对象(这是您想要的)。有关应用于存储桶和对象时不同权限的含义的详细信息,请参阅Amazon S3 ACL概述 - 权限

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 . 此博客文章讨论了IAM策略,存储桶策略和ACL之间的差异和组合

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM