Updated on 2026-08-03 GMT+08:00

Listing Objects (SDK for C)

If you have any questions during development, post them on the Issues page of GitHub.

This example shows how to list all objects in a bucket.

Sample code:

static void test_list_bucket_objects()
{
    // Create and initialize options, including the access domain name (host_name), access keys (access_key_id and access_key_secret), bucket name (bucket_name), and bucket storage class (storage_class).
    obs_options options;
    init_obs_options(&options);
    // Enter the endpoint corresponding to the bucket for host_name. CN-Hong Kong is used here as an example. Replace it with the one currently in use.
    options.bucket_options.host_name = "obs.ap-southeast-1.myhuaweicloud.com";
    options.bucket_options.bucket_name = "<Your bucketname>";


// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY.
    // Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
    options.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    options.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");

    // Set response callback function.
    obs_list_objects_handler list_bucket_objects_handler =
    {
        { &response_properties_callback, &list_objects_complete_callback },
        &list_objects_callback
    };
    
    // Customize callback data.
    list_bucket_callback_data data;
    memset(&data, 0, sizeof(list_bucket_callback_data)); 
    // List objects.
    list_bucket_objects(&options, "<prefix>", "<marker>", "<delimiter>", "<maxkeys>", &list_bucket_objects_handler, &data); 
    if (OBS_STATUS_OK == data.ret_status) {
        printf("list bucket objects successfully. \n");
    }
    else
    {
        printf("list bucket objects failed(%s).\n", 
            obs_get_status_name(data.ret_status));
    }
}