本文介绍如何列举指定存储空间(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之后的文件。

简单列举文件

以下代码用于列举指定存储空间下的文件,默认列举100个文件。

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

int main(void)
{
     /* 初始化OSS账号信息*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

     /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, 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;
        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;
         }      
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

列举指定个数的文件

以下代码用于列举指定个数的文件。

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

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

     /* 初始化网络等资源 */
    InitializeSdk();

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

    /* 列举文件 */
    ListObjectsRequest request(BucketName);
    /* 设置最大个数 */
    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;
        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; 
        }      
    }

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

列举指定目录下的文件和子目录

以下代码用于列举指定目录下的文件和子目录:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string keyPrefix = "yourkeyPrefix ";

     /* 初始化网络等资源 */
    InitializeSdk();

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

    std::string nextMarker = "";
    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();
    } while (outcome.result().IsTruncated());

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

列举指定目录下的文件大小

以下代码用于获取指定目录下的文件大小:

#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 = "";
        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.Key() 
            }

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

      return size;
    }

    /*初始化OSS账号信息*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string keyPrefix = "yourkeyPrefix";

    /*初始化网络等资源*/
    InitializeSdk();

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


    std::string nextMarker = "";
    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();
    } while (outcome.result().IsTruncated());

    /*释放网络等资源*/
    ShutdownSdk();
    return 0;
}

列举指定前缀的文件

以下代码用于列举包含指定前缀(prefix)的文件。

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

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string keyPrefix = "yourkeyPrefix ";

     /* 初始化网络等资源 */
    InitializeSdk();

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

    std::string nextMarker = "";
    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();
    } while (outcome.result().IsTruncated());

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

列举指定 marker 之后的文件

参数marker代表文件名称。以下代码用于列举指定marker之后的文件。

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

int main(void)
{
     /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string YourMarker = "yourMarker ";

     /* 初始化网络等资源 */
    InitializeSdk();

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

    do {
        /* 列举文件 */
        ListObjectsRequest request(BucketName);
         /* 列举指定marker之后的文件 */
        request.setMarker(YourMarker);
        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;  
        }
        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账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

     /* 初始化网络等资源 */
    InitializeSdk();

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

    std::string nextMarker = "";
    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();
    } while (outcome.result().IsTruncated());

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

文件夹功能

OSS没有文件夹的概念,所有元素都是以文件来存储。创建文件夹本质上来说是创建了一个大小为0并以正斜线(/)结尾的文件。这个文件可以被上传和下载,控制台会对以正斜线(/)结尾的文件以文件夹的方式展示。

通过delimiter和prefix两个参数可以模拟文件夹功能:

  • 如果设置prefix为某个文件夹名称,则会列举以此prefix开头的文件,即该文件夹下所有的文件和子文件夹(目录),显示为Objects。
  • 如果再设置delimiter为正斜线(/),则只列举该文件夹下的文件和子文件夹(目录),该文件夹下的子文件夹(目录)则显示为CommonPrefixes,子文件夹下的文件和文件夹不显示。

文件夹详情请参见文件夹功能。创建文件夹的完整代码请参见GitHub

假设存储空间中有4个文件: oss.jpg、fun/test.jpg、fun/movie/001.avi、fun/movie/007.avi,正斜线(/)作为文件夹的分隔符。下面的示例展示了如何模拟文件夹功能。

  • 列举存储空间下所有文件

    以下代码用于列举指定存储空间下的所有文件。

    #include <alibabacloud/oss/OssClient.h>
    using namespace AlibabaCloud::OSS;
    
    int main(void)
    {
        /*初始化OSS账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
    
        /*初始化网络等资源*/
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
    
        ListObjectOutcome outcome;
    
        std::string nextMarker = "";
    
        do {
            /*列举文件*/
            ListObjectsRequest request(BucketName);
            request.setMarker(nextMarker);
            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();
        } while (outcome.result().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账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
    
        /*初始化网络等资源*/
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        ListObjectOutcome outcome;
        std::string nextMarker = "";
        do {
                /*列举文件*/
                ListObjectsRequest request(BucketName);
                request.setPrefix("fun/");
                request.setMarker(nextMarker);
                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();
        } while (outcome.result().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账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
    
        /*初始化网络等资源*/
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        ListObjectOutcome outcome;
        std::string nextMarker = "";
        do {
                /*列举文件*/
                ListObjectsRequest request(BucketName);
                /*设置正斜线(/)为文件夹的分隔符*/
                request.setDelimiter("/");
                request.setPrefix("fun/");
                request.setMarker(nextMarker);
                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();
        } while (outcome.result().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 = "";
        int64_t size = 0;
        ListObjectOutcome outcome;
        do {
                /*列举文件*/
                ListObjectsRequest request(bucketName);
                request.setPrefix(folder);
                request.setMarker(nextMarker);
                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();
            } while (outcome.result().IsTruncated());
    
          return size;
    }
    
    int main(void)
    {   
        /*初始化OSS账号信息*/
        std::string AccessKeyId = "yourAccessKeyId";
        std::string AccessKeySecret = "yourAccessKeySecret";
        std::string Endpoint = "yourEndpoint";
        std::string BucketName = "yourBucketName";
    
        /*初始化网络等资源*/
        InitializeSdk();
    
        ClientConfiguration conf;
        OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
    
        ListObjectOutcome outcome;
        std::string nextMarker = "";
        do {
            /*列举文件*/
            ListObjectsRequest request(BucketName);
            /*设置正斜线(/)为文件夹的分隔符*/
            request.setDelimiter("/");
            request.setPrefix("fun/");
            request.setMarker(nextMarker);
            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();
        } while (outcome.result().IsTruncated());
    
        /*释放网络等资源*/
        ShutdownSdk();
        return 0;
    }