List files in the storage space
List files in the storage space
The BOS SDK allows users to list objects in the following two ways:
- Simple listing
- Advanced listing via parameters
In addition, users can also simulate folders while listing files.
Simple listing
To quickly and easily list the needed files, users can modify the bos_list_object_params_t structure through the bos_list_object method. This bos_list_object_params_t object contains the results of the listObject request, and users can retrieve all object descriptions via the getContents method of bos_list_object_params_t.
1 bos_pool_t *p = NULL;
2 bos_string_t bucket;
3 bos_request_options_t *options = NULL;
4 int is_cname = 0;
5 bos_table_t *resp_headers = NULL;
6 bos_status_t *s = NULL;
7 bos_list_object_params_t *params = NULL;
8 bos_list_object_content_t *content = NULL;
9 int size = 0;
10 char *key = NULL;
11 bos_pool_create(&p, NULL);
12 options = bos_request_options_create(p);
13 init_test_request_options(options, is_cname);
14 params = bos_create_list_object_params(p);
15 params->max_ret = 1;
16 params->truncated = 0;
17 bos_str_set(¶ms->prefix, "bos_test");
18 bos_str_set(&bucket, TEST_BUCKET_NAME);
19 s = bos_list_object(options, &bucket, params, &resp_headers);
20 //bos_list_for_each_entry(bos_list_object_content_t, content, ¶ms->object_list, node) {
21 // ++size;
22 // key = apr_psprintf(p, "%.*s", content->key.len, content->key.data);
23 //}
Note:
- By default, when a bucket contains more than 1,000 objects, only the first 1,000 will be returned. The is_truncated value in the result will be True, and next_marker will indicate the starting point for the subsequent fetch.
- To increase the number of objects returned, users can utilize the marker parameter to read objects in batches.
Advanced listing via parameters
Aside from simple listing, users can also implement various flexible queries by configuring the parameters in params. The configurable parameters for params include:
| Parameters | Function | |
|---|---|---|
| prefix | Restrict the returned object keys to those starting with a specific prefix | |
| delimiter | A character that groups object names. All objects with the specified prefix and the first occurrence of the delimiter in their names will be treated as a group element: CommonPrefixes. | |
| marker | Set the result to start returning from the first object in alphabetical order after the marker | |
| max_ret | Set the maximum number of objects to return, with a default and upper limit of 1,000. Specifying a value greater than 1,000 will default to 1,000. |
Note:
- If there is an object named with the prefix, when querying only with prefix, all returned keys will still include the object named with the Prefix. For details, see [Recursively List All Files in a Directory](#Simulate folder function).
- If there is an object named with the prefix, when querying with the combination of prefix and delimiter, there will be Null in all returned keys, and the key names do not contain the prefix. For details, see [View Files and Subdirectories in a Directory](#Simulate folder function).
The following are several examples to illustrate the method of listing via parameters:
Specify the maximum number of returned items
1params->max_ret = 100;
Return objects with a specified prefix
1bos_str_set(¶ms->prefix, "bos_test");
Return from after a specified object
1bos_str_set(¶ms->marker, params->next_marker.data);
The parameters available for calling in the parsing class of bos_list_object_params_t are:
| Parameters | Description |
|---|---|
| name | Bucket name |
| prefix | The matched objects from prefix to the first Delimiter character are returned as a group of elements |
| marker | Starting point of this query |
| max_ret | Maximum number of requests returned |
| is_truncated | Indicate whether all queries are returned; false - all results are returned this time; true - not all results are returned this time |
| object_list | A list of objects returned |
| +key | Object name |
| +last_modified | Last modification time of the object |
| +etag | The HTTP protocol entity tag of object. |
| +storage_class | Storage class of object |
| +size | The size of the object content (in bytes) |
| +owner_id | User ID of bucket owner |
| +owner_display_name | Name of bucket owner |
| common_prefix_list | The list of returned common prefixes |
Simulate folder function
In BOS storage, there is no concept of folders. All elements are stored as objects. However, when using data, BOS users often need to manage files in folders. Therefore, BOS provides the ability to create simulated folders, which essentially involves creating an object with a size of 0. This object can be uploaded and downloaded, but the console will display objects ending with “/” as folders.
By combining the delimiter and prefix parameters, users can simulate folder functionalities. The combined effect of delimiter and prefix is as follows:
If prefix is set to a folder name, files starting with the prefix can be listed, including all files and subfolders (directory) recursively listed under the folder. File names are displayed in contents. If delimiter is set to /, the return values only list the files and subfolders (directory) under the folder. The names of subfolders (directory) under the folder are returned in the CommonPrefixes. The files and folders recursively listed under the subfolders are not displayed.
The following are several application methods:
List all files in the bucket
When users need to get all files under a bucket, they can refer to [Pagination to Get All Objects](#Advanced listing via parameters)
Recursively list all files in a directory
You can obtain all files in the a directory by setting the Prefix parameter:
1void test_list_object_with_delimiter(CuTest *tc)
2{
3 bos_pool_t *p = NULL;
4 bos_string_t bucket;
5 bos_request_options_t *options = NULL;
6 int is_cname = 0;
7 bos_table_t *resp_headers = NULL;
8 bos_status_t *s = NULL;
9 bos_list_object_params_t *params = NULL;
10 bos_list_object_common_prefix_t *common_prefix = NULL;
11 int size = 0;
12 char *prefix = NULL;
13 bos_pool_create(&p, NULL);
14 options = bos_request_options_create(p);
15 init_test_request_options(options, is_cname);
16 params = bos_create_list_object_params(p);
17 params->max_ret = 5;
18 params->truncated = 0;
19 bos_str_set(¶ms->prefix, "bos_tmp");
20 bos_str_set(¶ms->delimiter, "/");
21 bos_str_set(&bucket, TEST_BUCKET_NAME);
22 s = bos_list_object(options, &bucket, params, &resp_headers);
23 bos_list_for_each_entry(bos_list_object_common_prefix_t, common_prefix, ¶ms->common_prefix_list, node) {
24 ++size;
25 prefix = apr_psprintf(p, "%.*s", common_prefix->prefix.len,
26 common_prefix->prefix.data);
27 }
28 bos_pool_destroy(p);
29 printf("test_list_object_with_delimiter ok\n");
30}
