Amazon S3: List Objects with ListObjectsV2
See more S3 - REST Examples
List current keys under a prefix using ListObjectsV2. Follow NextContinuationToken even when a page is empty. Requires s3:ListBucket. encoding-type=url makes XML-safe keys; decode once before using a returned key. Sizes and ETags remain strings. A listing is not an atomic snapshot across pages.
For general purpose S3 buckets, using regional HTTPS endpoints and AWS Signature Version 4. Directory buckets (S3 Express), access points, Outposts, and other AWS partitions need their own endpoint/authentication rules. Use a bucket name without dots for these virtual-hosted TLS examples; see the path-style example for dotted names.
Provide current IAM credentials in a private qa_data/aws/s3.json file with string fields access_key, secret_key, and session_token for temporary credentials. Prefer short-lived role credentials. This file is explicitly loaded; these examples do not discover profiles or refresh credentials. Keep it outside public content and source control. The session token is included in the signed request. TLS certificate verification is enabled on the Socket used by Rest.
Transport success and HTTP success are different. Keep full diagnostics private; signed requests and presigned URLs can contain credentials or grant access. For retryable reads use bounded backoff with jitter. Before retrying a write with an uncertain outcome, check the resource state. Keep keys, upload IDs, version IDs, ETags, and large sizes as strings.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_TextCompare
oleobject loo_Credentials
long li_SameText1
long li_SameText2
string ls_Region
string ls_BucketName
oleobject loo_Endpoint
oleobject loo_Aws
oleobject loo_Rest
long li_SameText3
oleobject loo_Connection
oleobject loo_Xml
oleobject loo_SeenTokens
string ls_NextToken
long li_More
long li_Page
string ls_ResponseBody
long li_SameText4
long li_SameText5
long i
long li_SameText6
long li_SameText7
long li_SameText8
oleobject loo_DecodedKey
long li_EscapedPluses
long li_SameText9
long li_SameText10
long li_SameText11
long li_SameText12
long li_SameText13
long li_SameText14
li_Success = 0
// StringBuilder compares text by value in every generated language.
loo_TextCompare = create oleobject
li_rc = loo_TextCompare.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_TextCompare
MessageBox("Error","Connecting to COM object failed")
return
end if
// Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
// Private configuration, not a file in public web content or source control.
// Load current IAM credentials authorized for the S3 operation.
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 AWS credentials."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("access_key"))
li_SameText1 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("access_key") <> 1) OR (li_SameText1 = 1) then
Write-Debug "Missing or invalid access_key."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("secret_key"))
li_SameText2 = loo_TextCompare.ContentsEqual("",1)
if (loo_Credentials.JsonTypeOf("secret_key") <> 1) OR (li_SameText2 = 1) then
Write-Debug "Missing or invalid secret_key."
destroy loo_TextCompare
destroy loo_Credentials
return
end if
// General purpose bucket in the commercial AWS partition.
ls_Region = "us-west-2"
ls_BucketName = "amzn-s3-demo-bucket"
loo_Endpoint = create oleobject
li_rc = loo_Endpoint.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Endpoint.Append(ls_BucketName)
if li_Success <> 1 then
Write-Debug "Could not build text."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
li_Success = loo_Endpoint.Append(".")
if li_Success <> 1 then
Write-Debug "Could not build text."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
li_Success = loo_Endpoint.Append("s3.")
if li_Success <> 1 then
Write-Debug "Could not build text."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
li_Success = loo_Endpoint.Append(ls_Region)
if li_Success <> 1 then
Write-Debug "Could not build text."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
li_Success = loo_Endpoint.Append(".amazonaws.com")
if li_Success <> 1 then
Write-Debug "Could not build text."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
return
end if
loo_Aws = create oleobject
li_rc = loo_Aws.ConnectToNewObject("Chilkat.AuthAws")
loo_Aws.AccessKey = loo_Credentials.StringOf("access_key")
loo_Aws.SecretKey = loo_Credentials.StringOf("secret_key")
loo_Aws.Region = ls_Region
loo_Aws.ServiceName = "s3"
loo_Aws.SignatureVersion = 4
loo_Rest = create oleobject
li_rc = loo_Rest.ConnectToNewObject("Chilkat.Rest")
loo_Rest.ConnectTimeoutMs = 20000
loo_Rest.IdleTimeoutMs = 60000
li_Success = loo_Rest.SetAuthAws(loo_Aws)
if li_Success <> 1 then
Write-Debug "Could not configure AWS signing."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
// Temporary credentials require this token in every signed query.
// Keep request URLs private. Do not clear this parameter between requests.
if loo_Credentials.HasMember("session_token") = 1 then
if loo_Credentials.JsonTypeOf("session_token") <> 1 then
Write-Debug "session_token must be a string."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Credentials.StringOf("session_token"))
li_SameText3 = loo_TextCompare.ContentsEqual("",1)
if li_SameText3 <> 1 then
li_Success = loo_Rest.AddQueryParam("X-Amz-Security-Token",loo_Credentials.StringOf("session_token"))
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
return
end if
end if
end if
// Connect to the SAME hostname used for TLS verification and request signing.
loo_Connection = create oleobject
li_rc = loo_Connection.ConnectToNewObject("Chilkat.Socket")
loo_Connection.RequireSslCertVerify = 1
loo_Connection.MaxReadIdleMs = 60000
loo_Connection.MaxSendIdleMs = 60000
li_Success = loo_Connection.Connect(loo_Endpoint.GetAsString(),443,1,20000)
if li_Success <> 1 then
Write-Debug "TLS connection failed; inspect diagnostics privately."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
li_Success = loo_Rest.UseConnection(loo_Connection,1)
if li_Success <> 1 then
Write-Debug "Could not attach the TLS connection."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
return
end if
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
loo_Xml.Strict = 1
li_Success = loo_Rest.AddQueryParam("list-type","2")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
li_Success = loo_Rest.AddQueryParam("max-keys","1000")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
li_Success = loo_Rest.AddQueryParam("encoding-type","url")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
// Optional prefix filter; a slash is part of an object key, not a real folder.
li_Success = loo_Rest.AddQueryParam("prefix","documents/")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
return
end if
loo_SeenTokens = create oleobject
li_rc = loo_SeenTokens.ConnectToNewObject("Chilkat.Hashtable")
ls_NextToken = ""
li_More = 1
li_Page = 0
do while (li_More = 1)
if li_Page = 1000 then
Write-Debug "Page limit reached; enumeration is incomplete."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
ls_ResponseBody = loo_Rest.FullRequestNoBody("GET","/")
if loo_Rest.LastMethodSuccess <> 1 then
Write-Debug "Request did not complete; inspect diagnostics privately."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
if loo_Rest.ResponseStatusCode <> 200 then
Write-Debug "S3 HTTP status: " + string(loo_Rest.ResponseStatusCode)
li_Success = loo_Xml.LoadXml(ls_ResponseBody)
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.Tag)
li_SameText4 = loo_TextCompare.ContentsEqual("Error",1)
if (li_Success = 1) AND (li_SameText4 = 1) then
Write-Debug "S3 error code: " + loo_Xml.GetChildContent("Code")
end if
// Keep signed headers, credentials, URLs, and full diagnostics private.
// A timeout may leave a write's outcome uncertain. Check before retrying.
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
li_Success = loo_Xml.LoadXml(ls_ResponseBody)
if li_Success <> 1 then
Write-Debug "Expected well-formed XML."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.Tag)
li_SameText5 = loo_TextCompare.ContentsEqual("ListBucketResult",1)
if li_SameText5 <> 1 then
Write-Debug "Unexpected S3 response element; this is not a successful result."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
i = 0
do while (i < loo_Xml.NumChildrenHavingTag("Contents"))
loo_Xml.I = i
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("Contents[i]|Key"))
li_SameText6 = loo_TextCompare.ContentsEqual("",1)
if (loo_Xml.HasChildWithTag("Contents[i]|Key") <> 1) OR (li_SameText6 = 1) then
Write-Debug "Missing response field: Contents[i]|Key"
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
Write-Debug "Encoded key: " + loo_Xml.GetChildContent("Contents[i]|Key")
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("Contents[i]|Size"))
li_SameText7 = loo_TextCompare.ContentsEqual("",1)
if (loo_Xml.HasChildWithTag("Contents[i]|Size") <> 1) OR (li_SameText7 = 1) then
Write-Debug "Missing response field: Contents[i]|Size"
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
Write-Debug "Size: " + loo_Xml.GetChildContent("Contents[i]|Size")
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("Contents[i]|ETag"))
li_SameText8 = loo_TextCompare.ContentsEqual("",1)
if (loo_Xml.HasChildWithTag("Contents[i]|ETag") <> 1) OR (li_SameText8 = 1) then
Write-Debug "Missing response field: Contents[i]|ETag"
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
return
end if
Write-Debug "ETag: " + loo_Xml.GetChildContent("Contents[i]|ETag")
// Decode percent escapes without interpreting a literal plus as a space.
loo_DecodedKey = create oleobject
li_rc = loo_DecodedKey.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_DecodedKey.Append(loo_Xml.GetChildContent("Contents[i]|Key"))
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
li_EscapedPluses = loo_DecodedKey.Replace("+","%2B")
li_Success = loo_DecodedKey.Decode("url","utf-8")
if li_Success <> 1 then
Write-Debug "Could not decode object key."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
Write-Debug "Key: " + loo_DecodedKey.GetAsString()
i = i + 1
loop
li_Page = li_Page + 1
li_More = 0
ls_NextToken = ""
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("IsTruncated"))
li_SameText9 = loo_TextCompare.ContentsEqual("",1)
if (loo_Xml.HasChildWithTag("IsTruncated") <> 1) OR (li_SameText9 = 1) then
Write-Debug "Missing response field: IsTruncated"
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("IsTruncated"))
li_SameText10 = loo_TextCompare.ContentsEqual("true",1)
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("IsTruncated"))
li_SameText11 = loo_TextCompare.ContentsEqual("false",1)
if (li_SameText10 <> 1) AND (li_SameText11 <> 1) then
Write-Debug "Invalid IsTruncated value."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("IsTruncated"))
li_SameText12 = loo_TextCompare.ContentsEqual("true",1)
if li_SameText12 = 1 then
loo_TextCompare.Clear()
loo_TextCompare.SetString(loo_Xml.GetChildContent("NextContinuationToken"))
li_SameText13 = loo_TextCompare.ContentsEqual("",1)
if (loo_Xml.HasChildWithTag("NextContinuationToken") <> 1) OR (li_SameText13 = 1) then
Write-Debug "Missing response field: NextContinuationToken"
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
ls_NextToken = loo_Xml.GetChildContent("NextContinuationToken")
end if
loo_TextCompare.Clear()
loo_TextCompare.SetString(ls_NextToken)
li_SameText14 = loo_TextCompare.ContentsEqual("",1)
if li_SameText14 <> 1 then
if loo_SeenTokens.Contains(ls_NextToken) = 1 then
Write-Debug "Repeated pagination token; enumeration is incomplete."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
li_Success = loo_SeenTokens.AddInt(ls_NextToken,1)
if li_Success <> 1 then
Write-Debug "Could not track pagination."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
if li_Page > 1 then
li_Success = loo_Rest.RemoveQueryParam("continuation-token")
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
end if
li_Success = loo_Rest.AddQueryParam("continuation-token",ls_NextToken)
if li_Success <> 1 then
Write-Debug "Could not prepare the request."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey
return
end if
li_More = 1
end if
loop
Write-Debug "Enumeration complete."
destroy loo_TextCompare
destroy loo_Credentials
destroy loo_Endpoint
destroy loo_Aws
destroy loo_Rest
destroy loo_Connection
destroy loo_Xml
destroy loo_SeenTokens
destroy loo_DecodedKey