PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkXml.pb"
IncludeFile "CkHttp.pb"
Procedure ChilkatExample()
success.i = 0
; This example assumes the Chilkat HTTP API to have been previously unlocked.
; See Global Unlock Sample for sample code.
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Insert your access key here:
CkHttp::setCkAwsAccessKey(http, "AWS_ACCESS_KEY")
; Insert your secret key here:
CkHttp::setCkAwsSecretKey(http, "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
strXml.s = CkHttp::ckS3_ListBucketObjects(http,"chilkat100?prefix=images/&delimiter=/")
If CkHttp::ckLastMethodSuccess(http) <> 1
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
ProcedureReturn
EndIf
Debug "Response status code = " + Str(CkHttp::ckLastStatus(http))
xml.i = CkXml::ckCreate()
If xml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkXml::ckLoadXml(xml,strXml)
If success <> 1
Debug CkXml::ckLastErrorText(xml)
CkHttp::ckDispose(http)
CkXml::ckDispose(xml)
ProcedureReturn
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 CkHttp::ckLastStatus(http) <> 200
Debug CkXml::ckGetXml(xml)
Debug "Failed."
CkHttp::ckDispose(http)
CkXml::ckDispose(xml)
ProcedureReturn
EndIf
; A sample response is shown below.
Debug CkXml::ckGetXml(xml)
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>
Prefix.s
; The XML can be parsed like this:
i.i = 0
count_i.i = CkXml::ckNumChildrenHavingTag(xml,"CommonPrefixes")
While i < count_i
CkXml::setCkI(xml, i)
Prefix = CkXml::ckGetChildContent(xml,"CommonPrefixes[i]|Prefix")
Debug "Prefix = " + Prefix
i = i + 1
Wend
CkHttp::ckDispose(http)
CkXml::ckDispose(xml)
ProcedureReturn
EndProcedure