gpt4 book ai didi

Python/Boto + AWS S3 版本控制 : how do I replace the current key with older version?

转载 作者:行者123 更新时间:2023-11-30 23:17:56 24 4
gpt4 key购买 nike

在启用了版本的存储桶上使用 python/boto 与 AWS S3 交互 - 如何将 key 恢复到历史版本?

我可以下载该文件的历史版本并重新上传,但这效率低下并且会导致重复 key 。有没有办法选择我想要的版本并将其设置为事件/当前版本(以便在未提供版本时自动下载)?

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])

最佳答案

您无法将某个版本“移动”为“当前”版本,但可以使用 copy_key() 将先前版本复制到同一 key ,从而使其成为“当前”版本.

> versions = list(bucket.list_versions('my_file.pdf'))
> print [k.version_id for k in versions]
[u'tsbfD0oKQ87a.cWyImABs5lVhVv84dnC', u'0nKieCek8C4q4GegjXKh8UhJCoiAMrqM', u'null']
> key.name
'my_file.pdf'
> bucket.copy_key(key.name, 'bucket-name', key.name, src_version_id=u'0nKieCek8C4q4GegjXKh8UhJCoiAMrqM')

这会产生一个新版本,它是早期版本的副本。所有复制均在“S3 内”完成,无需下载/上传内容。

是的,它仍然创建另一个版本。如果不希望出现这种情况,您可以删除以前的版本:

bucket.delete_key(key.name,version_id=u'0nKieCek8C4q4GegjXKh8UhJCoiAMrqM')

关于Python/Boto + AWS S3 版本控制 : how do I replace the current key with older version?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27067963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com