Sample code for 30+ languages & platforms
Unicode C

OSS List Bucket Objects (Alibaba Cloud)

See more Alibaba Cloud OSS Examples

Demonstrates how to list the objects in a bucket.

The Chilkat S3 functions in the HTTP class are compatible with Alibaba Cloud's OSS service.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkXmlW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    const wchar_t *bucketName;
    const wchar_t *strXml;
    HCkXmlW xml;
    const wchar_t *Key;
    const wchar_t *LastModified;
    const wchar_t *ETag;
    const wchar_t *SizeDecimalStr;
    const wchar_t *StorageClass;
    const wchar_t *ID;
    const wchar_t *DisplayName;
    const wchar_t *ListBucketResult_xmlns;
    const wchar_t *Name;
    int MaxKeys;
    const wchar_t *IsTruncated;
    int i;
    int count_i;

    success = FALSE;

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

    http = CkHttpW_Create();

    // Insert your AccessKey ID here:
    CkHttpW_putAwsAccessKey(http,L"access-key");

    // Insert your AccessKey Secret here:
    CkHttpW_putAwsSecretKey(http,L"secret-key");

    // To list objects in a bucket located in a different region, use the endpoint for that region, such as "oss-cn-hangzhou.aliyuncs.com 	"
    // See Alibaba Object Storage Service Regions and Endpoints
    CkHttpW_putAwsEndpoint(http,L"oss-us-east-1.aliyuncs.com");

    bucketName = L"chilkat";

    strXml = CkHttpW_s3_ListBucketObjects(http,bucketName);
    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

    // <?xml version="1.0" encoding="UTF-8"?>
    // <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    //     <Name>chilkat</Name>
    //     <Prefix/>
    //     <Marker/>
    //     <MaxKeys>1000</MaxKeys>
    //     <IsTruncated>false</IsTruncated>
    //     <Contents>
    //         <Key>orchard.json</Key>
    //         <LastModified>2021-10-29T16:58:12.000Z</LastModified>
    //         <ETag>"303FB46CF341094FEF6274B7789CD6AA"</ETag>
    //         <Size>22</Size>
    //         <StorageClass>STANDARD</StorageClass>
    //         <Owner>
    //             <ID>5035535379748121</ID>
    //             <DisplayName>5035535379748121</DisplayName>
    //         </Owner>
    //     </Contents>
    //     <Contents>
    //         <Key>seahorse.jpg</Key>
    //         <LastModified>2021-10-29T16:52:01.000Z</LastModified>
    //         <ETag>"A8551F0A5437F43A796FCA7623EE9232"</ETag>
    //         <Size>24388</Size>
    //         <StorageClass>STANDARD</StorageClass>
    //         <Owner>
    //             <ID>5035535379748121</ID>
    //             <DisplayName>5035535379748121</DisplayName>
    //         </Owner>
    // 
    //     </Contents>
    // </ListBucketResult>

    ListBucketResult_xmlns = CkXmlW_getAttrValue(xml,L"xmlns");
    Name = CkXmlW_getChildContent(xml,L"Name");
    MaxKeys = CkXmlW_GetChildIntValue(xml,L"MaxKeys");
    IsTruncated = CkXmlW_getChildContent(xml,L"IsTruncated");
    i = 0;
    count_i = CkXmlW_NumChildrenHavingTag(xml,L"Contents");
    while (i < count_i) {
        CkXmlW_putI(xml,i);
        Key = CkXmlW_getChildContent(xml,L"Contents[i]|Key");
        LastModified = CkXmlW_getChildContent(xml,L"Contents[i]|LastModified");
        ETag = CkXmlW_getChildContent(xml,L"Contents[i]|ETag");
        SizeDecimalStr = CkXmlW_getChildContent(xml,L"Contents[i]|Size");
        StorageClass = CkXmlW_getChildContent(xml,L"Contents[i]|StorageClass");
        ID = CkXmlW_getChildContent(xml,L"Contents[i]|Owner|ID");
        DisplayName = CkXmlW_getChildContent(xml,L"Contents[i]|Owner|DisplayName");
        i = i + 1;
    }



    CkHttpW_Dispose(http);
    CkXmlW_Dispose(xml);

    }