S3: List Objects Under a Prefix
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.
A prefix is a key-name filter, not a filesystem folder. The trailing slash matters: images/ excludes keys such as images-old/file.jpg. Without a delimiter, all matching descendant keys are included.
Chilkat Delphi DLL Downloads
var
success: Boolean;
stringCompare: HCkStringBuilder;
credentials: HCkJsonObject;
stringMatch1: Boolean;
stringMatch2: Boolean;
http: HCkHttp;
stringMatch3: Boolean;
region: PWideChar;
endpoint: HCkStringBuilder;
bucketName: PWideChar;
bucketPath: HCkStringBuilder;
encoded: HCkStringBuilder;
xml: HCkXml;
seenTokens: HCkHashtable;
token: PWideChar;
more: Boolean;
page: Integer;
prefix: PWideChar;
stringMatch4: Boolean;
xmlStr: PWideChar;
stringMatch5: Boolean;
stringMatch6: Boolean;
i: Integer;
count: Integer;
itemKey: PWideChar;
stringMatch7: Boolean;
size: PWideChar;
stringMatch8: Boolean;
modified: PWideChar;
stringMatch9: Boolean;
truncated: PWideChar;
stringMatch10: Boolean;
stringMatch11: Boolean;
stringMatch12: Boolean;
stringMatch13: Boolean;
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.
bucketName := 'your-bucket-name';
bucketPath := CkStringBuilder_Create();
encoded := CkStringBuilder_Create();
xml := CkXml_Create();
seenTokens := CkHashtable_Create();
token := '';
more := True;
page := 0;
prefix := 'images/';
while (more = True) do
begin
if (page = 10000) then
begin
Memo1.Lines.Add('Page limit reached; listing is incomplete.');
Exit;
end;
page := page + 1;
CkStringBuilder_SetString(bucketPath,bucketName);
CkStringBuilder_Append(bucketPath,'?list-type=2&max-keys=1000&encoding-type=url');
CkStringBuilder_SetString(encoded,prefix);
CkStringBuilder_Append(bucketPath,'&prefix=');
CkStringBuilder_Append(bucketPath,CkStringBuilder__getEncoded(encoded,'url','utf-8'));
CkStringBuilder_SetString(stringCompare,token);
stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if (stringMatch4 <> True) then
begin
CkStringBuilder_SetString(encoded,token);
CkStringBuilder_Append(bucketPath,'&continuation-token=');
CkStringBuilder_Append(bucketPath,CkStringBuilder__getEncoded(encoded,'url','utf-8'));
end;
xmlStr := CkHttp__s3_ListBucketObjects(http,CkStringBuilder__getAsString(bucketPath));
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;
success := CkXml_LoadXml(xml,xmlStr);
if (success <> True) then
begin
Memo1.Lines.Add('Invalid XML response.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkXml__tag(xml));
stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'ListBucketResult',True);
if (stringMatch5 <> True) then
begin
Memo1.Lines.Add('Unexpected XML response root.');
Exit;
end;
CkStringBuilder_SetString(stringCompare,CkXml__getChildContent(xml,'EncodingType'));
stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'url',True);
if (stringMatch6 <> True) then
begin
Memo1.Lines.Add('Expected URL-encoded listing keys.');
Exit;
end;
i := 0;
count := CkXml_NumChildrenHavingTag(xml,'Contents');
while (i < count) do
begin
CkXml_putI(xml,i);
// Decode keys exactly once. Sizes remain decimal strings (not int).
itemKey := CkHttp__urlDecode(http,CkXml__getChildContent(xml,'Contents[i]|Key'));
CkStringBuilder_SetString(stringCompare,itemKey);
stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkHttp_getLastMethodSuccess(http) <> True) or (stringMatch7 = True)) then
begin
Memo1.Lines.Add('Invalid object key.');
Exit;
end;
size := CkXml__getChildContent(xml,'Contents[i]|Size');
CkStringBuilder_SetString(stringCompare,size);
stringMatch8 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch8 = True)) then
begin
Memo1.Lines.Add('Incomplete object entry.');
Exit;
end;
modified := CkXml__getChildContent(xml,'Contents[i]|LastModified');
CkStringBuilder_SetString(stringCompare,modified);
stringMatch9 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch9 = True)) then
begin
Memo1.Lines.Add('Incomplete object entry.');
Exit;
end;
Memo1.Lines.Add('Key: ' + itemKey);
Memo1.Lines.Add('Bytes: ' + size);
// S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
Memo1.Lines.Add('Last modified (UTC): ' + modified);
i := i + 1;
end;
truncated := CkXml__getChildContent(xml,'IsTruncated');
CkStringBuilder_SetString(stringCompare,truncated);
stringMatch10 := CkStringBuilder_ContentsEqual(stringCompare,'true',True);
CkStringBuilder_SetString(stringCompare,truncated);
stringMatch11 := CkStringBuilder_ContentsEqual(stringCompare,'false',True);
if ((stringMatch10 <> True) and (stringMatch11 <> True)) then
begin
Memo1.Lines.Add('Missing or invalid IsTruncated; listing is incomplete.');
Exit;
end;
more := False;
CkStringBuilder_SetString(stringCompare,truncated);
stringMatch12 := CkStringBuilder_ContentsEqual(stringCompare,'true',True);
if (stringMatch12 = True) then
begin
token := CkXml__getChildContent(xml,'NextContinuationToken');
CkStringBuilder_SetString(stringCompare,token);
stringMatch13 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkXml_getLastMethodSuccess(xml) <> True) or (stringMatch13 = True)) then
begin
Memo1.Lines.Add('Missing continuation token; listing is incomplete.');
Exit;
end;
if (CkHashtable_Contains(seenTokens,token) = True) then
begin
Memo1.Lines.Add('Repeated continuation token; listing is incomplete.');
Exit;
end;
success := CkHashtable_AddStr(seenTokens,token,'seen');
if (success <> True) then
begin
Memo1.Lines.Add('Could not track pagination.');
Exit;
end;
more := True;
end;
end;
Memo1.Lines.Add('Object listing complete.');
CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(endpoint);
CkStringBuilder_Dispose(bucketPath);
CkStringBuilder_Dispose(encoded);
CkXml_Dispose(xml);
CkHashtable_Dispose(seenTokens);