S3: List Objects with ListObjectsV2
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.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_StringCompare
oleobject loo_Credentials
long li_StringMatch1
long li_StringMatch2
oleobject loo_Http
long li_StringMatch3
string ls_Region
oleobject loo_Endpoint
string ls_BucketName
oleobject loo_BucketPath
oleobject loo_Encoded
oleobject loo_Xml
oleobject loo_SeenTokens
string ls_Token
long li_More
long li_Page
long li_StringMatch4
string ls_XmlStr
long li_StringMatch5
long li_StringMatch6
long i
long li_Count
string ls_ItemKey
long li_StringMatch7
string ls_Size
long li_StringMatch8
string ls_Modified
long li_StringMatch9
string ls_Truncated
long li_StringMatch10
long li_StringMatch11
long li_StringMatch12
long li_StringMatch13
li_Success = 0
loo_StringCompare = create oleobject
li_rc = loo_StringCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_StringCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// 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.
loo_Credentials = create oleobject
li_rc = loo_Credentials.ConnectToNewObject("Chilkat.JsonObject")
loo_Credentials.Strict = 1
li_Success = loo_Credentials.LoadFile("qa_data/aws/s3.json")
if li_Success <> 1 then
Write-Debug "Could not load private S3 credentials."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("access_key"))
li_StringMatch1 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_StringMatch1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_StringCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_StringMatch2 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_StringMatch2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_StringCompare
destroy loo_Credentials
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.AwsAccessKey = loo_Credentials.StringOf("access_key")
loo_Http.AwsSecretKey = loo_Credentials.StringOf("secret_key")
if loo_Credentials.HasMember("session_token") = 1 then
loo_StringCompare.SetString(loo_Credentials.StringOf("session_token"))
li_StringMatch3 = loo_StringCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("session_token") <> 1) OR (li_StringMatch3 = 1) then
Write-Debug "session_token must be a nonempty string when supplied."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
return
end if
loo_Http.AwsSessionToken = loo_Credentials.StringOf("session_token")
end if
// Current Chilkat uses SigV4 by default. Do not select deprecated SigV2.
// Set the actual bucket region, not the caller's location.
ls_Region = "us-west-2"
loo_Http.AwsRegion = ls_Region
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
loo_Endpoint.Append("s3.")
loo_Endpoint.Append(ls_Region)
loo_Endpoint.Append(".amazonaws.com")
loo_Http.AwsEndpoint = loo_Endpoint.GetAsString()
loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
// A region redirect is not success. Correct the endpoint and signing region.
ls_BucketName = "your-bucket-name"
loo_BucketPath = create oleobject
li_rc = loo_BucketPath.ConnectToNewObject("Chilkat.StringBuilder")
loo_Encoded = create oleobject
li_rc = loo_Encoded.ConnectToNewObject("Chilkat.StringBuilder")
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
loo_SeenTokens = create oleobject
li_rc = loo_SeenTokens.ConnectToNewObject("Chilkat.Hashtable")
ls_Token = ""
li_More = 1
li_Page = 0
do while (li_More = 1)
if li_Page = 10000 then
Write-Debug "Page limit reached; listing is incomplete."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
li_Page = li_Page + 1
loo_BucketPath.SetString(ls_BucketName)
loo_BucketPath.Append("?list-type=2&max-keys=1000&encoding-type=url")
loo_StringCompare.SetString(ls_Token)
li_StringMatch4 = loo_StringCompare.ContentsEqual("",1)
if li_StringMatch4 <> 1 then
loo_Encoded.SetString(ls_Token)
loo_BucketPath.Append("&continuation-token=")
loo_BucketPath.Append(loo_Encoded.GetEncoded("url","utf-8"))
end if
ls_XmlStr = loo_Http.S3_ListBucketObjects(loo_BucketPath.GetAsString())
if loo_Http.LastMethodSuccess <> 1 then
Write-Debug "Request failed (transport or S3 error). HTTP status: " + string(loo_Http.LastStatus)
// Inspect LastErrorText privately. Do not log credentials or signed URLs.
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
if loo_Http.LastStatus <> 200 then
Write-Debug "Unexpected HTTP status: " + string(loo_Http.LastStatus)
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
li_Success = loo_Xml.LoadXml(ls_XmlStr)
if li_Success <> 1 then
Write-Debug "Invalid XML response."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
loo_StringCompare.SetString(loo_Xml.Tag)
li_StringMatch5 = loo_StringCompare.ContentsEqual("ListBucketResult",1)
if li_StringMatch5 <> 1 then
Write-Debug "Unexpected XML response root."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
loo_StringCompare.SetString(loo_Xml.GetChildContent("EncodingType"))
li_StringMatch6 = loo_StringCompare.ContentsEqual("url",1)
if li_StringMatch6 <> 1 then
Write-Debug "Expected URL-encoded listing keys."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
i = 0
li_Count = loo_Xml.NumChildrenHavingTag("Contents")
do while (i < li_Count)
loo_Xml.I = i
// Decode keys exactly once. Sizes remain decimal strings (not int).
ls_ItemKey = loo_Http.UrlDecode(loo_Xml.GetChildContent("Contents[i]|Key"))
loo_StringCompare.SetString(ls_ItemKey)
li_StringMatch7 = loo_StringCompare.ContentsEqual("",1)
if (loo_Http.LastMethodSuccess <> 1) OR (li_StringMatch7 = 1) then
Write-Debug "Invalid object key."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
ls_Size = loo_Xml.GetChildContent("Contents[i]|Size")
loo_StringCompare.SetString(ls_Size)
li_StringMatch8 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch8 = 1) then
Write-Debug "Incomplete object entry."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
ls_Modified = loo_Xml.GetChildContent("Contents[i]|LastModified")
loo_StringCompare.SetString(ls_Modified)
li_StringMatch9 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch9 = 1) then
Write-Debug "Incomplete object entry."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
Write-Debug "Key: " + ls_ItemKey
Write-Debug "Bytes: " + ls_Size
// S3 returns ISO 8601, not RFC 822. Keep the original UTC timestamp.
Write-Debug "Last modified (UTC): " + ls_Modified
i = i + 1
loop
ls_Truncated = loo_Xml.GetChildContent("IsTruncated")
loo_StringCompare.SetString(ls_Truncated)
li_StringMatch10 = loo_StringCompare.ContentsEqual("true",1)
loo_StringCompare.SetString(ls_Truncated)
li_StringMatch11 = loo_StringCompare.ContentsEqual("false",1)
if (li_StringMatch10 <> 1) AND (li_StringMatch11 <> 1) then
Write-Debug "Missing or invalid IsTruncated; listing is incomplete."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
li_More = 0
loo_StringCompare.SetString(ls_Truncated)
li_StringMatch12 = loo_StringCompare.ContentsEqual("true",1)
if li_StringMatch12 = 1 then
ls_Token = loo_Xml.GetChildContent("NextContinuationToken")
loo_StringCompare.SetString(ls_Token)
li_StringMatch13 = loo_StringCompare.ContentsEqual("",1)
if (loo_Xml.LastMethodSuccess <> 1) OR (li_StringMatch13 = 1) then
Write-Debug "Missing continuation token; listing is incomplete."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
if loo_SeenTokens.Contains(ls_Token) = 1 then
Write-Debug "Repeated continuation token; listing is incomplete."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
li_Success = loo_SeenTokens.AddStr(ls_Token,"seen")
if li_Success <> 1 then
Write-Debug "Could not track pagination."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens
return
end if
li_More = 1
end if
loop
Write-Debug "Object listing complete."
destroy loo_StringCompare
destroy loo_Credentials
destroy loo_Http
destroy loo_Endpoint
destroy loo_BucketPath
destroy loo_Encoded
destroy loo_Xml
destroy loo_SeenTokens