获取AWS S3存储桶中对象的所有版本? [英] Get all versions of an object in an AWS S3 bucket?

查看:532
本文介绍了获取AWS S3存储桶中对象的所有版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在存储桶上启用了对象版本控制.我想在该存储桶中获取密钥的所有版本.但是我找不到方法去做.如何使用S3 API做到这一点?

I've enabled object versioning on a bucket. I want to get all versions of a key inside that bucket. But I cannot find a method go do this; how would one accomplish this using the S3 APIs?

推荐答案

所以,今天早上我碰到了这堵砖墙.事实证明,这看似微不足道的事情很难做到.

So, I ran into this brick wall this morning. This seemingly trivial thing is incredibly difficult to do, it turns out.

您想要的API是获取存储桶对象版本 API,但遗憾的是使用起来并不平凡.

The API you want is the GET Bucket Object versions API, but it is sadly non-trivial to use.

首先,您必须避开一些非解决方案:KeyMarker,由boto3记录为

First, you have to steer clear of some non-solutions: KeyMarker, which is documented by boto3 as,

KeyMarker (字符串)-指定列出存储桶中的对象时要开始的键.

KeyMarker (string) -- Specifies the key to start with when listing objects in a bucket.

…列出存储桶中的对象时,不是以指定的键开头;相反,它会在该键之后 立即开始,这使得它在这里有点用处.

…does not start with the specified key when listing objects in a bucket; rather, it starts immediately after that key, which makes it somewhat useless here.

此API提供的最佳限制是Prefix;这并不是一个完美的选择,因为可能有一些钥匙不是我们感兴趣的钥匙,但仍然包含我们的钥匙.

The best restriction this API provides is Prefix; this isn't going to be perfect, since there could be keys that are not our key of interest that nonetheless contain our key.

还要提防MaxKeys;从字典上讲,我们很容易想到我们的密钥应该是第一个,并且所有以我们的密钥作为其密钥名称前缀的密钥都将紧随其后,因此我们可以使用MaxKeys对其进行修饰.可悲的是,MaxKeys并不控制响应中返回多少键,而是控制版本数. (而且我想这是事先不知道的.)

Also beware of MaxKeys; it is tempting to think that, lexicographically, our key should be first, and all keys which have our key as a prefix of their key name would follow, so we could trim them using MaxKeys; sadly, MaxKeys controls not how many keys are returned in the response, but rather the number of versions. (And I'm going to presume that isn't known in advance.)

因此,Prefix似乎是最好的.另外请注意,至少在某些语言中,客户端库不会为您处理分页,因此您还需要处理它.

So, Prefix is the best it seems that can be done. Also note that, at least in some languages, the client library will not handle pagination for you, so you'll additionally need to deal with that.

boto3为例:

response = client.list_object_versions(
    Bucket=bucket_name, Prefix=key_name,
)
while True:
    # Process `response`
    ...
    # Check if the results got paginated:
    if response['IsTruncated']:
        response = client.list_object_versions(
            Bucket=bucket_name, Prefix=key_name,
            KeyMarker=response['NextKeyMarker'],
            VersionIdMarker=response['NextVersionIdMarker'],
        )
    else:
       break

这篇关于获取AWS S3存储桶中对象的所有版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆