Sample code for 30+ languages & platforms
PowerBuilder

S3 Get Bucket Objects with CommonPrefixes

See more Amazon S3 Examples

Demonstrates how to get a list of bucket objects using the prefix and delimiter query params to get an XML result with CommonPrefixes.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
string ls_StrXml
oleobject loo_Xml
string ls_Prefix
integer i
integer li_Count_i

li_Success = 0

// This example assumes the Chilkat HTTP API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Insert your access key here:
loo_Http.AwsAccessKey = "AWS_ACCESS_KEY"

// Insert your secret key here:
loo_Http.AwsSecretKey = "AWS_SECRET_KEY"

// In this example, my bucket is "chilkat100".
// It contains a number of folders, one of which is named "images".
// I want to get a list of all sub-folders under the "images" folder
ls_StrXml = loo_Http.S3_ListBucketObjects("chilkat100?prefix=images/&delimiter=/")
if loo_Http.LastMethodSuccess <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    return
end if

Write-Debug "Response status code = " + string(loo_Http.LastStatus)

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

li_Success = loo_Xml.LoadXml(ls_StrXml)
if li_Success <> 1 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Http
    destroy loo_Xml
    return
end if

// If the response status code was not 200, then the XML response is not a 
// listing of objects, but instead contains error information.
if loo_Http.LastStatus <> 200 then
    Write-Debug loo_Xml.GetXml()
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Xml
    return
end if

// A sample response is shown below.
Write-Debug loo_Xml.GetXml()
Write-Debug "----"

// Here is the list of sub-folders (i.e. CommonPrefixes)

// <?xml version="1.0" encoding="UTF-8"?>
// <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
//     <Name>chilkat100</Name>
//     <Prefix>images/</Prefix>
//     <Marker/>
//     <MaxKeys>1000</MaxKeys>
//     <Delimiter>/</Delimiter>
//     <IsTruncated>false</IsTruncated>
//     <CommonPrefixes>
//         <Prefix>images/africa/</Prefix>
//     </CommonPrefixes>
//     <CommonPrefixes>
//         <Prefix>images/sea_creatures/</Prefix>
//     </CommonPrefixes>
// </ListBucketResult>

// The XML can be parsed like this:
i = 0
li_Count_i = loo_Xml.NumChildrenHavingTag("CommonPrefixes")
do while i < li_Count_i
    loo_Xml.I = i
    ls_Prefix = loo_Xml.GetChildContent("CommonPrefixes[i]|Prefix")
    Write-Debug "Prefix = " + ls_Prefix
    i = i + 1
loop


destroy loo_Http
destroy loo_Xml