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 Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
credentials: HCkJsonObject;
stringMatch1: Boolean;
stringMatch2: Boolean;
http: HCkHttp;
stringMatch3: Boolean;
region: PWideChar;
endpoint: HCkStringBuilder;
xmlStr: PWideChar;
xml: HCkXml;
stringMatch4: Boolean;
i: Integer;
count: Integer;
begin
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) then
begin
Memo1.Lines.Add('Could not load private S3 credentials.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'access_key'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'access_key') <> 1) or (stringMatch1 = True)) then
begin
Memo1.Lines.Add('Missing or invalid access_key.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'secret_key'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'secret_key') <> 1) or (stringMatch2 = True)) then
begin
Memo1.Lines.Add('Missing or invalid secret_key.');
Exit;
end;
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) then
begin
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'session_token'));
stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'session_token') <> 1) or (stringMatch3 = True)) then
begin
Memo1.Lines.Add('session_token must be a nonempty string when supplied.');
Exit;
end;
CkHttp_putAwsSessionToken(http,CkJsonObject__stringOf(credentials,'session_token'));
end;
// 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) then
begin
Memo1.Lines.Add('Request failed (transport or S3 error). HTTP status: ' + IntToStr(CkHttp_getLastStatus(http)));
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
Exit;
end;
if (CkHttp_getLastStatus(http) <> 200) then
begin
Memo1.Lines.Add('Unexpected HTTP status: ' + IntToStr(CkHttp_getLastStatus(http)));
Exit;
end;
xml := CkXml_Create();
success := CkXml_LoadXml(xml,xmlStr);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid XML response.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(xml));
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'ListAllMyBucketsResult',True);
if (stringMatch4 <> True) then
begin
Memo1.Lines.Add('Unexpected XML response root.');
Exit;
end;
i := 0;
count := CkXml_NumChildrenAt(xml,'Buckets');
while (i < count) do
begin
CkXml_putI(xml,i);
Memo1.Lines.Add('Bucket: ' + CkXml__getChildContent(xml,'Buckets|Bucket[i]|Name'));
Memo1.Lines.Add('Created (UTC): ' + CkXml__getChildContent(xml,'Buckets|Bucket[i]|CreationDate'));
// Owner.DisplayName is not required or used.
i := i + 1;
end;
if (CkXml_HasChildWithTag(xml,'ContinuationToken') = True) then
begin
Memo1.Lines.Add('More buckets exist. This unpaginated helper has not listed everything.');
Exit;
end;
Memo1.Lines.Add('Bucket listing complete.');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkXml_Dispose(xml);