全部產品
Search
文件中心

Object Storage Service:C++列舉檔案

更新時間:Jun 19, 2024

本文介紹如何列舉指定儲存空間(Bucket)的所有檔案、指定個數的檔案以及指定目錄下的檔案大小等。

背景資訊

OSS 檔案按照字母順序排列。您可以通過OssClient.ListObjects列出儲存空間下的檔案。ListObjects有以下三類參數格式:

  • ListObjectOutcome ListObjects(const std::string& bucket) const:列舉儲存空間下的檔案。最多列舉1000個檔案。

  • ListObjectOutcome ListObjects(const std::string& bucket, const std::string& prefix) const: 列舉儲存空間下指定首碼的檔案。最多列舉 1000 個檔案。

  • ListObjectOutcome ListObjects(const ListObjectsRequest& request) const:提供多種過濾功能,實現靈活的查詢功能。

主要參數及說明如下:

參數

描述

delimiter

對檔案名稱進行分組的一個字元。所有名稱包含指定的首碼且第一次出現delimiter字元之間的檔案作為一組(commonPrefixes)。

prefix

限定返回的檔案必須以prefix作為首碼。

maxKeys

限定此次列舉檔案的最大個數。預設值為100,最大值為1000。

marker

列舉指定marker之後的檔案。

注意事項

  • 本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見訪問網域名稱和資料中心

  • 本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見建立OssClient

  • 要列舉檔案,您必須有oss:ListObjects許可權。具體操作,請參見為RAM使用者授權自訂的權限原則

簡單列舉檔案

以下代碼用於列舉指定儲存空間下的檔案,預設列舉100個檔案。

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

int main(void)
{
    /* 初始化OSS帳號資訊。*/
            
    /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* 填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";

    /* 初始化網路等資源。*/
    InitializeSdk();

    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    /* 列舉檔案。*/
    ListObjectsRequest request(BucketName);
    auto outcome = client.ListObjects(request);

    if (!outcome.isSuccess())  {    
        /* 異常處理。*/
        std::cout << "ListObjects fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        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;
         }      
    }

    /* 釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

列舉指定個數的檔案

以下代碼用於列舉指定個數的檔案。

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

int main(void)
{
    /* 初始化OSS帳號資訊。*/
    
    /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* 填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";

     /* 初始化網路等資源。*/
    InitializeSdk();

    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    /* 列舉檔案。*/
    ListObjectsRequest request(BucketName);
    /* 設定列舉檔案的最大個數為200。*/
    request.setMaxKeys(200);
    auto outcome = client.ListObjects(request);

    if (!outcome.isSuccess( )) {    
        /* 異常處理。*/
        std::cout << "ListObjects fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        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; 
        }      
    }

    /* 釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

列舉指定目錄下的檔案和子目錄

以下代碼用於列舉指定目錄下的檔案和子目錄:

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

int main(void)
{
    /* 初始化OSS帳號資訊。*/
            
    /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* 填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填寫列舉的檔案首碼(Prefix)。*/
    std::string keyPrefix = "yourkeyPrefix";

     /* 初始化網路等資源。*/
    InitializeSdk();

    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    std::string nextMarker = "";
    bool isTruncated = false;  
    do {
        /* 列舉檔案。*/
        ListObjectsRequest request(BucketName);
         /* 設定正斜線(/)為檔案夾的分隔字元 */
        request.setDelimiter("/");
        request.setPrefix(keyPrefix);
        request.setMarker(nextMarker);
        auto outcome = client.ListObjects(request);

        if (!outcome.isSuccess ()) {    
            /* 異常處理。*/
            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);

    /* 釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

列舉指定目錄下的檔案大小

以下代碼用於擷取指定目錄下的檔案大小:

#include <iostream>
#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 {
        /* 列舉檔案。*/
        ListObjectsRequest request(bucketName);
        request.setPrefix(folder);
        request.setMarker(nextMarker);
        auto outcome = client.ListObjects(request);
        if (!outcome.isSuccess()) {    
            /* 異常處理 */
            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)
{
    /* 初始化OSS帳號資訊。*/
            
    /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* 填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填寫列舉的檔案首碼(Prefix)。*/
    std::string keyPrefix = "yourkeyPrefix";
	
    /* 初始化網路等資源。*/
    InitializeSdk();
    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    std::string nextMarker = "";
    bool isTruncated = false; 
    do {
        /* 列舉檔案。*/
        ListObjectsRequest request(BucketName);
        /* 設定正斜線(/)為檔案夾的分隔字元。*/
        request.setDelimiter("/");
        request.setPrefix(keyPrefix);
        request.setMarker(nextMarker);
        auto outcome = client.ListObjects(request);
        if (!outcome.isSuccess()) {    
            /* 異常處理。*/
            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);
    /* 釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

列舉指定首碼的檔案

以下代碼用於列舉包含指定首碼(prefix)的檔案。

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

int main(void)
{
     /* 初始化OSS帳號資訊。*/
               
     /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
     std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
     /* 填寫Bucket名稱,例如examplebucket。*/
     std::string BucketName = "examplebucket";
     /* 填寫列舉的檔案首碼(Prefix)。*/
     std::string keyPrefix = "yourkeyPrefix";

      /* 初始化網路等資源。*/
      InitializeSdk();

      ClientConfiguration conf;
      /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
  
      std::string nextMarker = "";
      bool isTruncated = false; 
      do {
              /* 列舉檔案。*/
              ListObjectsRequest request(BucketName);              
              request.setPrefix(keyPrefix);
              request.setMarker(nextMarker);
              auto outcome = client.ListObjects(request);

              if (!outcome.isSuccess()) {    
                    /* 異常處理。*/
                    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);

      /* 釋放網路等資源。*/
      ShutdownSdk();
      return 0;
}

列舉指定marker之後的檔案

參數marker代表檔案名稱。以下代碼用於列舉指定marker之後的檔案。

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

int main(void)
{
      /* 初始化OSS帳號資訊。*/
                  
      /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
      std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
      /* 填寫Bucket名稱,例如examplebucket。*/
      std::string BucketName = "examplebucket";
      /* 設定marker。*/
      std::string YourMarker = "yourMarker";

      /* 初始化網路等資源。*/
      InitializeSdk();

      ClientConfiguration conf;
      /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
      
    ListObjectOutcome outcome;
      do {
              /* 列舉檔案 */
              ListObjectsRequest request(BucketName);
              /* 列舉指定marker之後的檔案 */
              request.setMarker(YourMarker);
              outcome = client.ListObjects(request);

              if (!outcome.isSuccess()) {    
                    /* 異常處理 */
                    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());

      /* 釋放網路等資源 */
      ShutdownSdk();
      return 0;
}

指定檔案名稱編碼

如果檔案名稱含有以下特殊字元,需要進行編碼傳輸。OSS目前僅支援URL編碼。

  • 單引號('')

  • 雙引號("")

  • and符號(&)

  • 角括弧(< >)

  • 中文

以下代碼用於指定檔案名稱編碼:

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

int main(void)
{
    /* 初始化OSS帳號資訊。*/
            
    /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    /* 填寫Bucket名稱,例如examplebucket。*/
    std::string BucketName = "examplebucket";

    /* 初始化網路等資源 */
    InitializeSdk();

    ClientConfiguration conf;
    /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);

    std::string nextMarker = "";
    bool isTruncated = false; 
    do {
        /* 列舉檔案。*/
        ListObjectsRequest request(BucketName) ;
        /* 指定檔案名稱編碼。*/
        request.setEncodingType("url");
        request.setMarker(nextMarker);
        auto outcome = client.ListObjects(request);

        if (!outcome.isSuccess() ) {    
            /* 異常處理。*/
            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);

    /* 釋放網路等資源。*/
    ShutdownSdk();
    return 0;
}

檔案夾功能

OSS沒有檔案夾的概念,所有元素都是以檔案來儲存。建立檔案夾本質上來說是建立了一個大小為0並以正斜線(/)結尾的檔案。這個檔案可以被上傳和下載,控制台會對以正斜線(/)結尾的檔案以檔案夾的方式展示。

通過delimiter和prefix兩個參數可以類比檔案夾功能:

  • 如果設定prefix為某個檔案夾名稱,則會列舉以此prefix開頭的檔案,即該檔案夾下所有的檔案和子檔案夾(目錄)均顯示為objects。

  • 如果在設定了prefix的情況下,將delimiter設定為正斜線(/),則只列舉該檔案夾下的檔案和子檔案夾(目錄),該檔案夾下的子檔案夾(目錄)顯示為CommonPrefixes,子檔案夾下的檔案和檔案夾不顯示。

假設儲存空間中包含檔案oss.jpgfun/test.jpgfun/movie/001.avifun/movie/007.avi,以正斜線(/)作為檔案夾的分隔字元。以下樣本說明了如何通過類比檔案夾的方式列舉檔案。

  • 列舉儲存空間下所有檔案

    以下代碼用於列舉指定儲存空間下的所有檔案。

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS帳號資訊。*/
                
        /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* 填寫Bucket名稱,例如examplebucket。*/
        std::string BucketName = "examplebucket";
    
        /* 初始化網路等資源。*/
        InitializeSdk();
    
        ClientConfiguration conf;
        /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
        auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
        OssClient client(Endpoint, credentialsProvider, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false; 
        do {
            /* 列舉檔案。*/
            ListObjectsRequest request(BucketName);
            request.setMarker(nextMarker);
            auto outcome = client.ListObjects(request);
    
            if (!outcome.isSuccess()) {    
                /* 異常處理。*/
                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);
    
        /* 釋放網路等資源。*/
        ShutdownSdk();
        return 0;
    }

    返回結果如下:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    oss.jpg
    
    CommonPrefixes:
  • 列舉指定目錄下所有檔案

    以下代碼用於列舉指定目錄下的所有檔案。

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS帳號資訊。*/
                
        /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* 填寫Bucket名稱,例如examplebucket。*/
        std::string BucketName = "examplebucket";
    
        /* 初始化網路等資源。*/
        InitializeSdk();
    
        ClientConfiguration conf;
        /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
        auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
        OssClient client(Endpoint, credentialsProvider, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false; 
        do {
                /* 列舉檔案。*/
                ListObjectsRequest request(BucketName);
                request.setPrefix("fun/");
                request.setMarker(nextMarker);
                auto outcome = client.ListObjects(request);
    
                if (!outcome.isSuccess()) {    
                    /* 異常處理。*/
                    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);
    
        /* 釋放網路等資源。*/
        ShutdownSdk();
        return 0;
    }

    返回結果如下:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    
    CommonPrefixes:
  • 列舉目錄下的檔案和子目錄

    以下代碼用於列舉指定目錄下的檔案和子目錄。

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /* 初始化OSS帳號資訊。*/
                
        /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* 填寫Bucket名稱,例如examplebucket。*/
        std::string BucketName = "examplebucket";
    
        /* 初始化網路等資源。*/
        InitializeSdk();
    
        ClientConfiguration conf;
        /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
        auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
        OssClient client(Endpoint, credentialsProvider, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false;
        do {
                /* 列舉檔案。*/
                ListObjectsRequest request(BucketName);
                /* 設定正斜線(/)為檔案夾的分隔字元 */
                request.setDelimiter("/");
                request.setPrefix("fun/");
                request.setMarker(nextMarker);
                auto outcome = client.ListObjects(request);
    
                if (!outcome.isSuccess()) {    
                    /* 異常處理。*/
                    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);
    
        /* 釋放網路等資源。*/
        ShutdownSdk();
        return 0;
    }

    返回結果如下:

    Objects:
    fun/test.jpg
    
    CommonPrefixes:
    fun/movie/
  • 列舉指定目錄下的檔案大小

    以下代碼用於擷取指定目錄下的檔案大小:

    #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 {
                /* 列舉檔案。*/
                ListObjectsRequest request(bucketName);
                request.setPrefix(folder);
                request.setMarker(nextMarker);
                auto outcome = client.ListObjects(request);
    
                if (!outcome.isSuccess()) {    
                    /* 異常處理。*/
                    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)
    {   
        /* 初始化OSS帳號資訊。*/
                
        /* 填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
        std::string Endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        /* 填寫Bucket名稱,例如examplebucket。*/
        std::string BucketName = "examplebucket";
    
        /* 初始化網路等資源 */
        InitializeSdk();
    
        ClientConfiguration conf;
        /* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
        auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
        OssClient client(Endpoint, credentialsProvider, conf);
    
        std::string nextMarker = "";
        bool isTruncated = false;
        do {
            /* 列舉檔案。*/
            ListObjectsRequest request(BucketName);
            /* 設定正斜線(/)為檔案夾的分隔字元。*/
            request.setDelimiter("/");
            request.setPrefix("fun/");
            request.setMarker(nextMarker);
            auto outcome = client.ListObjects(request);
    
            if (!outcome.isSuccess()) {    
                /* 異常處理。*/
                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);
    
        /* 釋放網路等資源。*/
        ShutdownSdk();
        return 0;
    }

相關文檔