【发布时间】:2014-05-22 14:04:45
【问题描述】:
我正在使用 Python 库 boto 连接到 Amazon S3 并为静态网站创建存储桶和密钥。我的键和值是动态生成的,因此我以编程方式而不是通过 Web 界面(它使用 Web 界面工作)来执行此操作。我的代码目前如下所示:
import boto
from boto.s3.connection import S3Connection
from boto.s3.key import Key
conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.create_bucket(BUCKET_NAME)
bucket.configure_website('index.html', 'error.html')
bucket.set_acl('public-read')
for template in ['index.html', 'contact-us.html', 'cart.html', 'checkout.html']:
k = Key(bucket)
k.key = key
k.set_acl('public-read')
k.set_metadata('Content-Type', 'text/html')
k.set_contents_from_string(get_page_contents(template))
我在这段代码中遇到了各种错误和问题。当密钥已经存在并且我使用此代码更新它们时,我会将每个密钥的 ACL 设置为 public-read,但在浏览器中查看文件时仍然会收到 403 禁止错误。
我尝试删除所有键以从头开始重新创建它们,现在我得到了 NoSuchKey 异常。显然钥匙不存在,因为我正在尝试创建它。
我是不是走错了路?有没有不同的方法来创建密钥而不是更新它们?当权限不存在时,我是否遇到了某种竞争情况?
【问题讨论】:
-
你在 python shell 中试过这些例子boto.s3.amazonaws.com/s3_tut.html 吗??
-
我不确定,但是当您使用已经存在的密钥时应该使用
bucket.get_key吗?
标签: python django amazon-s3 acl boto