PowerBuilder
PowerBuilder
S3 List Objects in Bucket Folder
See more Amazon S3 Examples
Demonstrates how to retrieve the XML listing of the objects (i.e. files) stored in an Amazon S3 bucket, but just within a particular folder.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Http
string ls_StrXml
oleobject loo_Xml
integer li_NumItems
string ls_ItemKey
string ls_ItemSizeDecimalStr
string ls_LastModTimestamp
oleobject loo_Dt
oleobject loo_DtObj
integer i
integer li_BLocal
li_Success = 0
// This example assumes the Chilkat HTTP API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// In Amazon S3 (Simple Storage Service), there is a distinction between buckets and folders,
// which are both used for organizing and storing data.
// Bucket: A bucket is the top-level container in Amazon S3. It is similar to a directory or folder
// at the root level and acts as a unique namespace for your objects (files). Each bucket must have a globally
// unique name across all of Amazon S3. Buckets are created at the AWS account level and are used to store objects.
// Folder (Prefix): In Amazon S3, folders are not actual physical entities but are virtual constructs called prefixes.
// They are part of the object keys used to organize and group objects within a bucket. Object keys are essentially the
// full path or name of an object, including the folder structure. For example, if you have an object named
// "my-image.jpg" inside a folder named "photos," the object key would be "photos/my-image.jpg."
// Folders are helpful for organizing objects within a bucket and creating a hierarchical structure.
// In reality, S3 does not have a true folder structure. It stores objects in a flat structure with unique keys.
// However, the keys can include slashes ("/") to create a visual separation that resembles a folder structure.
// Various tools and S3 clients interpret the slashes as folder separators and display them accordingly.
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"
// ----------------------------------------------------------------------------------
// List the objects having prefix "images/"
// ----------------------------------------------------------------------------------
ls_StrXml = loo_Http.S3_ListBucketObjects("chilkat100?prefix=images/")
if loo_Http.LastMethodSuccess = 0 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 = 0 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 "----"
// Use this online tool to generate parsing code from sample XML:
// Generate Parsing Code from XML
// Iterate over the bucket items and get information for each..
li_NumItems = loo_Xml.NumChildrenHavingTag("Contents")
Write-Debug "Number of bucket items = " + string(li_NumItems)
loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.CkDateTime")
loo_DtObj = create oleobject
li_rc = loo_DtObj.ConnectToNewObject("Chilkat.DtObj")
i = 0
do while i < li_NumItems
loo_Xml.I = i
ls_ItemKey = loo_Xml.GetChildContent("Contents[i]|Key")
ls_ItemSizeDecimalStr = loo_Xml.GetChildContent("Contents[i]|Size")
ls_LastModTimestamp = loo_Xml.GetChildContent("Contents[i]|LastModified")
loo_Dt.SetFromRfc822(ls_LastModTimestamp)
// Get a local date/time.
li_BLocal = 1
loo_Dt.ToDtObj(li_BLocal,loo_DtObj)
Write-Debug string(i) + ": " + ls_ItemKey + ", " + ls_ItemSizeDecimalStr + ", " + string(loo_DtObj.Day) + "-" + string(loo_DtObj.Month) + "-" + string(loo_DtObj.Year) + ":" + string(loo_DtObj.Hour) + ":" + string(loo_DtObj.Minute)
i = i + 1
loop
// Sample output from the above loop:
// 0: images/africa/, 0, 11-6-2020:20:18
// 1: images/africa/giraffe.jpg, 262769, 11-6-2020:20:20
// 2: images/africa/lion.jpg, 1026769, 11-6-2020:20:20
// 3: images/sea_creatures/starfish123.jpg, 6229, 19-1-2017:10:45
// 4: images/sea_creatures/starfish���.jpg, 6229, 19-1-2017:12:7
destroy loo_Http
destroy loo_Xml
destroy loo_Dt
destroy loo_DtObj