6

I have had an S3 bucket for awhile but only now turned versioning on. Experimenting with it a bit trying to figure out what sort of detection protections I am getting with just the versioning on, without activating the "MFA delete" option.

I uploaded a test file, then deleted it then re-uploaded it twice. Now, using the S3 browser tool I am seeing 4 versions of the file: #1, #2 (deleted), #3 and #4 (current). If I use boto to get the latest version, I can extract its version_id:

import boto
c=boto.connect_s3()
b=c.get_bucket('my-bucket')
k = b.get_key('test2/dw.txt')
print k.version_id

But how do i get a full list of version_id's for a given key? And if I want to retrieve version #1 of the key (deleted), do I need to do something first using the version #2 id to "undelete" it?

Finally, does this deletion protection (creation of a delete marker) work with legacy files that had been uploaded before versioning was turned on?

Thx

4

3 回答 3

6

list_versions您可以使用存储桶对象的方法获取所有可用版本的列表。

import boto
c = boto.connect_s3()
bucket = c.get_bucket('my-bucket')
for version in bucket.list_versions():
    print(version)

这将返回具有特定关联的Key对象列表。version_ids您可以检索任何版本,但使用Key对象上的常规方法。如果要将旧版本设为当前版本,则必须重新上传或复制到服务器上。

对存储桶启用版本控制后,该时间点之后对存储桶中任何对象的所有删除操作都将导致将删除标记写入存储桶,而不是实际删除对象。

于 2015-01-30T19:49:24.857 回答
2

You can get list of all version using following method

session = boto3.Session(aws_access_key_id, aws_secret_access_key)

s3 = session.client('s3')

bucket_name = 'bucketname'

versions = s3.list_object_versions (Bucket = bucket_name, Prefix = 'Key')

print(versions.get('Versions'))

This will print a list of all versions present in that bucket along with other information like key, storage class, size etc

于 2018-05-05T10:39:47.200 回答
2

我没有看到也撤消删除标记的答案,所以这是我用来专门取消删除一个对象的脚本,如果您使用 AWS S3,您可能会忽略 ENDPOINT。

  • 此版本使用分页助手,以防对象的版本多于一个响应(默认为 1000)。
  • 我使用返回的 VersionId 创建一个 s3.ObjectVersion,然后使用 delete() 来恢复对象。
import boto3
import sys

ENDPOINT='10.62.64.200'

if len(sys.argv) != 3:
    print("Usage: {} bucketname key".format(sys.argv[0]))
    sys.exit(1)

bucketname = sys.argv[1]
key = sys.argv[2]

s3 = boto3.resource('s3', endpoint_url='http://' + ENDPOINT)
kwargs = {'Bucket' : bucketname, 'Prefix' : key}

pageresponse = s3.meta.client.get_paginator('list_object_versions').paginate(**kwargs)

for pageobject in pageresponse:
    if 'DeleteMarkers' in pageobject.keys() and pageobject['DeleteMarkers'][0]['Key'] == key:
        print("Undeleting s3://{}/{}".format(bucketname, key))
        s3.ObjectVersion(bucketname, key, pageobject['DeleteMarkers'][0]['VersionId']).delete()
于 2021-08-12T14:54:36.303 回答