Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) S3 List Objects in Bucket FolderSee more Amazon S3 ExamplesDemonstrates how to retrieve the XML listing of the objects (i.e. files) stored in an Amazon S3 bucket, but just within a particular folder.
#include <C_CkHttpW.h> #include <C_CkXmlW.h> #include <C_CkDateTimeW.h> #include <C_CkDtObjW.h> void ChilkatSample(void) { HCkHttpW http; const wchar_t *strXml; HCkXmlW xml; BOOL success; int numItems; const wchar_t *itemKey; const wchar_t *itemSizeDecimalStr; const wchar_t *lastModTimestamp; HCkDateTimeW dt; int i; BOOL bLocal; HCkDtObjW dtObj; // 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. http = CkHttpW_Create(); // Insert your access key here: CkHttpW_putAwsAccessKey(http,L"AWS_ACCESS_KEY"); // Insert your secret key here: CkHttpW_putAwsSecretKey(http,L"AWS_SECRET_KEY"); // ---------------------------------------------------------------------------------- // List the objects having prefix "images/" // ---------------------------------------------------------------------------------- strXml = CkHttpW_s3_ListBucketObjects(http,L"chilkat100?prefix=images/"); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); return; } wprintf(L"Response status code = %d\n",CkHttpW_getLastStatus(http)); xml = CkXmlW_Create(); success = CkXmlW_LoadXml(xml,strXml); if (success != TRUE) { wprintf(L"%s\n",CkXmlW_lastErrorText(xml)); CkHttpW_Dispose(http); CkXmlW_Dispose(xml); return; } // If the response status code was not 200, then the XML response is not a // listing of objects, but instead contains error information. if (CkHttpW_getLastStatus(http) != 200) { wprintf(L"%s\n",CkXmlW_getXml(xml)); wprintf(L"Failed.\n"); CkHttpW_Dispose(http); CkXmlW_Dispose(xml); return; } // A sample response is shown below. wprintf(L"%s\n",CkXmlW_getXml(xml)); wprintf(L"----\n"); // 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.. numItems = CkXmlW_NumChildrenHavingTag(xml,L"Contents"); wprintf(L"Number of bucket items = %d\n",numItems); dt = CkDateTimeW_Create(); i = 0; while (i < numItems) { CkXmlW_putI(xml,i); itemKey = CkXmlW_getChildContent(xml,L"Contents[i]|Key"); itemSizeDecimalStr = CkXmlW_getChildContent(xml,L"Contents[i]|Size"); lastModTimestamp = CkXmlW_getChildContent(xml,L"Contents[i]|LastModified"); CkDateTimeW_SetFromRfc822(dt,lastModTimestamp); // Get a local date/time. bLocal = TRUE; dtObj = CkDateTimeW_GetDtObj(dt,bLocal); wprintf(L"%d: %s, %s, %d-%d-%d:%d:%d\n",i,itemKey,itemSizeDecimalStr,CkDtObjW_getDay(dtObj),CkDtObjW_getMonth(dtObj),CkDtObjW_getYear(dtObj) ,CkDtObjW_getHour(dtObj),CkDtObjW_getMinute(dtObj)); CkDtObjW_Dispose(dtObj); i = i + 1; } // 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 CkHttpW_Dispose(http); CkXmlW_Dispose(xml); CkDateTimeW_Dispose(dt); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.