AWS SDK .NET 4.5 "从 AWS 返回的解组响应时出错。HTTP 状态代码:200 OK"在 ListObjectsV2 上



我在尝试列出bucket目录中的对象时遇到此错误。我不能从bucket的根目录中列出,因为它有1000多个对象,所以我需要深入目录列表以获得我想要的内容。当我从bucket的根显示时,我的代码是有效的,但当我试图在bucket的末尾添加目录以列出它们的内容时,我会收到这个错误"从AWS解组回响应时出错。HTTP状态码:200 OK"根元素丢失";在ListObjectsV2上。这是一个公共S3存储桶,所以我在下面包含了我的代码,以便其他人可以尝试。我使用AWS-SDK-NET45.zip,并在SSIS脚本任务中编译为Visual Basic 2019 for.NET 4.8。这应该行得通,对我做错了什么有什么想法吗?谢谢

---代码--


Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports Amazon.S3
Imports Amazon.S3.Model
Imports Amazon.Runtime
Imports Amazon
Imports Amazon.S3.Util
Imports System.Collections.ObjectModel
Imports System.IO
'ScriptMain is the entry point class of the script.  Do not change the name, attributes,
'or parent of this class.
<Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute()>
<System.CLSCompliantAttribute(False)>
Partial Public Class ScriptMain
Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase

Public Sub Main()
'
' Add your code here
'

Dim filecol As ObservableCollection(Of String)
Try
'filecol = ListingFiles("/gov-fpac-rma-pubfs-production/pub/References/actuarial_data_master/2023/")
'filecol = ListingFiles("/gov-fpac-rma-pubfs-production/") 'Bucket root
filecol = ListingFiles("/gov-fpac-rma-pubfs-production/pub/")
Dts.TaskResult = ScriptResults.Success
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
Dts.TaskResult = ScriptResults.Failure
End Try


End Sub
#Region "ScriptResults declaration"
'This enum provides a convenient shorthand within the scope of this class for setting the
'result of the script.
'This code was generated automatically.
Enum ScriptResults
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
End Enum
#End Region
Private Function ListingFiles(bucketName As String, Optional foldername As String = "/") As ObservableCollection(Of String)
Dim obsv As New ObservableCollection(Of String)
Dim delimiter As String = "/"
Dim AWS_ACCESS_KEY As String = "xxxxxxxxxxxx" 'Add your Access Key here
Dim AWS_SECRET_KEY As String = "xxxxxxxxxxxxxxxxx" ' 'Add your Secret here
Dim s3config As AmazonS3Config = New AmazonS3Config
With s3config
.ForcePathStyle = True
.RegionEndpoint = RegionEndpoint.USEast1

End With
Dim s3Client As AmazonS3Client = New AmazonS3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY, s3config)

If Not foldername.EndsWith(delimiter) Then
foldername = String.Format("{0}{1}", foldername, delimiter)
End If
Try
Try
Dim request As New ListObjectsV2Request() 
With request
.BucketName = bucketName
End With

Do
Dim response As New ListObjectsV2Response()
response = s3Client.ListObjectsV2(request)
For i As Integer = 1 To response.S3Objects.Count - 1
Dim entry As S3Object = response.S3Objects(i)
If Not foldername = "/" Then
If entry.Key.ToString.StartsWith(foldername) Then
Dim replacementstring As String = Replace(entry.Key, foldername, "")
If Not replacementstring = "" Then
obsv.Add(replacementstring)
End If
End If
Else
obsv.Add(Replace(entry.Key, foldername, ""))
End If
MessageBox.Show(entry.Key + "  " + entry.LastModified.ToString())
'Console.WriteLine("Object - " + entry.Key.ToString())
'Console.WriteLine(" Size - " + entry.Size.ToString())
'Console.WriteLine(" LastModified - " + entry.LastModified.ToString())
'Console.WriteLine(" Storage class - " + entry.StorageClass)
Next
If (response.IsTruncated) Then
request.ContinuationToken = response.NextContinuationToken
Else
request = Nothing
End If
Loop Until IsNothing(request)
Catch ex As AmazonS3Exception
Console.WriteLine(ex.Message.ToString)
Dts.TaskResult = ScriptResults.Failure
End Try
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
Dts.TaskResult = ScriptResults.Failure
End Try
Return obsv
End Function
End Class

好的,我按如下所示将前缀选项添加到ListObjectsV2Request中,它成功了。我可以从我想要的目录中获取文件列表。我认为它的工作原理类似于GetObjects函数,在这里你必须将目录添加到bucketname的末尾,并在Entry.Key中列出你想要的文件。希望其他人能找到这方面的帮助,因为我没有找到太多关于这方面的例子。

Dim request As New ListObjectsV2Request() 'With {.BucketName = bucketName}
With request
.BucketName = "/gov-fpac-rma-pubfs-production"
.Prefix = "pub/References/actuarial_data_master/2023/2023_"
End With

最新更新