ListObjects操作在AWS上的限制

huangapple go评论97阅读模式
英文:

ListObjects operation's limit on AWS

问题

我正在阅读 AWS 的 Go SDK 中 ListObjects 函数的文档

(同样的情况也适用于实际的 API 端点)

文档中写道:

返回存储桶中的一些或全部对象(最多 1,000 个)。

这是什么意思?如果我的存储桶中有 200,000 个对象,这个 API 调用将不起作用吗?

这个示例使用了 ListObjectsPages(在底层调用了 ListObjects),并声称列出了__所有__对象。

实际情况是什么?

英文:

I am going through the documentation of ListObjects function in AWS' go SDK.

(the same holds more or less for the actual API endpoint)

So the docs write:

> Returns some or all (up to 1,000) of the objects in a bucket.

What does this mean? If my bucket has 200.000 objects this API call will not work?

This example uses ListObjectsPages (which calls ListObjects under the hood) and claims to list all objects.

What is the actual case here?

答案1

得分: 3

我正在阅读 AWS Go SDK 中 ListObjects 函数的文档。

请使用 ListObjectsV2。它的行为几乎相同,但是它是 ListObjects 的更新版本。AWS 更新 API 的情况并不常见,但当他们这样做时,通常是有充分理由的。他们非常注重向后兼容性,这就是为什么 ListObjects 仍然存在的原因。

这个示例使用了 ListObjectsPages(在底层调用 ListObjects),并声称列出了所有对象。

ListObjectsPagesListObjects 的分页版本,V2 版本也是如此,我将在下面进行描述。

许多 AWS API 的响应都是分页的。AWS 使用游标分页;这意味着请求响应中包含一个游标 - 在 ListObjectsV2 的情况下是 ContinuationToken。如果存在更多对象(响应中的 IsTruncated),则后续的 ListObjectsV2 请求内容可以提供 ContinuationToken,以便从第一个响应结束的地方继续列出。

ListObjectsV2Pages 会为您处理迭代的 ListObjectsV2 请求,因此您不必处理 ContinuationTokenIsTruncated 的逻辑。相反,您提供一个函数,该函数将在响应中的每个“页面”上调用。

因此,可以准确地说 ListObjectsV2Pages 将列出“所有”对象,但这是因为它在后台进行了多个 ListObjectsV2 调用,以列出多个响应页面。

因此,...Pages 函数可以被视为方便函数。在适当的情况下,您应该始终使用它们-它们消除了分页的痛苦,而分页对于处理潜在的高容量 API 响应至关重要。在 AWS 中,如果支持分页,请假设您需要它-在典型情况下,即使后续页面包含结果,第一页的结果也不能保证包含任何结果。

英文:

> I am going through the documentation of ListObjects function in AWS' go SDK.

Use ListObjectsV2. It behaves more or less the same, but it's an updated version of ListObjects. It's not super common for AWS to update APIs, and when they do, it's usually for a good reason. They're great about backwards compatibility which is why ListObjects still exists.

> This example uses ListObjectsPages (which calls ListObjects under the hood) and claims to list all objects.

ListObjectsPages is a paginated equivalent of ListObjects, and ditto for the V2 versions which I'll describe below.

Many AWS API responses are paginated. AWS uses Cursor Pagination; this means request responses include a cursor - ContinuationToken in the case of ListObjectsV2 . If more objects exist (IsTruncated in the response), a subsequent ListObjectsV2 request content can provide the ContinuationToken to continue the listing where the first response left off.

ListObjectsV2Pages handles the iterative ListObjectsV2 requests for you so you don't have to handle the logic of ContinuationToken and IsTruncated. Instead, you provide a function that will be invoked for each "page" in the response.

So it's accurate to say ListObjectsV2Pages will list "all" the objects, but it's because it makes multiple ListObjectsV2 calls in the backend that it will list more than one page of responses.

Thus, ...Pages functions can be considered convenience functions. You should always use them when appropriate - they take away the pain of pagination, and pagination is critical to make potentially high volume api responses operable. In AWS, if pagination is supported, assume you need it - in typical cases, the first page of results is not guaranteed to contain any results, even if subsequent pages do.

答案2

得分: 1

The AWS Go SDK V2提供了分页器类型,帮助我们管理S3每个查询的项目限制。ListObjectsV2Pages已经被移除。取而代之的是我们得到了ListObjectsV2Paginator,它处理了@Daniel_Farrell提到的分页细节。

构造函数接受与列出对象查询相同的参数(type ListObjectsV2Input)。分页器公开了两个方法:HasMorePages: boolNextPage: (*ListObjectsV2Output, error)

var items []Item

for p.HasMorePages() {
  batch, err := p.NextPage(ctx)
  // etc...
  item = append(items, newItems...)
}
英文:

The AWS Go SDK V2 gives us paginator types to help us manage S3's per-query item limits. ListObjectsV2Pages is gone. In its place we get ListObjectsV2Paginator, which deals with the pagination details that @Daniel_Farrell mentioned.

The constructor accepts the same params as the list objects query (type ListObjectsV2Input). The paginator exposes 2 methods: HasMorePages: bool and NextPage: (*ListObjectsV2Output, error).

var items []Item

for p.HasMorePages() {
  batch, err := p.NextPage(ctx)
  // etc...
  item = append(items, newItems...)
}

huangapple
  • 本文由 发表于 2021年12月17日 05:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/70385831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定