如何从Amazon S3下载100,000多个版本的文件的所有版本?

时间:2015-09-08 15:13:50

标签: amazon-web-services amazon-s3 versioning versions

我在Windows上使用AWS命令行,到目前为止我找到的所有方法似乎都建议我需要获取所有对象的版本ID列表。是否有某种像我可以使用的通配符?

2 个答案:

答案 0 :(得分:1)

使用boto的此Python代码将下载存储桶中找到的所有文件版本。大量版本可能需要通过结果集进行分页。

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)

答案 1 :(得分:1)

使用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'])