S3: Retrieve Paginated ListObjectsV2 XML
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.
Prints each validated XML page separately, rather than pretending multiple pages form one XML document. Keys in the XML remain URL-encoded. Use the parsed listing example to decode them.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set stringCompare [new_CkStringBuilder]
# 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.
set credentials [new_CkJsonObject]
CkJsonObject_put_Strict $credentials 1
set success [CkJsonObject_LoadFile $credentials "qa_data/aws/s3.json"]
if {$success != 1} then {
puts "Could not load private S3 credentials."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "access_key"]
set stringMatch1 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "access_key"] != 1] || [$stringMatch1 == 1]} then {
puts "Missing or invalid access_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "secret_key"]
set stringMatch2 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "secret_key"] != 1] || [$stringMatch2 == 1]} then {
puts "Missing or invalid secret_key."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
exit
}
set http [new_CkHttp]
CkHttp_put_AwsAccessKey $http [CkJsonObject_stringOf $credentials "access_key"]
CkHttp_put_AwsSecretKey $http [CkJsonObject_stringOf $credentials "secret_key"]
if {[CkJsonObject_HasMember $credentials "session_token"] == 1} then {
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $credentials "session_token"]
set stringMatch3 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $credentials "session_token"] != 1] || [$stringMatch3 == 1]} then {
puts "session_token must be a nonempty string when supplied."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
exit
}
CkHttp_put_AwsSessionToken $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.
set region "us-west-2"
CkHttp_put_AwsRegion $http $region
set endpoint [new_CkStringBuilder]
CkStringBuilder_Append $endpoint "s3."
CkStringBuilder_Append $endpoint $region
CkStringBuilder_Append $endpoint ".amazonaws.com"
CkHttp_put_AwsEndpoint $http [CkStringBuilder_getAsString $endpoint]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
# A region redirect is not success. Correct the endpoint and signing region.
set bucketName "your-bucket-name"
set bucketPath [new_CkStringBuilder]
set encoded [new_CkStringBuilder]
set xml [new_CkXml]
set seenTokens [new_CkHashtable]
set token ""
set more 1
set page 0
while {$more == 1} {
if {$page == 10000} then {
puts "Page limit reached; listing is incomplete."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
set page [expr $page + 1]
CkStringBuilder_SetString $bucketPath $bucketName
CkStringBuilder_Append $bucketPath "?list-type=2&max-keys=1000&encoding-type=url"
CkStringBuilder_SetString $stringCompare $token
set stringMatch4 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {$stringMatch4 != 1} then {
CkStringBuilder_SetString $encoded $token
CkStringBuilder_Append $bucketPath "&continuation-token="
CkStringBuilder_Append $bucketPath [CkStringBuilder_getEncoded $encoded "url" "utf-8"]
}
set xmlStr [CkHttp_s3_ListBucketObjects $http [CkStringBuilder_getAsString $bucketPath]]
if {[CkHttp_get_LastMethodSuccess $http] != 1} then {
puts "Request failed (transport or S3 error). HTTP status: [CkHttp_get_LastStatus $http]"
# Inspect LastErrorText privately. Do not log credentials or signed URLs.
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
if {[CkHttp_get_LastStatus $http] != 200} then {
puts "Unexpected HTTP status: [CkHttp_get_LastStatus $http]"
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
set success [CkXml_LoadXml $xml $xmlStr]
if {$success != 1} then {
puts "Invalid XML response."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
CkStringBuilder_SetString $stringCompare [CkXml_tag $xml]
set stringMatch5 [CkStringBuilder_ContentsEqual $stringCompare "ListBucketResult" 1]
if {$stringMatch5 != 1} then {
puts "Unexpected XML response root."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
CkStringBuilder_SetString $stringCompare [CkXml_getChildContent $xml "EncodingType"]
set stringMatch6 [CkStringBuilder_ContentsEqual $stringCompare "url" 1]
if {$stringMatch6 != 1} then {
puts "Expected URL-encoded listing keys."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
puts [CkXml_getXml $xml]
set truncated [CkXml_getChildContent $xml "IsTruncated"]
CkStringBuilder_SetString $stringCompare $truncated
set stringMatch7 [CkStringBuilder_ContentsEqual $stringCompare "true" 1]
CkStringBuilder_SetString $stringCompare $truncated
set stringMatch8 [CkStringBuilder_ContentsEqual $stringCompare "false" 1]
if {expr [$stringMatch7 != 1] && [$stringMatch8 != 1]} then {
puts "Missing or invalid IsTruncated; listing is incomplete."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
set more 0
CkStringBuilder_SetString $stringCompare $truncated
set stringMatch9 [CkStringBuilder_ContentsEqual $stringCompare "true" 1]
if {$stringMatch9 == 1} then {
set token [CkXml_getChildContent $xml "NextContinuationToken"]
CkStringBuilder_SetString $stringCompare $token
set stringMatch10 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkXml_get_LastMethodSuccess $xml] != 1] || [$stringMatch10 == 1]} then {
puts "Missing continuation token; listing is incomplete."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
if {[CkHashtable_Contains $seenTokens $token] == 1} then {
puts "Repeated continuation token; listing is incomplete."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
set success [CkHashtable_AddStr $seenTokens $token "seen"]
if {$success != 1} then {
puts "Could not track pagination."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens
exit
}
set more 1
}
}
puts "Object listing complete."
delete_CkStringBuilder $stringCompare
delete_CkJsonObject $credentials
delete_CkHttp $http
delete_CkStringBuilder $endpoint
delete_CkStringBuilder $bucketPath
delete_CkStringBuilder $encoded
delete_CkXml $xml
delete_CkHashtable $seenTokens