全部产品
Search
文档中心

对象存储 OSS:C++列举文件

更新时间:Dec 20, 2023

本文介绍如何列举指定存储空间(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;
    }

相关文档