S3: List Buckets with the Http Convenience Method
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.
Calls S3_ListBuckets and parses bucket names and creation timestamps without relying on Owner.DisplayName. Requires s3:ListAllMyBuckets.
Unpaginated helper: AWS recommends paginated ListBuckets requests. Unpaginated requests are supported only for accounts with the default general purpose bucket quota of 10,000; accounts with an approved higher quota must use pagination even if they currently own fewer buckets. This method exposes no pagination parameters, and AwsSubResources does not add them to its request in the tested runtime. Do not use this convenience example as a scalable inventory. A dedicated paginated ListBuckets example is recommended.
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.
' This convenience method is UNPAGINATED. See the limitations below.
Dim xmlStr As String = http.S3_ListBuckets
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
Dim xml As ChilkatXml
xml.Initialize
success = xml.LoadXml(xmlStr)
If success <> True Then
Log("Invalid XML response.")
Return
End If
stringCompare.SetString(xml.Tag)
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("ListAllMyBucketsResult", True)
If stringMatch4 <> True Then
Log("Unexpected XML response root.")
Return
End If
Dim i As Int = 0
Dim count As Int = xml.NumChildrenAt("Buckets")
Do While (i < count)
xml.I = i
Log("Bucket: " & xml.GetChildContent("Buckets|Bucket[i]|Name"))
Log("Created (UTC): " & xml.GetChildContent("Buckets|Bucket[i]|CreationDate"))
' Owner.DisplayName is not required or used.
i = i + 1
Loop
If xml.HasChildWithTag("ContinuationToken") = True Then
Log("More buckets exist. This unpaginated helper has not listed everything.")
Return
End If
Log("Bucket listing complete.")