vb.net - 列出超过 1000 个对象键的问题 - AWS-S3 - VB .net
问题描述
出于某种原因,即使在使用延续令牌分配实现循环以继续列出之后,下面的代码也不会列出超过 1000 个对象,这告诉 ListObjectsV2Request 仅在 IsTruncated 为真时才继续和停止。我的存储桶有 33,000 个对象,即使前缀和键是空字符串,结果总是返回 1000 个对象。谁能帮我理解为什么我总是得到 1000 个对象?标记,适用于旧版本的 Amazon SDK。
''' <summary>
''' List objects in a AWS-S3 Bucket depending on the Prefix and Key passes to it.
''' See: https://docs.aws.amazon.com/AmazonS3/latest/userguide/ListingKeysUsingAPIs.html
''' </summary>
''' <param name="myBucketName">The name of the AWS-S3 bucket.</param>
''' <param name="FilePrefix">Act as a organizer. For example `TestFiles\` can be added in front of many files to group them</param>
''' <param name="Key_or_ActualFilename">The actual name of the file with extension.
''' 'For example `ListOfCustomer1.xls` in TestFiles\ListOfCustomer1.xls
''' </param>
''' <remarks>
''' Notes from Amazon documentation:
''' Iterating through multi-page results:
''' As buckets can contain a virtually unlimited number of keys, the complete results of a list query can be extremely large.
''' To manage large result sets, the Amazon S3 API supports pagination to split them into multiple responses.
''' Each list keys response returns a page of up to 1,000 keys with an indicator indicating if the response is truncated.
''' You send a series of list keys requests until you have received all the keys. AWS SDK wrapper libraries provide the same pagination.
''' Function returns List(Of S3Object). Last modified by FGV on 03/20/2022 09:15PM
''' </remarks>
Async Function ListAFileOrFilesInAWS_S3(myBucketName As String, FilePrefix As String, Key_or_ActualFilename As String) As Task(Of List(Of S3Object))
Try
Dim myClient As S3.IAmazonS3 = Get_AWS_S3_Client()
'
If myClient IsNot Nothing Then
Dim SearchKey As String = ""
If String.IsNullOrEmpty(FilePrefix) And String.IsNullOrEmpty(Key_or_ActualFilename) Then
SearchKey = "" ' Search everywhere in the Bucket
ElseIf String.IsNullOrEmpty(FilePrefix) = False And String.IsNullOrEmpty(Key_or_ActualFilename) Then
SearchKey = FilePrefix ' Search for the file key only. more likely will return a file or two with the same unique filename or key part
ElseIf String.IsNullOrEmpty(FilePrefix) And String.IsNullOrEmpty(Key_or_ActualFilename) = False Then
SearchKey = Key_or_ActualFilename ' Search for the prefix only , more likely will return multiple files with the same prefix
ElseIf String.IsNullOrEmpty(FilePrefix) = False And String.IsNullOrEmpty(Key_or_ActualFilename) = False Then
SearchKey = FilePrefix & "\" & Key_or_ActualFilename
End If
'
Dim mylistOfObjectsRequest As ListObjectsV2Request = New ListObjectsV2Request With {
.BucketName = myBucketName,
.Prefix = SearchKey
}
'
' for some reason it does not list more than 1000 objects,
' even after implementing the loop below and the continuation token assignment!
Dim myListOfObjectsResponse As ListObjectsV2Response
Do
myListOfObjectsResponse = Await myClient.ListObjectsV2Async(mylistOfObjectsRequest)
'
'For Each entry As S3Object In myListOfObjectsResponse.S3Objects
' Console.WriteLine("key = {0} size = {1}", entry.Key, entry.Size)
'Next
'Console.WriteLine("Next Continuation Token: {0}", myListOfObjectsResponse.NextContinuationToken)
''
mylistOfObjectsRequest.ContinuationToken = myListOfObjectsResponse.NextContinuationToken
Loop While (myListOfObjectsResponse.IsTruncated)
'
' code below for troubleshooting purpose only
'For Each fileObject As S3Object In myListOfObjectsResponse.S3Objects
' If fileObject.Size > 0 Then ' if file size = 0 then is a folder must probably!
' MsgBox("Key Or File Full Path: " & fileObject.Key & " - Size: " & fileObject.Size.ToString _
' & " Bytes - Last Modified: " & fileObject.LastModified.ToString)
' End If
'Next
'
' using Linq to return the object everywhere in the bucket, but exclude the "folders" (object with size 0) themselves.
Return myListOfObjectsResponse.S3Objects.Where(Function(file) file.Size > 0).ToList()
'
End If
Catch ex As Exception
add_A_LogEntry(ErrorLogFilePath, "Unknown error listing files (object) via AWS-S3 client. Error: " & ex.Message)
End Try
Return Nothing
End Function
解决方案
推荐阅读
- flutter - 错误:找不到正确的提供者
高于此消费者 颤振中的小部件 - node.js - Botbuilder Proactive cancelAllDialogs() 与 Azure Functions 中的节点
- react-admin - 为创建和编辑视图添加自定义成功和错误消息
- node.js - 从 build 文件夹部署到 heroku 后无法从 src 文件夹导入图像
- python - 包含 lambda 表达式的函数背后的机制是什么?(Python)
- selenium - Selenium 没有浏览所有推文
- android - 更新子 Firebase 后刷新 ListView
- swift - 如何使以编程方式添加的 NSTextView 处于活动状态?
- azure - 发布天蓝色功能时找不到名称错误的应用程序
- c# - 在动态计算属性中使用选择未映射的实体框架错误