This topic describes how to list all objects in a bucket, a specified number of objects, and the sizes of objects in a specified directory.

Objects are listed alphabetically. You can use OssClient.ListObjects to list the objects in a bucket. ListObjects supports the following types of parameter formats:
  • ListObjectOutcome ListObjects(const std::string& bucket) const: lists objects in a bucket. A maximum of 1,000 objects can be listed.
  • ListObjectOutcome ListObjects(const std::string& bucket, const std::string& prefix) const: lists objects whose names contain the specified prefix in a bucket. A maximum of 1,000 objects can be listed.
  • ListObjectOutcome ListObjects(const ListObjectsRequest& request) const: provides multiple filter conditions to query objects.
The following table describes the parameters for object listing.
Operation Description
delimiter The character that is used to group objects by name. Objects whose names contain the same string from the prefix and the next occurrence of the delimiter are grouped as a single result element in commonPrefixes.
prefix The prefix that the names of returned objects must contain.
maxKeys The maximum number of objects that can be listed at a time. The default value is 100, and the maximum value is 1000.
marker The position from which the list operation starts. All objects are returned in lexicographic order starting from this position.

Simple list

The following code provides an example on how to list objects in a specified bucket. By default, a maximum of 100 objects can be listed.

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the information about the account that is used to access OSS. */
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize resources such as networks. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /* List objects. */
    ListObjectsRequest request(BucketName);
    auto outcome = client.ListObjects(request);

    if (!outcome.isSuccess())  {    
        /* Handle exceptions. */
        std::cout << "ListObjects fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;  
    }
    else {
        for (const auto& object : outcome.result().ObjectSummarys()) {
            std::cout << "object"<<
            ",name:" << object.Key() <<
            ",size:" << object.Size() <<
            ",lastmodify time:" << object.LastModified() << std:: endl;
         }      
    }

    /* Release resources such as networks. */
    ShutdownSdk();
    return 0;
}

List a specified number of objects

The following code provides an example on how to list a specified number of objects:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the information about the account that is used to access OSS. */
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

     /* Initialize resources such as networks. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf) ;

    /* List objects. */
    ListObjectsRequest request(BucketName);
    /* Set the maximum number of returned objects to 200. */
    request.setMaxKeys(200);
    auto outcome = client.ListObjects(request);

    if (!outcome.isSuccess( )) {    
        /* Handle exceptions. */
        std::cout << "ListObjects fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;  
    }
    else {
        for (const auto& object : outcome.result().ObjectSummarys()) {
            std::cout << "object"<<
            ",name:" << object.Key() <<
            ",size:" << object.Size() <<
            ",lastmodify time:" << object.LastModified() << std ::endl; 
        }      
    }

    /* Release resources such as networks. */
    ShutdownSdk();
    return 0;
}

List objects and subdirectories in a specified directory

The following code provides an example on how to list objects and subdirectories within a directory in a bucket:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the information about the account that is used to access OSS. */
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the prefix that the objects to return must contain in their names. 
    std::string keyPrefix = "yourkeyPrefix ";

     /* Initialize resources such as networks. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    std::string nextMarker = "";
    bool isTruncated = false;  
    do {
        /* List objects. */
        ListObjectsRequest request(BucketName);
         /* Set the delimiter to a forward slash (/). */
        request.setDelimiter("/");
        request.setPrefix(keyPrefix);
        request.setMarker(nextMarker);
        auto outcome = client.ListObjects(request);

        if (!outcome.isSuccess ()) {    
            /* Handle exceptions. */
            std::cout << "ListObjects fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            break;
        }  
        for (const auto& object : outcome.result().ObjectSummarys()) {
            std::cout << "object"<<
            ",name:" << object.Key() <<
            ",size:" << object.Size() <<
            ",lastmodify time:" << object.LastModified() << std::endl;
        }
        for (const auto& commonPrefix : outcome.result().CommonPrefixes()) {
            std::cout << "commonPrefix" << ",name:" << commonPrefix << std::endl;
        }
        nextMarker = outcome.result().NextMarker();
        isTruncated = outcome.result().IsTruncated();
    } while (isTruncated);

    /* Release resources such as networks. */
    ShutdownSdk();
    return 0;
}

List sizes of objects in a specified directory

The following code provides an example on how to query the sizes of objects in a specified directory:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    static int64_t calculateFolderLength (const OssClient &client, const std::string &bucketName, const std::string &folder)
    {
        std::string nextMarker = "";
        bool isTruncated = false; 
        int64_t size = 0;
        do {
            /* List objects. */
            ListObjectsRequest request(bucketName);
            request.setPrefix(folder);
            request.setMarker(nextMarker);
            auto outcome = client.ListObjects(request);

            if (!outcome.isSuccess()) {    
                /* Handle exceptions. */
                std::cout << "ListObjects fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                break;
            }  

            for (const auto& object : outcome.result().ObjectSummarys()) {
                size += object.Key() 
            }

            nextMarker = outcome.result().NextMarker();
            isTruncated = outcome.result().IsTruncated();
      } while (isTruncated);

      return size;
    }

    /* Initialize the information about the account that is used to access OSS. */
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";
    /* Specify the prefix that the objects to return must contain in their names. 
    std::string keyPrefix = "yourkeyPrefix ";

    /* Initialize resources such as networks. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);


    std::string nextMarker = "";
    bool isTruncated = false; 
    do {
        /* List objects. */
        ListObjectsRequest request(BucketName);
        /* Set the delimiter to forward slashes (/). */
        request.setDelimiter("/");
        request.setPrefix(keyPrefix);
        request.setMarker(nextMarker);
        auto outcome = client.ListObjects(request);

        if (!outcome.isSuccess()) {    
            /* Handle exceptions. */
            std::cout << "ListObjects fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            break;
        }  

        for (const auto& object : outcome.result().ObjectSummarys()) {
            std::cout << "object" <<
            ",name:" << object.Key() <<
            ",size:" << object.Size() << std::endl;
        }

        for (const auto& commonPrefix : outcome.result().CommonPrefixes()) {
            int64_t foldersize = calculateFolderLength(client, BucketName, commonPrefix);
            std::cout << "folder" <<
            ",name:" << commonPrefix <<
            ",size:" << foldersize << std::endl;
        }
        nextMarker = outcome.result().NextMarker();
        isTruncated = outcome.result().IsTruncated(); 
    } while (isTruncated);

    /* Release resources such as networks. */
    ShutdownSdk();
    return 0;
}

List objects whose names contain a specified prefix

The following code provides an example on how to list objects whose names contain a specified prefix:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* Initialize the information about the account that is used to access OSS. */
     /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
     std::string AccessKeyId = "yourAccessKeyId";
     std::string AccessKeySecret = "yourAccessKeySecret";
     /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
     std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
     /* Specify the name of the bucket. Example: examplebucket. */
     std::string BucketName = "examplebucket";
     /* Specify the prefix that the objects to return must contain in their names. 
     std::string keyPrefix = "yourkeyPrefix ";

      /* Initialize resources such as networks. */
      InitializeSdk();

      ClientConfiguration conf;
      OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
      std::string nextMarker = "";
      bool isTruncated = false; 
      do {
              /* List objects. */
              ListObjectsRequest request(BucketName);              
              request.setPrefix(keyPrefix);
              request.setMarker(nextMarker);
              auto outcome = client.ListObjects(request);

              if (!outcome.isSuccess()) {    
                    /* Handle exceptions. */
                    std::cout << "ListObjects fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                        break;
              }
            for (const auto& object : outcome.result().ObjectSummarys()) {
                  std::cout << "object"<<
              ",name:" << object.Key() <<
              ",size:" << object.Size() <<
              ",lastmodify time:" << object.LastModified() << std::endl;
            }      
              nextMarker = outcome.result().NextMarker();
              isTruncated = outcome.result().IsTruncated();
      } while (isTruncated);

      /* Release resources such as networks. */
      ShutdownSdk();
      return 0;
}

List objects whose names are alphabetically after the object specified by marker

You can configure the marker parameter to specify the name of the object after which the list operation starts. The following code provides an example on how to list objects after the object specified by Marker:

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
      /* Initialize the information about the account that is used to access OSS. */
      /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
      std::string AccessKeyId = "yourAccessKeyId";
      std::string AccessKeySecret = "yourAccessKeySecret";
      /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
      std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
      /* Specify the name of the bucket. Example: examplebucket. */
      std::string BucketName = "examplebucket";
      /* Specify marker as the name of the object after which the list begins. 
      std::string YourMarker = "yourMarker";

      /* Initialize resources such as networks. */
      InitializeSdk();

      ClientConfiguration conf;
      OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
      
    ListObjectOutcome outcome;
      do {
              /* List the objects. */
              ListObjectsRequest request(BucketName);
              /* Specify the name of the object after which the list begins. */
              request.setMarker(YourMarker);
              outcome = client.ListObjects(request);

              if (!outcome.isSuccess()) {    
                    /* Handle exceptions. */
                    std::cout << "ListObjects fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                    break;  
              }
            YourMarker = outcome.result().NextMarker();
            for (const auto& object : outcome.result().ObjectSummarys()) {
                       std::cout << "object"<<
                  ",name:" << object.Key() <<
                ",size:" << object.Size() <<
                ",lastmodify time:" << object.LastModified() << std::endl;
            }      
      } while (outcome.result().IsTruncated());

      /* Release network resources. */
      ShutdownSdk();
      return 0;
}

List objects and specify the encoding type of the object names

An object name that contains one of the following special characters needs to be encoded before the object is transmitted. Currently, OSS supports only URL encoding.

  • Single quotation marks (')
  • Double quotation marks ("")
  • Ampersands (&)
  • Angle brackets (<>)
  • Pause markers
  • Chinese characters
The following code provides an example on how to encode a specified object name:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /* Initialize the information about the account that is used to access OSS. */
    /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* Specify the name of the bucket. Example: examplebucket. */
    std::string BucketName = "examplebucket";

    /* Initialize network resources. */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    std::string nextMarker = "";
    bool isTruncated = false; 
    do {
        /* List objects. */
        ListObjectsRequest request(BucketName) ;
        /* Specify the encoding type of the object names. */
        request.setEncodingType("url");
        request.setMarker(nextMarker);
        auto outcome = client.ListObjects(request);

        if (!outcome.isSuccess() ) {    
            /* Handle exceptions. */
            std::cout << "ListObjects fail" <<
            ",code:" << outcome.error().Code() <<
            ",message:" << outcome.error().Message() <<
            ",requestId:" << outcome.error().RequestId() << std::endl;
            break; 
        }
        for (const auto& object : outcome.result().ObjectSummarys()) {
            std::cout << "object"<<
            ",name:" << object.Key() <<
            ",size:" << object.Size() <<
            ",lastmodify time:" << object.LastModified() << std::endl;
        }      
        nextMarker = outcome.result().NextMarker();
        isTruncated = outcome.result().IsTruncated();
    } while (isTruncated);

    /* Release resources such as networks. */
    ShutdownSdk();
    return 0;
}

List objects in a directory

OSS uses a flat structure for objects instead of a hierarchical structure to store objects. A directory is an object whose size is 0 and whose name ends with a forward slash (/). You can upload and download this object. By default, objects whose names end with a forward slash (/) are displayed as directories in the OSS console.

You can specify the Delimiter and Prefix parameters to list objects by directory.

  • If you set Prefix to a directory name in the request, objects and subdirectories whose names contain the prefix are listed.
  • If you also set Delimiter to a forward slash (/) in the request, the objects and subdirectories whose names start with the specified prefix in the directory are listed. Each subdirectory is listed as a single result element in CommonPrefixes. The objects and directories in these subdirectories are not listed.

For more information about directories, see List objects. For more information about the complete code used to create a directory, visit GitHub.

Example: The following four objects are stored in a bucket: oss.jpg, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The forward slash (/) is specified as the directory delimiter. The following code provides examples on how to list objects in simulated directories.

  • List all objects in a bucket

    The following code provides an example on how to list all objects in a bucket:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the information about the account that is used to access OSS. */
        /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* Specify the name of the bucket. Example: examplebucket. */
        std::string BucketName = "examplebucket";
    
        /* Initialize resources such as networks. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false; 
        do {
            /* List objects. */
            ListObjectsRequest request(BucketName);
            request.setMarker(nextMarker);
            auto outcome = client.ListObjects(request);
    
            if (!outcome.isSuccess()) {    
                /* Handle exceptions. */
                std::cout << "ListObjects fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                ShutdownSdk();
                return -1;  
            }
            else {
                for (const auto& object : outcome.result().ObjectSummarys()) {
                    std::cout << "object"<<
                    ",name:" << object.Key() <<
                    ",size:" << object.Size() <<
                    ",lastmodify time:" << object.LastModified() << std::endl;
                }      
            }
            nextMarker = outcome.result().NextMarker();
            isTruncated = outcome.result().IsTruncated();
        } while (isTruncated);
    
        /* Release resources such as networks. */
        ShutdownSdk();
        return 0;
    }

    The following result is returned:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    oss.jpg
    
    CommonPrefixes:
  • List all objects in the specified directory

    The following code provides an example on how to list all objects in the specified directory:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the information about the account that is used to access OSS. */
        /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* Specify the name of the bucket. Example: examplebucket. */
        std::string BucketName = "examplebucket";
    
        /* Initialize resources such as networks. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false; 
        do {
                /* List objects. */
                ListObjectsRequest request(BucketName);
                request.setPrefix("fun/");
                request.setMarker(nextMarker);
                auto outcome = client.ListObjects(request);
    
                if (!outcome.isSuccess()) {    
                    /* Handle exceptions. */
                    std::cout << "ListObjects fail" <<
                    ",code:" << outcome.error().Code() <<
                    ",message:" << outcome.error().Message() <<
                    ",requestId:" << outcome.error().RequestId() << std::endl;
                    break;
                }  
                for (const auto& object : outcome.result().ObjectSummarys()) {
                    std::cout << "object"<<
                    ",name:" << object.Key() <<
                    ",size:" << object.Size() <<
                    ",lastmodify time:" << object.LastModified() << std::endl;
                }
                for (const auto& commonPrefix : outcome.result().CommonPrefixes()) {
                    std::cout << "commonPrefix" << ",name:" << commonPrefix << std::endl;
                }
                nextMarker = outcome.result().NextMarker();
                isTruncated = outcome.result().IsTruncated();
        } while (isTruncated);
    
        /* Release resources such as networks. */
        ShutdownSdk();
        return 0;
    }

    The following result is returned:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    
    CommonPrefixes:
  • List objects and subdirectories in a specified directory

    The following code provides an example on how to list objects and subdirectories in a specified directory:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* Initialize the information about the account that is used to access OSS. */
        /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* Specify the name of the bucket. Example: examplebucket. */
        std::string BucketName = "examplebucket";
    
        /* Initialize resources such as networks. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false;
        do {
                /* List objects. */
                ListObjectsRequest request(BucketName);
                /* Set the delimiter to a forward slash (/). */
                request.setDelimiter("/");
                request.setPrefix("fun/");
                request.setMarker(nextMarker);
                auto outcome = client.ListObjects(request);
    
                if (!outcome.isSuccess()) {    
                    /* Handle exceptions. */
                    std::cout << "ListObjects fail" <<
                    ",code:" << outcome.error().Code() <<
                    ",message:" << outcome.error().Message() <<
                    ",requestId:" << outcome.error().RequestId() << std::endl;
                    break;
                }  
                for (const auto& object : outcome.result().ObjectSummarys()) {
                    std::cout << "object"<<
                    ",name:" << object.Key() <<
                    ",size:" << object.Size() <<
                    ",lastmodify time:" << object.LastModified() << std::endl;
                }
                for (const auto& commonPrefix : outcome.result().CommonPrefixes()) {
                    std::cout << "commonPrefix" << ",name:" << commonPrefix << std::endl;
                }
                nextMarker = outcome.result().NextMarker();
                isTruncated = outcome.result().IsTruncated();
        } while (isTruncated);
    
        /* Release resources such as networks. */
        ShutdownSdk();
        return 0;
    }

    The following result is returned:

    Objects:
    fun/test.jpg
    
    CommonPrefixes:
    fun/movie/
  • List sizes of objects in a specified directory

    The following code provides an example on how to query the sizes of objects in a specified directory:

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    static int64_t calculateFolderLength (const OssClient &client, const std::string &bucketName, const std::string &folder)
    {
        std::string nextMarker = "";
        bool isTruncated = false;
        int64_t size = 0;
        do {
                /* List objects. */
                ListObjectsRequest request(bucketName);
                request.setPrefix(folder);
                request.setMarker(nextMarker);
                auto outcome = client.ListObjects(request);
    
                if (!outcome.isSuccess()) {    
                    /* Handle exceptions. */
                    std::cout << "ListObjects fail" <<
                    ",code:" << outcome.error().Code() <<
                    ",message:" << outcome.error().Message() <<
                    ",requestId:" << outcome.error().RequestId() << std::endl;
                    break;
                }  
    
                for (const auto& object : outcome.result().ObjectSummarys()) {
                    size += object.Size();
                }
    
                nextMarker = outcome.result().NextMarker();
                isTruncated = outcome.result().IsTruncated();
            } while (isTruncated);
    
          return size;
    }
    
    int main(void)
    {   
        /* Initialize the information about the account that is used to access OSS. */
        /* Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to access OSS because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* Specify the name of the bucket. Example: examplebucket. */
        std::string BucketName = "examplebucket";
    
        /* Initialize network resources. */
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false;
        do {
            /* List objects. */
            ListObjectsRequest request(BucketName);
            /* Set the delimiter to forward slashes (/). */
            request.setDelimiter("/");
            request.setPrefix("fun/");
            request.setMarker(nextMarker);
            auto outcome = client.ListObjects(request);
    
            if (!outcome.isSuccess()) {    
                /* Handle exceptions. */
                std::cout << "ListObjects fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                break;
            }  
    
            for (const auto& object : outcome.result().ObjectSummarys()) {
                std::cout << "object" <<
                ",name:" << object.Key() <<
                ",size:" << object.Size() << std::endl;
            }
    
            for (const auto& commonPrefix : outcome.result().CommonPrefixes()) {
                int64_t foldersize = calculateFolderLength(client, BucketName, commonPrefix);
                std::cout << "folder" <<
                ",name:" << commonPrefix <<
                ",size:" << foldersize << std::endl;
            }
            nextMarker = outcome.result().NextMarker();
            isTruncated = outcome.result().IsTruncated();
        } while (isTruncated);
    
        /* Release resources such as networks. */
        ShutdownSdk();
        return 0;
    }