boto.s3:key对象上的copy()丢失了'Content-Type'元数据

时间:2012-02-03 22:40:29

标签: python amazon-s3 content-type boto

以下是复制S3密钥的示例代码。您可能需要执行此操作的原因很多,其中一个原因是更新关键元数据。虽然这似乎是广泛接受的解决方案,但存在一个大问题。问题是,当我执行下面的示例时,实际上我丢失了我的Content-Type,它默认返回'application / octet-stream'(如果尝试提供Web图像则不是很有用)。

# Get bucket
conn = S3Connection(self._aws_key, self._aws_secret)
bucket = conn.get_bucket(self._aws_bucket)

# Create key
k = Key(bucket)
k.key = key

# Copy old key
k.metadata.update({ meta_key: meta_value })
k2 = k.copy(k.bucket.name, k.name, k.metadata, preserve_acl=True)
k = k2

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:5)

以下GitHub Gist为我工作:

import boto

s3 = boto.connect_s3()
bucket = s3.lookup('mybucket')
key = bucket.lookup('mykey')

# Copy the key onto itself, preserving the ACL but changing the content-type
key.copy(key.bucket, key.name, preserve_acl=True, metadata={'Content-Type': 'text/plain'})

key = bucket.lookup('mykey')
print key.content_type

花了很多时间跑完了!

答案 1 :(得分:1)

查看this帖子

你需要做一个

key = bucket.get_key(key.name)

然后:

metadata['Content-Type'] = key.content_type will work

否则,key.content_type将返回application/octet-stream