DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
String sStrXml
Handle hoXml
Integer iNumItems
String sItemKey
String sItemSizeDecimalStr
String sLastModTimestamp
Handle hoDt
Obj Handle hoDtObj
Integer i
Boolean iBLocal
String sTemp1
Integer iTemp1
Integer iTemp2
Integer iTemp3
Integer iTemp4
Integer iTemp5
Boolean bTemp1
Move False To iSuccess
// 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.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// Insert your access key here:
Set ComAwsAccessKey Of hoHttp To "AWS_ACCESS_KEY"
// Insert your secret key here:
Set ComAwsSecretKey Of hoHttp To "AWS_SECRET_KEY"
// ----------------------------------------------------------------------------------
// List the objects having prefix "images/"
// ----------------------------------------------------------------------------------
Get ComS3_ListBucketObjects Of hoHttp "chilkat100?prefix=images/" To sStrXml
Get ComLastMethodSuccess Of hoHttp To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLastStatus Of hoHttp To iTemp1
Showln "Response status code = " iTemp1
Get Create (RefClass(cComChilkatXml)) To hoXml
If (Not(IsComObjectCreated(hoXml))) Begin
Send CreateComObject of hoXml
End
Get ComLoadXml Of hoXml sStrXml To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoXml To sTemp1
Showln sTemp1
Procedure_Return
End
// If the response status code was not 200, then the XML response is not a
// listing of objects, but instead contains error information.
Get ComLastStatus Of hoHttp To iTemp1
If (iTemp1 <> 200) Begin
Get ComGetXml Of hoXml To sTemp1
Showln sTemp1
Showln "Failed."
Procedure_Return
End
// A sample response is shown below.
Get ComGetXml Of hoXml To sTemp1
Showln sTemp1
Showln "----"
// 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..
Get ComNumChildrenHavingTag Of hoXml "Contents" To iNumItems
Showln "Number of bucket items = " iNumItems
Get Create (RefClass(cComCkDateTime)) To hoDt
If (Not(IsComObjectCreated(hoDt))) Begin
Send CreateComObject of hoDt
End
Get Create (RefClass(cComChilkatDtObj)) To hoDtObj
If (Not(IsComObjectCreated(hoDtObj))) Begin
Send CreateComObject of hoDtObj
End
Move 0 To i
While (i < iNumItems)
Set ComI Of hoXml To i
Get ComGetChildContent Of hoXml "Contents[i]|Key" To sItemKey
Get ComGetChildContent Of hoXml "Contents[i]|Size" To sItemSizeDecimalStr
Get ComGetChildContent Of hoXml "Contents[i]|LastModified" To sLastModTimestamp
Get ComSetFromRfc822 Of hoDt sLastModTimestamp To iSuccess
// Get a local date/time.
Move True To iBLocal
Get pvComObject of hoDtObj to vDtObj
Send ComToDtObj To hoDt iBLocal vDtObj
Get ComDay Of hoDtObj To iTemp1
Get ComMonth Of hoDtObj To iTemp2
Get ComYear Of hoDtObj To iTemp3
Get ComHour Of hoDtObj To iTemp4
Get ComMinute Of hoDtObj To iTemp5
Showln i ": " sItemKey ", " sItemSizeDecimalStr ", " iTemp1 "-" iTemp2 "-" iTemp3 ":" iTemp4 ":" iTemp5
Move (i + 1) To i
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
End_Procedure