【发布时间】:2016-03-31 22:10:32
【问题描述】:
您好,我正在使用 boto 将文件写入 S3 存储桶。我最近删除了一个 EC2 实例并使用新的密钥对启动了一个新实例。我现在可以从存储桶中读取文件,但是当我尝试写入文件时,它会失败且没有错误。我可以看到存储桶中的文件。这是代码:
conn = S3Connection()
bucket = conn.get_bucket('my-bucket')
keylist = bucket.list()
k = Key(bucket)
def upload_file():
# Get the name of the uploaded files
uploaded_files = request.files.getlist("file[]")
current_date = time.strftime("%x").split("/")[0]+time.strftime("%x").split("/")[1]+time.strftime("%x").split("/")[2]
filenames = []
for file in uploaded_files:
# Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
# Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
file_contents = file.read()
k.key = current_date + "/" + file.filename
print "uploading files now "
k.set_contents_from_string(file_contents)
我不确定这是否是因为新的密钥对。不过,它确实提供了读取访问权限。但是写入不会发生。是因为新实例吗?仅供参考,它在 localhost 中完美运行。
【问题讨论】:
-
“它在 localhost 中完美运行”是什么意思?密钥对用于登录 EC2 实例,它们不用于 API 调用。而是检查分配给实例的角色,该角色用于将 IAM 凭证分配给在实例上运行的软件。
-
您是否从
k.set_contents_from_string()调用中获得了返回值?您可能想尝试为每个单独的文件获取一个新密钥 (k = Key(bucket)),而不是重新使用现有值。