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 Lua Downloads
local chilkat = require("chilkat")
local stringCompare = chilkat.StringBuilder.new()
-- 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.
local credentials = chilkat.JsonObject.new()
credentials.strict = true
local success = credentials:load_file("qa_data/aws/s3.json")
if not success then
print("Could not load private S3 credentials.")
return
end
stringCompare:set_string(credentials:string_of("access_key"))
local stringMatch1 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("access_key") ~= 1) or (stringMatch1) then
print("Missing or invalid access_key.")
return
end
stringCompare:set_string(credentials:string_of("secret_key"))
local stringMatch2 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("secret_key") ~= 1) or (stringMatch2) then
print("Missing or invalid secret_key.")
return
end
local http = chilkat.Http.new()
http.aws_access_key = credentials:string_of("access_key")
http.aws_secret_key = credentials:string_of("secret_key")
if credentials:has_member("session_token") then
stringCompare:set_string(credentials:string_of("session_token"))
local stringMatch3 = stringCompare:contents_equal("", true)
if (credentials:json_type_of("session_token") ~= 1) or (stringMatch3) then
print("session_token must be a nonempty string when supplied.")
return
end
http.aws_session_token = credentials:string_of("session_token")
end
-- Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
-- Set the actual bucket region, not the caller's location.
local region = "us-west-2"
http.aws_region = region
local endpoint = chilkat.StringBuilder.new()
endpoint:append("s3.")
endpoint:append(region)
endpoint:append(".amazonaws.com")
http.aws_endpoint = endpoint:get_as_string()
http.require_ssl_cert_verify = true
http.follow_redirects = false
-- A region redirect is not success. Correct the endpoint and signing region.
local bucketName = "your-bucket-name"
local bucketPath = chilkat.StringBuilder.new()
local encoded = chilkat.StringBuilder.new()
local xml = chilkat.Xml.new()
local seenTokens = chilkat.Hashtable.new()
local token = ""
local more = true
local page = 0
while more do
if page == 10000 then
print("Page limit reached; listing is incomplete.")
return
end
page = page + 1
bucketPath:set_string(bucketName)
bucketPath:append("?list-type=2&max-keys=1000&encoding-type=url")
stringCompare:set_string(token)
local stringMatch4 = stringCompare:contents_equal("", true)
if not stringMatch4 then
encoded:set_string(token)
bucketPath:append("&continuation-token=")
bucketPath:append(encoded:get_encoded("url", "utf-8"))
end
local xmlStr = http:s3_list_bucket_objects(bucketPath:get_as_string())
if not http.last_method_success then
print("Request failed (transport or S3 error). HTTP status: " .. http.last_status)
-- Inspect LastErrorText privately. Do not log credentials or signed URLs.
return
end
if http.last_status ~= 200 then
print("Unexpected HTTP status: " .. http.last_status)
return
end
success = xml:load_xml(xmlStr)
if not success then
print("Invalid XML response.")
return
end
stringCompare:set_string(xml.tag)
local stringMatch5 = stringCompare:contents_equal("ListBucketResult", true)
if not stringMatch5 then
print("Unexpected XML response root.")
return
end
stringCompare:set_string(xml:get_child_content("EncodingType"))
local stringMatch6 = stringCompare:contents_equal("url", true)
if not stringMatch6 then
print("Expected URL-encoded listing keys.")
return
end
print(xml:get_xml())
local truncated = xml:get_child_content("IsTruncated")
stringCompare:set_string(truncated)
local stringMatch7 = stringCompare:contents_equal("true", true)
stringCompare:set_string(truncated)
local stringMatch8 = stringCompare:contents_equal("false", true)
if (not stringMatch7) and (not stringMatch8) then
print("Missing or invalid IsTruncated; listing is incomplete.")
return
end
more = false
stringCompare:set_string(truncated)
local stringMatch9 = stringCompare:contents_equal("true", true)
if stringMatch9 then
token = xml:get_child_content("NextContinuationToken")
stringCompare:set_string(token)
local stringMatch10 = stringCompare:contents_equal("", true)
if (not xml.last_method_success) or (stringMatch10) then
print("Missing continuation token; listing is incomplete.")
return
end
if seenTokens:contains(token) then
print("Repeated continuation token; listing is incomplete.")
return
end
success = seenTokens:add_str(token, "seen")
if not success then
print("Could not track pagination.")
return
end
more = true
end
end
print("Object listing complete.")