This topic describes how to list all objects in a bucket, a specified number of objects in a bucket, and objects whose names contain a specified prefix in a bucket.

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"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
const char *access_key_id = "yourAccessKeyId";
const char *access_key_secret = "yourAccessKeySecret";
/* Specify the bucket name. Example: examplebucket. */
const char *bucket_name = "examplebucket";
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. The value 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Configure network parameters such as the timeout period. */
    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 networks and memory. */
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
    aos_pool_t *pool;
    /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
    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 the memory resources in the memory pool to the options. */
    oss_client_options = oss_request_options_create(pool);
    /* Initialize oss_client_options. */
    init_options(oss_client_options);
    /* Initialize the 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. This operation releases the memory resources allocated for the request. */
    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 value of the truncated parameter in the response is true. Additionally, the next_marker parameter is returned as the start point for the next read. To list more than 1,000 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

The following code provides an example on how to list a specified number of objects:

#include "oss_api.h"
#include "aos_http_io.h"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
const char *access_key_id = "yourAccessKeyId";
const char *access_key_secret = "yourAccessKeySecret";
/* Specify the bucket name. Example: examplebucket. */
const char *bucket_name = "examplebucket";
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. The value 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Configure network parameters such as the timeout period. */
    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 networks and memory. */
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
    aos_pool_t *pool;
    /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
    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 the 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);
    /* Specify the maximum number of objects that you want to return. The maximum number cannot exceed 1,000. The default value is 100. */
    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 exit the program, call aos_http_io_deinitialize to release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

List objects whose names contain a specified prefix

The following code provides an example on how to list objects whose names contain a specified prefix:

#include "oss_api.h"
#include "aos_http_io.h"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
const char *access_key_id = "yourAccessKeyId";
const char *access_key_secret = "yourAccessKeySecret";
/* Specify the bucket name. Example: examplebucket. */
const char *bucket_name = "examplebucket";
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. The value 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Configure network parameters such as the timeout period. */
    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 networks and memory. */
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
    aos_pool_t *pool;
    /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
    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 the 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 exit the program, call aos_http_io_deinitialize to release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

List the objects whose names are alphabetically after the specified marker

You can configure the marker parameter to specify the name of the object after which the list operation starts. The following code provides an example on how to list objects whose names are alphabetically after the object specified by marker:

#include "oss_api.h"
#include "aos_http_io.h"
/* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
const char *endpoint = "yourEndpoint";
/* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
const char *access_key_id = "yourAccessKeyId";
const char *access_key_secret = "yourAccessKeySecret";
/* Specify the bucket name. Example: examplebucket. */
const char *bucket_name = "examplebucket";
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. The value 0 indicates that CNAME is not used. */
    options->config->is_cname = 0;
    /* Configure network parameters such as the timeout period. */
    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 networks and memory. */
    if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
        exit(1);
    }
    /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
    aos_pool_t *pool;
    /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
    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 the 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 the objects whose names are alphabetically after the 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 exit the program, call aos_http_io_deinitialize to release the allocated global resources. */
    aos_http_io_deinitialize();
    return 0;
}

List objects by directory

OSS uses a flat structure to store objects. A directory is an object whose size is 0 and whose name ends with a forward slash (/). You can upload and download this object. By default, objects whose names end with a forward slash (/) are displayed as directories in the OSS console.

You can specify the delimiter and prefix parameters to list objects by directory.

  • If you set prefix to a directory name in the request, objects and subdirectories whose names contain the prefix are listed.
  • If you also set delimiter to a forward slash (/) in the request, the objects and subdirectories whose names start with the specified prefix in the directory are listed. Each subdirectory is listed as a single result element in commonPrefixes. The objects and directories in these subdirectories are not listed.

Example: The following four objects are stored in a bucket: oss.jpg, fun/test.jpg, fun/movie/001.avi, and fun/movie/007.avi. The forward slash (/) is specified as the directory delimiter. The following code provides examples on how to list objects by directory.

  • List all objects in a bucket

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

    #include "oss_api.h"
    #include "aos_http_io.h"
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    const char *endpoint = "yourEndpoint";
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    const char *access_key_id = "yourAccessKeyId";
    const char *access_key_secret = "yourAccessKeySecret";
    /* Specify the bucket name. Example: examplebucket. */
    const char *bucket_name = "examplebucket";
    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. The value 0 indicates that CNAME is not used. */
        options->config->is_cname = 0;
        /* Configure network parameters such as the timeout period. */
        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 networks and memory. */
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
        aos_pool_t *pool;
        /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
        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 the memory resources in the memory pool to the options. */
        oss_client_options = oss_request_options_create(pool);
        /* Initialize oss_client_options. */
        init_options(oss_client_options);
        /* Initialize the 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. This operation releases the memory resources allocated for the request. */
        aos_pool_destroy(pool);
        /* Release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }

    The following result is returned:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    oss.jpg
    
    CommonPrefixes:
  • List all objects in the specified directory

    The following code provides an example on how to list all objects in the specified directory:

    #include "oss_api.h"
    #include "aos_http_io.h"
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    const char *endpoint = "yourEndpoint";
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    const char *access_key_id = "yourAccessKeyId";
    const char *access_key_secret = "yourAccessKeySecret";
    /* Specify the bucket name. Example: examplebucket. */
    const char *bucket_name = "examplebucket";
    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. The value 0 indicates that CNAME is not used. */
        options->config->is_cname = 0;
        /* Configure network parameters such as the timeout period. */
        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 networks and memory. */
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
        aos_pool_t *pool;
        /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
        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 the 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 exit the program, call aos_http_io_deinitialize to release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }

    The following result is returned:

    Objects:
    fun/movie/001.avi
    fun/movie/007.avi
    fun/test.jpg
    
    CommonPrefixes:
  • List the objects and the subdirectories in the specified directory

    The following code provides an example on how to list objects and subdirectories in a specified directory:

    #include "oss_api.h"
    #include "aos_http_io.h"
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    const char *endpoint = "yourEndpoint";
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    const char *access_key_id = "yourAccessKeyId";
    const char *access_key_secret = "yourAccessKeySecret";
    /* Specify the bucket name. Example: examplebucket. */
    const char *bucket_name = "examplebucket";
    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. The value 0 indicates that CNAME is not used. */
        options->config->is_cname = 0;
        /* Configure network parameters such as the timeout period. */
        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 networks and memory. */
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
        aos_pool_t *pool;
        /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
        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 the 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 exit the program, call aos_http_io_deinitialize to release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }

    The following result is returned:

    Objects:
    fun/test.jpg
    
    CommonPrefixes:
    fun/movie/
  • List sizes of objects in a specified directory

    The following code provides an example on how tolist the sizes of objects in a specified directory:

    #include "oss_api.h"
    #include "aos_http_io.h"
    /* Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    const char *endpoint = "yourEndpoint";
    /* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. */
    const char *access_key_id = "yourAccessKeyId";
    const char *access_key_secret = "yourAccessKeySecret";
    /* Specify the bucket name. Example: examplebucket. */
    const char *bucket_name = "examplebucket";
    
    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. The value 0 indicates that CNAME is not used. */
        options->config->is_cname = 0;
        /* Configure network parameters such as the timeout period. */
        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 networks and memory. */
        if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
            exit(1);
        }
        /* Create a memory pool to manage memory. aos_pool_t is equivalent to apr_pool_t. The code used to create a memory pool is included in the APR library. */
        aos_pool_t *pool;
        /* Create a memory pool. The value of the second parameter is NULL. This value indicates that the pool does not inherit other memory pools. */
        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 the 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 exit the program, call aos_http_io_deinitialize to release the allocated global resources. */
        aos_http_io_deinitialize();
        return 0;
    }

References