1
在启用了版本的存储桶中使用python/boto与AWS S3接口 - 如何将密钥还原为历史版本?Python/Boto + AWS S3版本控制:我如何用旧版本替换当前密钥?
我可以下载文件的历史版本并重新上传,但效率不高,会导致重复密钥。有没有办法选择我想要的版本并将其设置为活动/当前版本(例如,在版本未提供时会自动下载)?
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('my_test_bucket')
key = Key(bucket)
key.key = 'my_file.pdf'
key.set_contents_from_filename('1.pdf')
key.set_contents_from_filename('2.pdf')
key.set_contents_from_filename('3.pdf')
# Now I have a file with the key `my_file.pdf`
# and I can see the versions using something like:
versions = list(bucket.list_versions('my_file.pdf'))
print [k.version_id for k in versions]
# To make on older version active, I can download & re-upload
versions[1].get_contents_to_filename('old.pdf')
key.set_contents_from_filename('old.pdf')
# But this seems very inefficient, and creates duplicate version keys. It would be
# Nice to do something like:
versions[1].last_modified = datetime.now()
# Or even this, but this seems less efficient:
versions[1].copy('my_test_bucket', 'my_file.pdf') # And then delete
bucket.delete_key('my_file.pdf', version_id=versions[1])
谢谢 - 这很好! – 2014-11-25 20:56:18