如何使用Python(和boto)在Amazon S3中克隆一个键?
我在我的S3存储桶里有一个文件,它是通过一个键来存储的。我想创建一个新的键,里面也包含同样的文件。请问有没有办法不下载这个文件就能做到这一点?我希望能用Python来解决这个问题,最好是用boto库。
6 个回答
4
在查看boto的源代码时,我发现Key对象有一个“copy”方法。感谢你提到的关于CopyObject操作的建议。
10
from boto.s3.key import Key
#Get source key from bucket by name
source_key = source_bucket.get_key(source_key_name)
#Copy source key to a new bucket with a new key name (can be the same as source)
#Note: source_key is Key
source_key.copy(dest_bucket_name,dest_key_name)
#The signature of boto's Key class:
def copy(self, dst_bucket, dst_key, metadata=None,
reduced_redundancy=False, preserve_acl=False,
encrypt_key=False, validate_dst_bucket=True)
#set preserve_acl=True to copy the acl from the source key
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。
16
这里的“bucket”指的是目标存储桶,也就是你要把东西放进去的地方:
bucket.copy_key(new_key,source_bucket,source_key)