本文介绍如何列举文件。

列举文件完整代码请参见GitHub

OSS文件按照字母顺序排列。您可以通过oss_list_object列出存储空间下的文件。主要的参数如下:

参数 说明
delimiter 对文件名称进行分组的一个字符。所有名称包含指定的前缀且第一次出现delimiter字符之间的文件作为一组元素(commonPrefixes)。
prefix 限定返回的文件必须以prefix作为前缀。
max_ret 限定此次列举文件的最大个数。默认值为100,最大值为1000。
marker 列举指定marker之后的文件。

列举所有文件

以下代码用于列出当前存储空间下的所有文件。

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* 用char*类型的字符串初始化aos_string_t类型。*/
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* 是否设置了CNAME。0表示不设置。*/
    options->config->is_cname = 0;
    /* 用于设置网络相关参数,比如超时时间等。*/
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* 用于内存管理的内存池(pool),等价于apr_pool_t。其实现代码在apr库中。*/
    aos_pool_t *pool;
    /* 重新创建一个内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
    aos_pool_create(&pool, NULL);
    /* 创建并初始化options,该参数包括endpoint、access_key_id、access_key_secret、is_cname、curl等全局配置信息。*/
    oss_request_options_t *oss_client_options;
    /* 在内存池中分配内存给options。*/
    oss_client_options = oss_request_options_create(pool);
    /* 初始化Client的选项oss_client_options。*/
    init_options(oss_client_options);
    /* 初始化参数。*/
    aos_string_t bucket;
    aos_status_t *resp_status = NULL; 
    oss_list_object_params_t *params = NULL;
    oss_list_object_content_t *content = NULL;
    int size = 0;
    char *line = NULL;
    char *prefix = "";
    char *nextMarker = "";
    aos_str_set(&bucket, bucket_name);
    params = oss_create_list_object_params(pool);
    params->max_ret = 100;
    aos_str_set(&params->prefix, prefix);
    aos_str_set(&params->marker, nextMarker);
    printf("Object\tSize\tLastModified\n");
    /* 列举所有文件。*/
    do {
        resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
        if (!aos_status_is_ok(resp_status))
        {
            printf("list object failed\n");
            break;
        }
        aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
            ++size;
            line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
                content->size.len, content->size.data, 
                content->last_modified.len, content->last_modified.data);
            printf("%s", line);
        }
        nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
        aos_str_set(&params->marker, nextMarker);
        aos_list_init(&params->object_list);
        aos_list_init(&params->common_prefix_list);
    } while (params->truncated == AOS_TRUE);
    printf("Total %d\n", size);
    /* 释放内存池,相当于释放了请求过程中各资源分配的内存。*/
    aos_pool_destroy(pool);
    /* 释放之前分配的全局资源。*/
    aos_http_io_deinitialize();
    return 0;
}
说明 默认列举文件数最多1000,若超过,则只返回前1000个文件, 且返回结果中的 truncated 为 true,并返回 next_marker 作为下次读取的起点。若想调整返回文件数,可修改 max_ret 参数,或使用marker参数分次读取。

列举指定个数的文件

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

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* 用char*类型的字符串初始化aos_string_t类型。*/
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* 是否设置了CNAME。0表示不设置。*/
    options->config->is_cname = 0;
    /* 用于设置网络相关参数,比如超时时间等。*/
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* 程序入口调用aos_http_io_initialize方法,这个方法内部会做一些全局资源的初始化,涉及网络、内存等部分。*/
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* 等价于apr_pool_t,用于内存管理的内存池,实现代码在apr库中。*/
    aos_pool_t *pool;
    /* 重新创建一个新的内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
    aos_pool_create(&pool, NULL);
    /* 创建并初始化options,这个参数内部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl参数等全局配置信息。*/
    oss_request_options_t *oss_client_options;
    /* options的内存是由pool分配的,后续释放pool后,相当于释放了options的内存,不需要单独释放内存。*/
    oss_client_options = oss_request_options_create(pool);
    /* 初始化Client的选项oss_client_options。*/
    init_options(oss_client_options);
    /* 初始化参数。*/
    aos_string_t bucket;
    aos_status_t *resp_status = NULL; 
    oss_list_object_params_t *params = NULL;
    oss_list_object_content_t *content = NULL;
    int size = 0;
    char *line = NULL;
    char *prefix = "";
    char *nextMarker = "";
    aos_str_set(&bucket, bucket_name);
    params = oss_create_list_object_params(pool);
    /* 设置列举文件的最大个数为200。*/
    params->max_ret = 200;
    aos_str_set(&params->prefix, prefix);
    aos_str_set(&params->marker, nextMarker);
    printf("Object\tSize\tLastModified\n");
    /* 列举所有文件。*/
    resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
    if (!aos_status_is_ok(resp_status))
    {
        printf("list object failed\n");
    }
    aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
        ++size;
        line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
            content->size.len, content->size.data, 
            content->last_modified.len, content->last_modified.data);
        printf("%s", line);
    }
    printf("Total %d\n", size);
    /* 执行完一个请求后,释放这个内存池,相当于释放了这个请求过程中各个部分分配的内存。*/
    aos_pool_destroy(pool);
    /* 程序结束前,调用aos_http_io_deinitialize方法释放之前分配的全局资源。*/
    aos_http_io_deinitialize();
    return 0;
}

列举指定前缀的文件

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

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* 用char*类型的字符串初始化aos_string_t类型。*/
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* 是否设置了CNAME。0表示不设置。*/
    options->config->is_cname = 0;
    /* 用于设置网络相关参数,比如超时时间等。*/
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* 程序入口调用aos_http_io_initialize方法,这个方法内部会做一些全局资源的初始化,涉及网络,内存等部分。*/
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* 等价于apr_pool_t,用于内存管理的内存池,实现代码在apr库中。*/
    aos_pool_t *pool;
    /* 重新创建一个新的内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
    aos_pool_create(&pool, NULL);
    /* 创建并初始化options,这个参数内部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl参数等全局配置信息。*/
    oss_request_options_t *oss_client_options;
    /* options的内存是由pool分配的,后续释放pool后,相当于释放了options的内存,不需要单独释放内存。*/
    oss_client_options = oss_request_options_create(pool);
    /* 初始化Client的选项oss_client_options。*/
    init_options(oss_client_options);
    /* 初始化参数。*/
    aos_string_t bucket;
    aos_status_t *resp_status = NULL; 
    oss_list_object_params_t *params = NULL;
    oss_list_object_content_t *content = NULL;
    int size = 0;
    char *line = NULL;
    /* 列举包含指定前缀的文件。*/
    char *prefix = "<yourObjectPefix>";
    char *nextMarker = "";
    aos_str_set(&bucket, bucket_name);
    params = oss_create_list_object_params(pool);
    params->max_ret = 100;
    aos_str_set(&params->prefix, prefix);
    aos_str_set(&params->marker, nextMarker);
    printf("Object\tSize\tLastModified\n");
    /* 列举所有文件。*/
    do {
        resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
        if (!aos_status_is_ok(resp_status))
        {
            printf("list object failed\n");
            break;
        }
        aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
            ++size;
            line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
                content->size.len, content->size.data, 
                content->last_modified.len, content->last_modified.data);
            printf("%s", line);
        }
        nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
        aos_str_set(&params->marker, nextMarker);
        aos_list_init(&params->object_list);
        aos_list_init(&params->common_prefix_list);
    } while (params->truncated == AOS_TRUE);
    printf("Total %d\n", size);
    /* 执行完一个请求后,释放这个内存池,相当于释放了这个请求过程中各个部分分配的内存。*/
    aos_pool_destroy(pool);
    /* 程序结束前,调用aos_http_io_deinitialize方法释放之前分配的全局资源。*/
    aos_http_io_deinitialize();
    return 0;
}

列举指定marker之后的文件

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

#include "oss_api.h"
#include "aos_http_io.h"
const char *endpoint = "<yourEndpoint>";
const char *access_key_id = "<yourAccessKeyId>";
const char *access_key_secret = "<yourAccessKeySecret>";
const char *bucket_name = "<yourBucketName>";
void init_options(oss_request_options_t *options)
{
    options->config = oss_config_create(options->pool);
    /* 用char*类型的字符串初始化aos_string_t类型。*/
    aos_str_set(&options->config->endpoint, endpoint);
    aos_str_set(&options->config->access_key_id, access_key_id);
    aos_str_set(&options->config->access_key_secret, access_key_secret);
    /* 是否设置了CNAME。0表示不设置。*/
    options->config->is_cname = 0;
    /* 用于设置网络相关参数,比如超时时间等。*/
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* 程序入口调用aos_http_io_initialize方法,这个方法内部会做一些全局资源的初始化,涉及网络,内存等部分。*/
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* 等价于apr_pool_t,用于内存管理的内存池,实现代码在apr库中。*/
    aos_pool_t *pool;
    /* 重新创建一个新的内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
    aos_pool_create(&pool, NULL);
    /* 创建并初始化options,这个参数内部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl参数等全局配置信息。*/
    oss_request_options_t *oss_client_options;
    /* options的内存是由pool分配的,后续释放pool后,相当于释放了options的内存,不需要单独释放内存。*/
    oss_client_options = oss_request_options_create(pool);
    /* 初始化Client的选项oss_client_options。*/
    init_options(oss_client_options);
    /* 初始化参数。*/
    aos_string_t bucket;
    aos_status_t *resp_status = NULL; 
    oss_list_object_params_t *params = NULL;
    oss_list_object_content_t *content = NULL;
    int size = 0;
    char *line = NULL;
    char *prefix = "";
    /* 列举指定marker之后的文件。*/
    char *nextMarker = "<yourObjectMarker>";
    aos_str_set(&bucket, bucket_name);
    params = oss_create_list_object_params(pool);
    params->max_ret = 100;
    aos_str_set(&params->prefix, prefix);
    aos_str_set(&params->marker, nextMarker);
    printf("Object\tSize\tLastModified\n");
    /* 列举所有文件。*/
    do {
        resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
        if (!aos_status_is_ok(resp_status))
        {
            printf("list object failed\n");
            break;
        }
        aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
            ++size;
            line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
                content->size.len, content->size.data, 
                content->last_modified.len, content->last_modified.data);
            printf("%s", line);
        }
        nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
        aos_str_set(&params->marker, nextMarker);
        aos_list_init(&params->object_list);
        aos_list_init(&params->common_prefix_list);
    } while (params->truncated == AOS_TRUE);
    printf("Total %d\n", size);
    /* 执行完一个请求后,释放这个内存池,相当于释放了这个请求过程中各个部分分配的内存。*/
    aos_pool_destroy(pool);
    /* 程序结束前,调用aos_http_io_deinitialize方法释放之前分配的全局资源。*/
    aos_http_io_deinitialize();
    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 "oss_api.h"
    #include "aos_http_io.h"
    const char *endpoint = "<yourEndpoint>";
    const char *access_key_id = "<yourAccessKeyId>";
    const char *access_key_secret = "<yourAccessKeySecret>";
    const char *bucket_name = "<yourBucketName>";
    void init_options(oss_request_options_t *options)
    {
        options->config = oss_config_create(options->pool);
        /* 用char*类型的字符串初始化aos_string_t类型。*/
        aos_str_set(&options->config->endpoint, endpoint);
        aos_str_set(&options->config->access_key_id, access_key_id);
        aos_str_set(&options->config->access_key_secret, access_key_secret);
        /* 是否设置了CNAME。0表示不设置。*/
        options->config->is_cname = 0;
        /* 用于设置网络相关参数,比如超时时间等。*/
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    int main(int argc, char *argv[])
    {
        /* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* 用于内存管理的内存池(pool),等价于apr_pool_t。其实现代码在apr库中。*/
        aos_pool_t *pool;
        /* 重新创建一个内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
        aos_pool_create(&pool, NULL);
        /* 创建并初始化options,该参数包括endpoint、access_key_id、access_key_secret、is_cname、curl等全局配置信息。*/
        oss_request_options_t *oss_client_options;
        /* 在内存池中分配内存给options。*/
        oss_client_options = oss_request_options_create(pool);
        /* 初始化Client的选项oss_client_options。*/
        init_options(oss_client_options);
        /* 初始化参数。*/
        aos_string_t bucket;
        aos_status_t *resp_status = NULL; 
        oss_list_object_params_t *params = NULL;
        oss_list_object_content_t *content = NULL;
        int size = 0;
        char *line = NULL;
        char *nextMarker = "";
        aos_str_set(&bucket, bucket_name);
        params = oss_create_list_object_params(pool);
        params->max_ret = 100;
        aos_str_set(&params->marker, nextMarker);
        printf("Object\tSize\tLastModified\n");
        /* 列举所有文件。*/
        do {
            resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
            if (!aos_status_is_ok(resp_status))
            {
                printf("list object failed\n");
                break;
            }
            aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
                ++size;
                line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
                    content->size.len, content->size.data, 
                    content->last_modified.len, content->last_modified.data);
                printf("%s", line);
            }
            nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
            aos_str_set(&params->marker, nextMarker);
            aos_list_init(&params->object_list);
            aos_list_init(&params->common_prefix_list);
        } while (params->truncated == AOS_TRUE);
        printf("Total %d\n", size);
        /* 释放内存池,相当于释放了请求过程中各资源分配的内存。*/
        aos_pool_destroy(pool);
        /* 释放之前分配的全局资源。*/
        aos_http_io_deinitialize();
        return 0;
    }

    返回结果如下:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    oss.jpg
    
    CommonPrefixes:
    					
  • 列举指定目录下所有文件

    以下代码用于列举指定目录下的所有文件。

    #include "oss_api.h"
    #include "aos_http_io.h"
    const char *endpoint = "<yourEndpoint>";
    const char *access_key_id = "<yourAccessKeyId>";
    const char *access_key_secret = "<yourAccessKeySecret>";
    const char *bucket_name = "<yourBucketName>";
    void init_options(oss_request_options_t *options)
    {
        options->config = oss_config_create(options->pool);
        /* aos_str_set是用char*类型的字符串初始化aos_string_t类型。*/
        aos_str_set(&options->config->endpoint, endpoint);
        aos_str_set(&options->config->access_key_id, access_key_id);
        aos_str_set(&options->config->access_key_secret, access_key_secret);
        /* 是否设置了CNAME。0表示不设置。*/
        options->config->is_cname = 0;
        /* 用于设置网络相关参数,比如超时时间等。*/
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    int main(int argc, char *argv[])
    {
        /* 程序入口调用aos_http_io_initialize方法,该方法内部会做某些全局资源的初始化,涉及网络、内存等部分。*/
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* 等价于apr_pool_t,用于内存管理的内存池,实现代码在apr库中。*/
        aos_pool_t *pool;
        /* 重新创建一个新的内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
        aos_pool_create(&pool, NULL);
        /* 创建并初始化options,这个参数内部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl参数等全局配置信息。*/
        oss_request_options_t *oss_client_options;
        /* options的内存是由pool分配的,后续释放pool后,相当于释放了options的内存,不需要单独释放内存。*/
        oss_client_options = oss_request_options_create(pool);
        /* 初始化Client的选项oss_client_options。*/
        init_options(oss_client_options);
        /* 初始化参数。*/
        aos_string_t bucket;
        aos_status_t *resp_status = NULL; 
        oss_list_object_params_t *params = NULL;
        oss_list_object_content_t *content = NULL;
        int size = 0;
        char *line = NULL;
        /* 列举包含指定前缀的文件。*/
        char *prefix = "fun/";
        char *nextMarker = "";
        aos_str_set(&bucket, bucket_name);
        params = oss_create_list_object_params(pool);
        params->max_ret = 100;
        aos_str_set(&params->prefix, prefix);
        aos_str_set(&params->marker, nextMarker);
        printf("Object\tSize\tLastModified\n");
        /* 列举所有文件。*/
        do {
            resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
            if (!aos_status_is_ok(resp_status))
            {
                printf("list object failed\n");
                break;
            }
            aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
                ++size;
                line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
                    content->size.len, content->size.data, 
                    content->last_modified.len, content->last_modified.data);
                printf("%s", line);
            }
            nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
            aos_str_set(&params->marker, nextMarker);
            aos_list_init(&params->object_list);
            aos_list_init(&params->common_prefix_list);
        } while (params->truncated == AOS_TRUE);
        printf("Total %d\n", size);
        /* 执行完一个请求后,释放这个内存池,相当于释放了这个请求过程中各个部分分配的内存。*/
        aos_pool_destroy(pool);
        /* 程序结束前,调用aos_http_io_deinitialize方法释放之前分配的全局资源 */
        aos_http_io_deinitialize();
        return 0;
    }

    返回结果如下:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    
    CommonPrefixes:
    					
  • 列举目录下的文件和子目录

    以下代码用于列举指定目录下的文件和子目录。

    #include "oss_api.h"
    #include "aos_http_io.h"
    const char *endpoint = "<yourEndpoint>";
    const char *access_key_id = "<yourAccessKeyId>";
    const char *access_key_secret = "<yourAccessKeySecret>";
    const char *bucket_name = "<yourBucketName>";
    void init_options(oss_request_options_t *options)
    {
        options->config = oss_config_create(options->pool);
        /* aos_str_set是用char*类型的字符串初始化aos_string_t类型。*/
        aos_str_set(&options->config->endpoint, endpoint);
        aos_str_set(&options->config->access_key_id, access_key_id);
        aos_str_set(&options->config->access_key_secret, access_key_secret);
        /* 是否设置了CNAME。0表示不设置。*/
        options->config->is_cname = 0;
        /* 用于设置网络相关参数,比如超时时间等。*/
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    int main(int argc, char *argv[])
    {
        /* 程序入口调用aos_http_io_initialize方法,该方法内部会做某些全局资源的初始化,涉及网络、内存等部分。*/
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* 等价于apr_pool_t,用于内存管理的内存池,实现代码在apr库中。*/
        aos_pool_t *pool;
        /* 重新创建一个新的内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
        aos_pool_create(&pool, NULL);
        /* 创建并初始化options,这个参数内部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl参数等全局配置信息。*/
        oss_request_options_t *oss_client_options;
        /* options的内存是由pool分配的,后续释放pool后,相当于释放了options的内存,不需要单独释放内存。*/
        oss_client_options = oss_request_options_create(pool);
        /* 初始化Client的选项oss_client_options。*/
        init_options(oss_client_options);
        /* 初始化参数。*/
        aos_string_t bucket;
        aos_status_t *resp_status = NULL; 
        oss_list_object_params_t *params = NULL;
        oss_list_object_content_t *content = NULL;
        oss_list_object_common_prefix_t *commonPrefix = NULL;
        int size = 0;
        char *line = NULL;
        /* 列举包含指定前缀的文件。*/
        char *prefix = "fun/";
        char *nextMarker = "";
        /* 设置正斜线(/)为文件夹的分隔符。*/
        char *delimiter = "/";
        aos_str_set(&bucket, bucket_name);
        params = oss_create_list_object_params(pool);
        params->max_ret = 100;
        aos_str_set(&params->prefix, prefix);
        aos_str_set(&params->marker, nextMarker);
        aos_str_set(&params->delimiter, delimiter);
        printf("Object\tSize\tLastModified\n");
        /*列举所有文件。*/
        do {
            resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
            if (!aos_status_is_ok(resp_status))
            {
                printf("list object failed\n");
                break;
            }
            aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
                ++size;
                line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
                    content->size.len, content->size.data, 
                    content->last_modified.len, content->last_modified.data);
                printf("%s", line);
            }
            aos_list_for_each_entry(oss_list_object_common_prefix_t, commonPrefix, &params->common_prefix_list, node) {
                line = apr_psprintf(pool, "%.*s", commonPrefix->prefix.len, commonPrefix->prefix.data);
                printf("%s", line);
            }
            nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
            aos_str_set(&params->marker, nextMarker);
            aos_list_init(&params->object_list);
            aos_list_init(&params->common_prefix_list);
        } while (params->truncated == AOS_TRUE);
        printf("Total %d\n", size);
        /* 执行完一个请求后,释放这个内存池,相当于释放了这个请求过程中各个部分分配的内存。*/
        aos_pool_destroy(pool);
        /* 程序结束前,调用aos_http_io_deinitialize方法释放之前分配的全局资源。*/
        aos_http_io_deinitialize();
        return 0;
    }

    返回结果如下:

    Objects:
    fun/test.jpg
    
    CommonPrefixes:
    fun/movie/
    					
  • 列举指定目录下的文件大小

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

    #include "oss_api.h"
    #include "aos_http_io.h"
    const char *endpoint = "<yourEndpoint>";
    const char *access_key_id = "<yourAccessKeyId>";
    const char *access_key_secret = "<yourAccessKeySecret>";
    const char *bucket_name = "<yourBucketName>";
    
    void init_options(oss_request_options_t *options)
    {
        options->config = oss_config_create(options->pool);
        /* aos_str_set是用char*类型的字符串初始化aos_string_t类型。*/
        aos_str_set(&options->config->endpoint, endpoint);
        aos_str_set(&options->config->access_key_id, access_key_id);
        aos_str_set(&options->config->access_key_secret, access_key_secret);
        /* 是否设置了CNAME。0表示不设置。*/
        options->config->is_cname = 0;
        /* 用于设置网络相关参数,比如超时时间等。*/
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    
    int64_t calculateFolderLength (aos_string_t bucketName, char* folder)
    {
        int64_t size = 0;
        oss_list_object_params_t *params = NULL;
        char *nextMarker = "";
        aos_status_t *resp_status = NULL;
        aos_pool_t *p = NULL;
        oss_request_options_t *options = NULL;
        oss_list_object_content_t* content = NULL;
        aos_pool_create(&p, NULL);
        options = oss_request_options_create(p);
        params = oss_create_list_object_params(p);
        params->max_ret = 10;
        aos_str_set(&params->prefix, folder);
        aos_str_set(&params->marker, nextMarker);
    
        /* 列举所有文件。*/
        do {
            resp_status = oss_list_object(options, &bucketName, params, NULL);
            if (!aos_status_is_ok(resp_status))
            {
                printf("list object failed\n");
                break;
            }
            aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
                size += content->size.len;
            }
            nextMarker = apr_psprintf(p, "%.*s", params->next_marker.len, params->next_marker.data);
            aos_str_set(&params->marker, nextMarker);
            aos_list_init(&params->object_list);
            aos_list_init(&params->common_prefix_list);
        } while (params->truncated == AOS_TRUE); 
    
        /* 执行完一个请求后,释放这个内存池,相当于释放了这个请求过程中各个部分分配的内存。*/
        aos_pool_destroy(p);
        return size;
    }
    
    int main(int argc, char *argv[])
    {
        /* 程序入口调用aos_http_io_initialize方法,该方法内部会做某些全局资源的初始化,涉及网络、内存等部分。*/
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* 等价于apr_pool_t,用于内存管理的内存池,实现代码在apr库中。*/
        aos_pool_t *pool;
        /* 重新创建一个新的内存池,第二个参数值是NULL,表示没有继承其它内存池。*/
        aos_pool_create(&pool, NULL);
        /* 创建并初始化options,这个参数内部主要包括endpoint,access_key_id,access_key_secret,is_cname, curl参数等全局配置信息。*/
        oss_request_options_t *oss_client_options;
        /* options的内存是由pool分配的,后续释放pool后,相当于释放了options的内存,不需要单独释放内存。*/
        oss_client_options = oss_request_options_create(pool);
        /* 初始化Client的选项oss_client_options。*/
        init_options(oss_client_options);
        /* 初始化参数。*/
        aos_string_t bucket;
        aos_status_t *resp_status = NULL; 
        oss_list_object_params_t *params = NULL;
        oss_list_object_content_t *content = NULL;
        oss_list_object_common_prefix_t *commonPrefix = NULL;
        int size = 0;
        char *line = NULL;
        /* 列举包含指定前缀的文件。*/
        char *prefix = "fun/";
        char *nextMarker = "";
        /* 设置正斜线(/)为文件夹的分隔符。*/
        char *delimiter = "/";
        aos_str_set(&bucket, bucket_name);
        params = oss_create_list_object_params(pool);
        params->max_ret = 100;
        aos_str_set(&params->prefix, prefix);
        aos_str_set(&params->marker, nextMarker);
        aos_str_set(&params->delimiter, delimiter);
        printf("Object\tSize\tLastModified\n");
        /*列举所有文件。*/
        do {
            resp_status = oss_list_object(oss_client_options, &bucket, params, NULL);
            if (!aos_status_is_ok(resp_status))
            {
                printf("list object failed\n");
                break;
            }
            aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
                ++size;
                line = apr_psprintf(pool, "%.*s\t%.*s\t%.*s\n", content->key.len, content->key.data, 
                    content->size.len, content->size.data, 
                    content->last_modified.len, content->last_modified.data);
                printf("%s", line);
            }
            aos_list_for_each_entry(oss_list_object_common_prefix_t, commonPrefix, &params->common_prefix_list, node) {
                line = apr_psprintf(pool, "%.*s", commonPrefix->prefix.len, commonPrefix->prefix.data);
                int64_t size = calculateFolderLength(bucket, commonPrefix->prefix.data);
                printf("Total %d\n", size);
            }
            nextMarker = apr_psprintf(pool, "%.*s", params->next_marker.len, params->next_marker.data);
            aos_str_set(&params->marker, nextMarker);
            aos_list_init(&params->object_list);
            aos_list_init(&params->common_prefix_list);
        } while (params->truncated == AOS_TRUE);
        printf("Total %d\n", size);
        /* 执行完一个请求后,释放这个内存池,相当于释放了这个请求过程中各个部分分配的内存。*/
        aos_pool_destroy(pool);
        /* 程序结束前,调用aos_http_io_deinitialize方法释放之前分配的全局资源。*/
        aos_http_io_deinitialize();
        return 0;
    }