S3: List Objects with ListObjectsV2
See more S3 - HTTP Examples
Uses Chilkat.Http with Amazon S3 general purpose buckets and Signature Version 4. These examples do not cover directory buckets/S3 Express, access-point ARNs, Outposts, or other specialized endpoints. They target the commercial AWS partition; other partitions need their documented endpoint suffix.
Load private qa_data/aws/s3.json containing nonempty string fields access_key and secret_key, plus session_token for temporary credentials. Prefer short-lived role credentials. These samples do not discover AWS profiles or refresh credentials. Set the bucket's actual region and replace sample bucket names and object keys. TLS certificate verification is explicitly enabled. Keep detailed HTTP diagnostics private because they can contain credentials or presigned URLs.
Uses S3_ListBucketObjects with list-type=2 to request ListObjectsV2. Follows opaque NextContinuationToken values while IsTruncated is true, including empty pages; detects missing/repeated tokens and stops at 10,000 pages with incomplete-results status. Requires s3:ListBucket.
Requests encoding-type=url so keys containing XML-incompatible characters can be represented. Decode returned keys exactly once before reusing them. Query values are encoded separately. Sizes remain decimal strings to preserve large values; LastModified is an ISO 8601 UTC timestamp, not RFC 822. Owner.DisplayName is not used. Listing pages are not a single snapshot if objects change during enumeration.
Chilkat B4X Downloads
Dim success As Boolean = False
Dim stringCompare As ChilkatStringBuilder
stringCompare.Initialize
' This example requires the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
' Load private credentials; never put access keys or session tokens in source.
Dim credentials As ChilkatJsonObject
credentials.Initialize
credentials.Strict = True
success = credentials.LoadFile("qa_data/aws/s3.json")
If success <> True Then
Log("Could not load private S3 credentials.")
Return
End If
stringCompare.SetString(credentials.StringOf("access_key"))
Dim stringMatch1 As Boolean = stringCompare.ContentsEqual("", True)
If (credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = True) Then
Log("Missing or invalid access_key.")
Return
End If
stringCompare.SetString(credentials.StringOf("secret_key"))
Dim stringMatch2 As Boolean = stringCompare.ContentsEqual("", True)
If (credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = True) Then
Log("Missing or invalid secret_key.")
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
http.AwsAccessKey = credentials.StringOf("access_key")
http.AwsSecretKey = credentials.StringOf("secret_key")
If credentials.HasMember("session_token") = True Then
stringCompare.SetString(credentials.StringOf("session_token"))
Dim stringMatch3 As Boolean = stringCompare.ContentsEqual("", True)
If (credentials.JsonTypeOf("session_token") <> 1) Or (stringMatch3 = True) Then
Log("session_token must be a nonempty string when supplied.")
Return
End If
http.AwsSessionToken = credentials.StringOf("session_token")
End If
' Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
' Set the actual bucket region, not the caller's location.
Dim region As String = "us-west-2"
http.AwsRegion = region
Dim endpoint As ChilkatStringBuilder
endpoint.Initialize
endpoint.Append("s3.")
endpoint.Append(region)
endpoint.Append(".amazonaws.com")
http.AwsEndpoint = endpoint.GetAsString
http.RequireSslCertVerify = True
http.FollowRedirects = False
' A region redirect is not success. Correct the endpoint and signing region.
Dim bucketName As String = "your-bucket-name"
Dim bucketPath As ChilkatStringBuilder
bucketPath.Initialize
Dim encoded As ChilkatStringBuilder
encoded.Initialize
Dim xml As ChilkatXml
xml.Initialize
Dim seenTokens As ChilkatHashtable
seenTokens.Initialize
Dim token As String = ""
Dim more As Boolean = True
Dim page As Int = 0
Do While (more = True)
If page = 10000 Then
Log("Page limit reached; listing is incomplete.")
Return
End If
page = page + 1
bucketPath.SetString(bucketName)
bucketPath.Append("?list-type=2&max-keys=1000&encoding-type=url")
stringCompare.SetString(token)
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("", True)
If stringMatch4 <> True Then
encoded.SetString(token)
bucketPath.Append("&continuation-token=")
bucketPath.Append(encoded.GetEncoded("url", "utf-8"))
End If
Dim xmlStr As String = http.S3_ListBucketObjects(bucketPath.GetAsString)
If http.LastMethodSuccess <> True Then
Log("Request failed (transport or S3 error). HTTP status: " & http.LastStatus)
' Inspect LastErrorText privately. Do not log credentials or signed URLs.
Return
End If
If http.LastStatus <> 200 Then
Log("Unexpected HTTP status: " & http.LastStatus)
Return
End If
success = xml.LoadXml(xmlStr)
If success <> True Then
Log("Invalid XML response.")
Return
End If
stringCompare.SetString(xml.Tag)
Dim stringMatch5 As Boolean = stringCompare.ContentsEqual("ListBucketResult", True)
If stringMatch5 <> True Then
Log("Unexpected XML response root.")
Return
End If
stringCompare.SetString(xml.GetChildContent("EncodingType"))
Dim stringMatch6 As Boolean = stringCompare.ContentsEqual("url", True)
If stringMatch6 <> True Then
Log("Expected URL-encoded listing keys.")
Return
End If
Dim i As Int = 0
Dim count As Int = xml.NumChildrenHavingTag("Contents")
Do While (i < count)
xml.I = i
' Decode keys exactly once. Sizes remain decimal strings (not int).
Dim itemKey As String = http.UrlDecode(xml.GetChildContent("Contents[i]|Key"))
stringCompare.SetString(itemKey)
Dim stringMatch7 As Boolean = stringCompare.ContentsEqual("", True)
If (http.LastMethodSuccess <> True) Or (stringMatch7 = True) Then
Log("Invalid object key.")
Return
End If
Dim size As String = xml.GetChildContent("Contents[i]|Size")
stringCompare.SetString(size)
Dim stringMatch8 As Boolean = stringCompare.ContentsEqual("", True)
If (xml.LastMethodSuccess <> True) Or (stringMatch8 = True) Then
Log("Incomplete object entry.")
Return
End If
Dim modified As String = xml.GetChildContent("Contents[i]|LastModified")
stringCompare.SetString(modified)
Dim stringMatch9 As Boolean = stringCompare.ContentsEqual("", True)
If (xml.LastMethodSuccess <> True) Or (stringMatch9 = True) Then
Log("Incomplete object entry.")
Return
End If
Log("Key: " & itemKey)
Log("Bytes: " & size)
' S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
Log("Last modified (UTC): " & modified)
i = i + 1
Loop
Dim truncated As String = xml.GetChildContent("IsTruncated")
stringCompare.SetString(truncated)
Dim stringMatch10 As Boolean = stringCompare.ContentsEqual("true", True)
stringCompare.SetString(truncated)
Dim stringMatch11 As Boolean = stringCompare.ContentsEqual("false", True)
If (stringMatch10 <> True) And (stringMatch11 <> True) Then
Log("Missing or invalid IsTruncated; listing is incomplete.")
Return
End If
more = False
stringCompare.SetString(truncated)
Dim stringMatch12 As Boolean = stringCompare.ContentsEqual("true", True)
If stringMatch12 = True Then
token = xml.GetChildContent("NextContinuationToken")
stringCompare.SetString(token)
Dim stringMatch13 As Boolean = stringCompare.ContentsEqual("", True)
If (xml.LastMethodSuccess <> True) Or (stringMatch13 = True) Then
Log("Missing continuation token; listing is incomplete.")
Return
End If
If seenTokens.Contains(token) = True Then
Log("Repeated continuation token; listing is incomplete.")
Return
End If
success = seenTokens.AddStr(token, "seen")
If success <> True Then
Log("Could not track pagination.")
Return
End If
more = True
End If
Loop
Log("Object listing complete.")