boto.s3: copy() 复制键对象时丢失 'Content-Type' 元数据
这里有一段关于复制S3密钥的示例代码。你可能会有很多理由想要这样做,其中一个原因是更新密钥的元数据。虽然这似乎是大家普遍接受的解决方案,但其实有一个大问题。问题在于,当我执行下面的例子时,我的内容类型(Content-Type)会丢失,默认变成了'application/octet-stream'(这对于提供网页图片来说并不太有用)。
# 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 个回答
1
看看这个帖子
你需要做一个
key = bucket.get_key(key.name)
然后:
metadata['Content-Type'] = key.content_type will work
否则,key.content_type
会返回application/octet-stream
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
不过运行起来花了很长时间!