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 Visual FoxPro Downloads
LOCAL lnSuccess
LOCAL loStringCompare
LOCAL loCredentials
LOCAL lnStringMatch1
LOCAL lnStringMatch2
LOCAL loHttp
LOCAL lnStringMatch3
LOCAL lcRegion
LOCAL loEndpoint
LOCAL lcXmlStr
LOCAL loXml
LOCAL lnStringMatch4
LOCAL i
LOCAL lnCount
lnSuccess = 0
loStringCompare = CreateObject('Chilkat.StringBuilder')
* 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.
loCredentials = CreateObject('Chilkat.JsonObject')
loCredentials.Strict = 1
lnSuccess = loCredentials.LoadFile("qa_data/aws/s3.json")
IF (lnSuccess <> 1) THEN
? "Could not load private S3 credentials."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("access_key"))
lnStringMatch1 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("access_key") <> 1) OR (lnStringMatch1 = 1)) THEN
? "Missing or invalid access_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loStringCompare.SetString(loCredentials.StringOf("secret_key"))
lnStringMatch2 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("secret_key") <> 1) OR (lnStringMatch2 = 1)) THEN
? "Missing or invalid secret_key."
RELEASE loStringCompare
RELEASE loCredentials
CANCEL
ENDIF
loHttp = CreateObject('Chilkat.Http')
loHttp.AwsAccessKey = loCredentials.StringOf("access_key")
loHttp.AwsSecretKey = loCredentials.StringOf("secret_key")
IF (loCredentials.HasMember("session_token") = 1) THEN
loStringCompare.SetString(loCredentials.StringOf("session_token"))
lnStringMatch3 = loStringCompare.ContentsEqual("",1)
IF ((loCredentials.JsonTypeOf("session_token") <> 1) OR (lnStringMatch3 = 1)) THEN
? "session_token must be a nonempty string when supplied."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
CANCEL
ENDIF
loHttp.AwsSessionToken = loCredentials.StringOf("session_token")
ENDIF
* Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
* Set the actual bucket region, not the caller's location.
lcRegion = "us-west-2"
loHttp.AwsRegion = lcRegion
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("s3.")
loEndpoint.Append(lcRegion)
loEndpoint.Append(".amazonaws.com")
loHttp.AwsEndpoint = loEndpoint.GetAsString()
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
* A region redirect is not success. Correct the endpoint and signing region.
* This convenience method is UNPAGINATED. See the limitations below.
lcXmlStr = loHttp.S3_ListBuckets()
IF (loHttp.LastMethodSuccess <> 1) THEN
? "Request failed (transport or S3 error). HTTP status: " + STR(loHttp.LastStatus)
* Inspect LastErrorText privately. Do not log credentials or signed URLs.
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
CANCEL
ENDIF
IF (loHttp.LastStatus <> 200) THEN
? "Unexpected HTTP status: " + STR(loHttp.LastStatus)
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
CANCEL
ENDIF
loXml = CreateObject('Chilkat.Xml')
lnSuccess = loXml.LoadXml(lcXmlStr)
IF (lnSuccess <> 1) THEN
? "Invalid XML response."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loXml
CANCEL
ENDIF
loStringCompare.SetString(loXml.Tag)
lnStringMatch4 = loStringCompare.ContentsEqual("ListAllMyBucketsResult",1)
IF (lnStringMatch4 <> 1) THEN
? "Unexpected XML response root."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loXml
CANCEL
ENDIF
i = 0
lnCount = loXml.NumChildrenAt("Buckets")
DO WHILE (i < lnCount)
loXml.I = i
? "Bucket: " + loXml.GetChildContent("Buckets|Bucket[i]|Name")
? "Created (UTC): " + loXml.GetChildContent("Buckets|Bucket[i]|CreationDate")
* Owner.DisplayName is not required or used.
i = i + 1
ENDDO
IF (loXml.HasChildWithTag("ContinuationToken") = 1) THEN
? "More buckets exist. This unpaginated helper has not listed everything."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loXml
CANCEL
ENDIF
? "Bucket listing complete."
RELEASE loStringCompare
RELEASE loCredentials
RELEASE loHttp
RELEASE loEndpoint
RELEASE loXml