This topic describes how to list the objects stored in a bucket.

For the complete code used to list objects, visit GitHub.

Objects are listed in alphabetical order. You can use oss_list_object to list objects in a bucket. The following table describes common parameters.

Parameter Description
delimiter Groups objects by name. CommonPrefixes specifies a set of substrings of object names. The substrings start with the prefix and end with the next occurrence of the specified delimiter.
prefix Specifies the prefix that returned object names must contain.
max_ret Specifies the maximum number of objects that can be listed at a time. The default value is 100. The maximum value is 1000.
marker Specifies the name of the object after which the listing begins.

List all objects

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

#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);
    /* Use a char* string to initialize the aos_string_t data type. */
    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);
    /* Specify whether to use CNAME. 0 indicates that the CNAME is not used. */
    options->config->is_cname = 0;
    /* Configure network parameters, such as timeout. */
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory. */
    if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
    aos_pool_t *pool;
    /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
    aos_pool_create(&pool, NULL);
    /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
    oss_request_options_t *oss_client_options;
    /* Allocate a memory chunk in the memory pool to options. */
    oss_client_options = oss_request_options_create(pool);
    /* Initialize oss_client_options. */
    init_options(oss_client_options);
    /* Initialize parameters. */
    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");
    /* List all objects. */
    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);
    /* Release the memory pool. The memory allocated to various resources used for the request is released. */
    aos_pool_destroy(pool);
    /* Release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}
Note If a bucket contains more than 1,000 objects, only the first 1,000 objects are returned and the truncated parameter in the returned results is true. Additionally, the next_marker parameter is returned to serve as the start point for the next read. To list more objects at a time, you can modify the max_ret parameter. You can also use the marker parameter to perform multiple read operations.

List a specified number of objects

#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);
    /* Use a char* string to initialize the aos_string_t data type. */
    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);
    /* Specify whether to use CNAME. */
    options->config->is_cname = 0;
    /* Configure network parameters, such as timeout. */
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory. */
    if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
    aos_pool_t *pool;
    /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
    aos_pool_create(&pool, NULL);
    /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
    oss_request_options_t *oss_client_options;
    /* The memory consumed by options is allocated by the memory pool and is released with the pool. */
    oss_client_options = oss_request_options_create(pool);
    /* Initialize oss_client_options. */
    init_options(oss_client_options);
    /* Initialize parameters. */
    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);
    /* Set the maximum number of returned objects to 200. */
    params->max_ret = 200;
    aos_str_set(&params->prefix, prefix);
    aos_str_set(&params->marker, nextMarker);
    printf("Object\tSize\tLastModified\n");
    /* List all objects. */
    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);
    /* Release the memory pool after the request is complete. In this case, the memory allocated to various resources used for the request is released. */
    aos_pool_destroy(pool);
    /* Before you end the program, call aos_http_io_deinitialize to release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

List objects whose names contain the specified 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);
    /* Use a char* string to initialize the aos_string_t data type. */
    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);
    /* Specify whether to use CNAME. */
    options->config->is_cname = 0;
    /* Configure network parameters, such as timeout. */
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory.*/
    if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
    aos_pool_t *pool;
    /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
    aos_pool_create(&pool, NULL);
    /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
    oss_request_options_t *oss_client_options;
    /* The memory consumed by options is allocated by the memory pool and is released with the pool. */
    oss_client_options = oss_request_options_create(pool);
    /* Initialize oss_client_options. */
    init_options(oss_client_options);
    /* Initialize parameters. */
    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;
    /* List the objects whose names contain the specified prefix. */
    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");
    /* List all objects. */
    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);
    /* Release the memory pool after the request is complete. In this case, the memory allocated to various resources used for the request is released. */
    aos_pool_destroy(pool);
    /* Before you end the program, call aos_http_io_deinitialize to release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

List objects that follow a specified 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);
    /* Use a char* string to initialize the aos_string_t data type. */
    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);
    /* Specify whether to use CNAME. */
    options->config->is_cname = 0;
    /* Configure network parameters, such as timeout. */
    options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
    /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory.*/
    if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
    aos_pool_t *pool;
    /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
    aos_pool_create(&pool, NULL);
    /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
    oss_request_options_t *oss_client_options;
    /* The memory consumed by options is allocated by the memory pool and is released with the pool. */
    oss_client_options = oss_request_options_create(pool);
    /* Initialize oss_client_options. */
    init_options(oss_client_options);
    /* Initialize parameters. */
    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 = "";
    /* List objects that follow a specified 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");
    /* List all objects. */
    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);
    /* Release the memory pool after the request is complete. In this case, the memory allocated to various resources used for the request is released. */
    aos_pool_destroy(pool);
    /* Before you end the program, call aos_http_io_deinitialize to release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

Folder simulation

For more information about folders, see Folder simulation. For the complete code used to create a folder, visit GitHub.

  • List all objects in a bucket
    #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);
        /* Use a char* string to initialize the aos_string_t data type. */
        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);
        /* Specify whether to use CNAME. 0 indicates that the CNAME is not used. */
        options->config->is_cname = 0;
        /* Configure network parameters, such as timeout. */
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    int main(int argc, char *argv[])
    {
        /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory. */
        if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
        aos_pool_t *pool;
        /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
        aos_pool_create(&pool, NULL);
        /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
        oss_request_options_t *oss_client_options;
        /* Allocate a memory chunk in the memory pool to options. */
        oss_client_options = oss_request_options_create(pool);
        /* Initialize oss_client_options. */
        init_options(oss_client_options);
        /* Initialize parameters. */
        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");
        /* List all objects. */
        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);
        /* Release the memory pool. The memory allocated to various resources used for the request is released. */
        aos_pool_destroy(pool);
        /* Release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }
  • List all objects in a specified folder
    #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);
        /* Use a char* string to initialize the aos_string_t data type. */
        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);
        /* Specify whether to use CNAME. */
        options->config->is_cname = 0;
        /* Configure network parameters, such as timeout. */
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    int main(int argc, char *argv[])
    {
        /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory. */
        if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
        aos_pool_t *pool;
        /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
        aos_pool_create(&pool, NULL);
        /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
        oss_request_options_t *oss_client_options;
        /* The memory consumed by options is allocated by the memory pool and is released with the pool. */
        oss_client_options = oss_request_options_create(pool);
        /* Initialize oss_client_options. */
        init_options(oss_client_options);
        /* Initialize parameters. */
        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;
        /* List the objects whose names contain the specified prefix. */
        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");
        /* List all objects. */
        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);
        /* Release the memory pool after the request is complete. In this case, the memory allocated to various resources used for the request is released. */
        aos_pool_destroy(pool);
        /* Before you end the program, call aos_http_io_deinitialize to release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }
  • List the objects and subfolders in a folder
    #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);
        /* Use a char* string to initialize the aos_string_t data type. */
        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);
        /* Specify whether to use CNAME. */
        options->config->is_cname = 0;
        /* Configure network parameters, such as timeout. */
        options->ctl = aos_http_controller_create(options->pool, 0);
    }
    int main(int argc, char *argv[])
    {
        /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory. */
        if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
        aos_pool_t *pool;
        /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
        aos_pool_create(&pool, NULL);
        /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
        oss_request_options_t *oss_client_options;
        /* The memory consumed by options is allocated by the memory pool and is released with the pool. */
        oss_client_options = oss_request_options_create(pool);
        /* Initialize oss_client_options. */
        init_options(oss_client_options);
        /* Initialize parameters. */
        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;
        /* List the objects whose names contain the specified prefix. */
        char *prefix = "fun/";
        char *nextMarker = "";
        /* Set the delimiter to forward slashes (/). */
        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");
        /* List all objects. */
        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);
        /* Release the memory pool after the request is complete. In this case, the memory allocated to various resources used for the request is released. */
        aos_pool_destroy(pool);
        /* Before you end the program, call aos_http_io_deinitialize to release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }
  • List sizes of objects in a specified folder

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

    #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);
        /* Use a char* string to initialize the aos_string_t data type. */
        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);
        /* Specify whether to use CNAME. */
        options->config->is_cname = 0;
        /* Configure network parameters, such as timeout. */
        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);
    
        /* List all objects. */
        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); 
    
        /* Release the memory pool after the request is complete. In this case, the memory allocated to various resources used for the request is released. */
        aos_pool_destroy(p);
        return size;
    }
    
    int main(int argc, char *argv[])
    {
        /* Call aos_http_io_initialize in main() to initialize global resources, such as the network and memory. */
        if (aos_http_io_initialize(NULL, 0) ! = AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. The implementation code is included in the APR library. aos_pool_t is equivalent to apr_pool_t. */
        aos_pool_t *pool;
        /* Create a new memory pool. The second parameter value is NULL, indicating that the pool does not inherit any other memory pool. */
        aos_pool_create(&pool, NULL);
        /* Create and initialize options. This parameter includes global configuration information such as endpoint, access_key_id, access_key_secret, is_cname, and curl. */
        oss_request_options_t *oss_client_options;
        /* The memory consumed by options is allocated by the memory pool and is released with the pool. */
        oss_client_options = oss_request_options_create(pool);
        /* Initialize oss_client_options. */
        init_options(oss_client_options);
        /* Initialize parameters. */
        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;
        /* List the objects whose names contain the specified prefix. */
        char *prefix = "fun/";
        char *nextMarker = "";
        /* Set the delimiter to forward slashes (/). */
        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");
        /* List all objects. */
        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);
        /* Release the memory pool after the request is complete. In this case, the memory allocated to various resources used for the request is released. */
        aos_pool_destroy(pool);
        /* Before you end the program, call aos_http_io_deinitialize to release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }