如何从 Amazon S3 下载具有 100,000 多个版本的文件的所有版本?
How do I download all the versions of a file with 100,000+ versions from Amazon S3?
我在 Windows 上使用 AWS 命令行,到目前为止我发现的所有方法似乎都表明我需要获取所有对象的版本 ID 列表。我可以使用类似 * 的某种通配符吗?
此 Python 代码使用 boto
将下载存储桶中找到的所有文件版本。大量版本可能需要对结果集进行分页。
import boto
conn = boto.connect_s3()
bucket = conn.get_bucket('BUCKET')
# Get a list of all versions contained in the bucket
versions = bucket.list_versions(prefix='FILENAME')
for v in versions:
# Save the version to a filename based on the Last Modified date
v.get_contents_to_filename(v.last_modified)
使用 Boto3 John 的解决方案需要更新如下。我正在使用修改后的 ts 保存文件。
import boto3
client = boto3.client('s3')
_bucket = '<s3Bucket>'
_file = '<fileName>'
_key = '<the s3 prefix>' + _file
_local = '<local path>' + _file
response = client.list_object_versions(
Bucket=_bucket,
Prefix=_key
)
for v in response['Versions']:
client.download_file(_bucket, _key,
_local + '_' + v['LastModified'].strftime('%Y%m%d%H%M%S'),
ExtraArgs={"VersionId": v["VersionId"]})
print(v['LastModified'])
我在 Windows 上使用 AWS 命令行,到目前为止我发现的所有方法似乎都表明我需要获取所有对象的版本 ID 列表。我可以使用类似 * 的某种通配符吗?
此 Python 代码使用 boto
将下载存储桶中找到的所有文件版本。大量版本可能需要对结果集进行分页。
import boto
conn = boto.connect_s3()
bucket = conn.get_bucket('BUCKET')
# Get a list of all versions contained in the bucket
versions = bucket.list_versions(prefix='FILENAME')
for v in versions:
# Save the version to a filename based on the Last Modified date
v.get_contents_to_filename(v.last_modified)
使用 Boto3 John 的解决方案需要更新如下。我正在使用修改后的 ts 保存文件。
import boto3
client = boto3.client('s3')
_bucket = '<s3Bucket>'
_file = '<fileName>'
_key = '<the s3 prefix>' + _file
_local = '<local path>' + _file
response = client.list_object_versions(
Bucket=_bucket,
Prefix=_key
)
for v in response['Versions']:
client.download_file(_bucket, _key,
_local + '_' + v['LastModified'].strftime('%Y%m%d%H%M%S'),
ExtraArgs={"VersionId": v["VersionId"]})
print(v['LastModified'])