Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cStrXml
LOCAL oXml
LOCAL cPrefix
LOCAL i
LOCAL nCount_i

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

//  Insert your access key here:
oHttp:AwsAccessKey := "AWS_ACCESS_KEY"

//  Insert your secret key here:
oHttp: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
cStrXml := oHttp:S3_ListBucketObjects("chilkat100?prefix=images/&delimiter=/")
IF (oHttp:LastMethodSuccess != 1)
    ? oHttp:LastErrorText
    oHttp:destroy()
    RETURN
ENDIF

? "Response status code = " + Str(oHttp:LastStatus)

oXml := CreateObject("Chilkat.Xml")
nSuccess := oXml:LoadXml(cStrXml)
IF (nSuccess != 1)
    ? oXml:LastErrorText
    oHttp:destroy()
    oXml:destroy()
    RETURN
ENDIF

//  If the response status code was not 200, then the XML response is not a 
//  listing of objects, but instead contains error information.
IF (oHttp:LastStatus != 200)
    ? oXml:GetXml()
    ? "Failed."
    oHttp:destroy()
    oXml:destroy()
    RETURN
ENDIF

//  A sample response is shown below.
? oXml:GetXml()
? "----"

//  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
nCount_i := oXml:NumChildrenHavingTag("CommonPrefixes")
DO WHILE i < nCount_i
    oXml:I := i
    cPrefix := oXml:GetChildContent("CommonPrefixes[i]|Prefix")
    ? "Prefix = " + cPrefix
    i := i + 1
ENDDO

oHttp:destroy()
oXml:destroy()