AWS SDK .NET 4.5 "Error unmarshalling response back from AWS. HTTP Status Code: 200 OK" on ListObjectsV2 #2103
-
I do not know if this is a bug or not, but any help would be appreciated. I am getting this error trying to list objects in a directory on a bucket. I cannot list from the root of the bucket as it has more than 1000 objects, so I need to drill farther down into the directory list to get what I want. My code works when I display from the root of the bucket, but when I try to add directories at the end of the bucket to list their contents I get this error. "Error unmarshalling response back from AWS. HTTP Status Code: 200 OK", "Root element is missing" on the ListObjectsV2. This is a public S3 bucket so I have included my code below so others can try it. I am using AWS-SDK-NET45.zip, AWSSDK.Core.dll v3.7.12.11 and compiling as Visual Basic 2019 for .NET 4.8 within an SSIS Script task. This should work, any ideas on what I am doing wrong? Thanks.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @cstro2021, Good afternoon. Looks like you are using the API incorrectly. If you refer service API documentation for S3: ListObjectsV2, the there is a prefix parameter in addition to Bucket parameter. Bucket parameter should be the name of the bucket, not the path down to the specific key (there is no concept of folder in S3, it is represented as a key). So you need to use the Imports System.Collections.ObjectModel
Imports System.Windows.Forms
Imports Amazon
Imports Amazon.S3
Imports Amazon.S3.Model
Module Module1
Sub Main()
Dim filecol As ObservableCollection(Of String)
Try
filecol = ListingFiles("gov-fpac-rma-pubfs-production", "pub/")
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
End Try
End Sub
Private Function ListingFiles(bucketName As String, Optional prefix As String = "", Optional foldername As String = "/") As ObservableCollection(Of String)
Dim obsv As New ObservableCollection(Of String)
Dim delimiter As String = "/"
Dim s3config As AmazonS3Config = New AmazonS3Config
With s3config
.ForcePathStyle = True
End With
Dim s3Client As AmazonS3Client = New AmazonS3Client(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
If Not String.IsNullOrWhiteSpace(prefix) Then
request.Prefix = prefix
End If
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)
End Try
Catch ex As Exception
Console.WriteLine(ex.Message.ToString)
End Try
Return obsv
End Function
End Module Thanks, |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Hi @cstro2021,
Good afternoon.
Looks like you are using the API incorrectly. If you refer service API documentation for S3: ListObjectsV2, the there is a prefix parameter in addition to Bucket parameter. Bucket parameter should be the name of the bucket, not the path down to the specific key (there is no concept of folder in S3, it is represented as a key). So you need to use the
Prefix
property if you want to narrow down results to specific folders. In your case, below is the minimal example: