如何从存储桶中获取一个对象?

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

how to get only one object from a bucket?

问题

我需要一次只获取一个S3存储桶中的对象。我只找到了获取存储桶中所有对象的API,有没有办法只获取一个对象?我将使用特定的位置或索引来一次获取一个对象。

result, err := w.Client.ListObjectsV2(ctx, input)
if err != nil {
    fmt.Println("获取对象时出错:")
    fmt.Println(err)
}
for _, item := range result.Contents {
    fmt.Println("名称:", *item.Key)
    fmt.Println("最后修改时间:", *item.LastModified)
    fmt.Println("大小:", item.Size)
    fmt.Println("存储类别:", item.StorageClass)
    fmt.Println("")
}
英文:

I need to get only one object at a time from S3 bucket..
And I only found the API to get all the objects in a bucket.. is there a way to get only one?
I'm gonna use a certain position or index to get one object at a time.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    result, err := w.Client.ListObjectsV2(ctx, input)
	if err != nil {
		fmt.Println(&quot;Got an error retrieving objects:&quot;)
		fmt.Println(err)
	}
	for _, item := range result.Contents {
		fmt.Println(&quot;Name:          &quot;, *item.Key)
		fmt.Println(&quot;Last modified: &quot;, *item.LastModified)
		fmt.Println(&quot;Size:          &quot;, item.Size)
		fmt.Println(&quot;Storage class: &quot;, item.StorageClass)
		fmt.Println(&quot;&quot;)
	}

<!-- end snippet -->

答案1

得分: 2

你可以在ListObjectsV2Input中指定MaxKeys=1来请求返回的S3键的最大数量:

> MaxKeys:设置响应中返回的键的最大数量。默认情况下,该操作最多返回1,000个键名。响应可能包含较少的键,但绝不会超过该数量。

要获取下一个S3键,您可以在后续的ListObjectsV2请求中使用StartAfterContinuationToken

英文:

You can request a maximum number of S3 keys to be returned by indicating MaxKeys=1 in your ListObjectsV2Input:

> MaxKeys: Sets the maximum number of keys returned in the response. By default the action returns up to 1,000 key names. The response might contain fewer keys but will never contain more.

To get the next S3 key, you would use either StartAfter or ContinuationToken in your subsequent ListObjectsV2 request.

huangapple
  • 本文由 发表于 2021年6月7日 18:54:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/67870313.html
匿名

发表评论

匿名网友

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

确定