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 C Downloads
#include <C_CkStringBuilder.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkXml.h>
void ChilkatSample(void)
{
BOOL success;
HCkStringBuilder stringCompare;
HCkJsonObject credentials;
BOOL stringMatch1;
BOOL stringMatch2;
HCkHttp http;
BOOL stringMatch3;
const char *region;
HCkStringBuilder endpoint;
const char *xmlStr;
HCkXml xml;
BOOL stringMatch4;
int i;
int count;
success = FALSE;
stringCompare = CkStringBuilder_Create();
// 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.
credentials = CkJsonObject_Create();
CkJsonObject_putStrict(credentials,TRUE);
success = CkJsonObject_LoadFile(credentials,"qa_data/aws/s3.json");
if (success != TRUE) {
printf("Could not load private S3 credentials.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"access_key"));
stringMatch1 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"access_key") != 1) || (stringMatch1 == TRUE)) {
printf("Missing or invalid access_key.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"secret_key"));
stringMatch2 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"secret_key") != 1) || (stringMatch2 == TRUE)) {
printf("Missing or invalid secret_key.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
return;
}
http = CkHttp_Create();
CkHttp_putAwsAccessKey(http,CkJsonObject_stringOf(credentials,"access_key"));
CkHttp_putAwsSecretKey(http,CkJsonObject_stringOf(credentials,"secret_key"));
if (CkJsonObject_HasMember(credentials,"session_token") == TRUE) {
CkStringBuilder_SetString(stringCompare,CkJsonObject_stringOf(credentials,"session_token"));
stringMatch3 = CkStringBuilder_ContentsEqual(stringCompare,"",TRUE);
if ((CkJsonObject_JsonTypeOf(credentials,"session_token") != 1) || (stringMatch3 == TRUE)) {
printf("session_token must be a nonempty string when supplied.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
return;
}
CkHttp_putAwsSessionToken(http,CkJsonObject_stringOf(credentials,"session_token"));
}
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
region = "us-west-2";
CkHttp_putAwsRegion(http,region);
endpoint = CkStringBuilder_Create();
CkStringBuilder_Append(endpoint,"s3.");
CkStringBuilder_Append(endpoint,region);
CkStringBuilder_Append(endpoint,".amazonaws.com");
CkHttp_putAwsEndpoint(http,CkStringBuilder_getAsString(endpoint));
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putFollowRedirects(http,FALSE);
// A region redirect is not success. Correct the endpoint and signing region.
// This convenience method is UNPAGINATED. See the limitations below.
xmlStr = CkHttp_s3_ListBuckets(http);
if (CkHttp_getLastMethodSuccess(http) != TRUE) {
printf("Request failed (transport or S3 error). HTTP status: %d\n",CkHttp_getLastStatus(http));
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
return;
}
if (CkHttp_getLastStatus(http) != 200) {
printf("Unexpected HTTP status: %d\n",CkHttp_getLastStatus(http));
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
return;
}
xml = CkXml_Create();
success = CkXml_LoadXml(xml,xmlStr);
if (success != TRUE) {
printf("Invalid XML response.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(xml);
return;
}
CkStringBuilder_SetString(stringCompare,CkXml_tag(xml));
stringMatch4 = CkStringBuilder_ContentsEqual(stringCompare,"ListAllMyBucketsResult",TRUE);
if (stringMatch4 != TRUE) {
printf("Unexpected XML response root.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(xml);
return;
}
i = 0;
count = CkXml_NumChildrenAt(xml,"Buckets");
while ((i < count)) {
CkXml_putI(xml,i);
printf("Bucket: %s\n",CkXml_getChildContent(xml,"Buckets|Bucket[i]|Name"));
printf("Created (UTC): %s\n",CkXml_getChildContent(xml,"Buckets|Bucket[i]|CreationDate"));
// Owner.DisplayName is not required or used.
i = i + 1;
}
if (CkXml_HasChildWithTag(xml,"ContinuationToken") == TRUE) {
printf("More buckets exist. This unpaginated helper has not listed everything.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(xml);
return;
}
printf("Bucket listing complete.\n");
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(xml);
}