Get and Update Metadata Information
File meta information
Set file meta information
Object metadata refers to the attributes of files provided by users when uploading to BOS. It is mainly divided into two categories: standard HTTP attribute settings (HTTP headers) and user-defined metadata.
- Set Http header of object
The BOS C SDK interacts with the background HTTP API, enabling users to define the HTTP headers of objects during file uploads. The following describes commonly used HTTP headers:
| Name | Description | Default value |
|---|---|---|
| Content-MD5 | File data verification: After setting, BOS will enable file content MD5 verification, compare the MD5 you provide with the MD5 of the file, and throw an error if they are inconsistent | None |
| Content-Type | File MIME: This defines the file type and web page encoding, determining how the browser reads the file. If unspecified, BOS generates it based on the file's extension. If the file lacks an extension, a default value will be applied. | application/octet-stream |
| Content-Disposition | Indicate how the MIME user agent displays the attached file, whether to open or download it, and the file name | None |
| Content-Length | The length of the uploaded file. If it exceeds the length of the stream/file, it will be truncated; if it is insufficient, it will be the actual value | Stream/file duration |
| Expires | Cache expiration time | None |
| Cache-Control | Specify the caching behavior of the web page when the object is downloaded | None |
Reference code is as follows:
1...
2 bos_table_t *headers = bos_table_make(p, 2);
3 apr_table_add(headers, "x-bce-metadata-directive", "replace");
4 apr_table_add(headers, "x-bce-storage-class", "STANDARD_IA");
5...
- User-defined meta information
BOS supports user-defined metadata for describing objects. Example usage is shown in the following code:
1// Set the value of custom metadata name to my-data
2 apr_table_add(headers, "name", "my-data");
Prompt:
- In the above code, the user has customized a metadata with the name "name" and value "my-data".
- When users download this object, this metadata can also be obtained.
- An object may have multiple similar parameters, but the total size of all user meta must not exceed 2KB.
Get only ObjectMetaData
Use the head_object method to retrieve only the ObjectMetaData, without accessing the object entity.
The parameters available for calling in the ObjectMetaData parsing class are:
| Parameters | Description |
|---|---|
| content_type | Object type |
| content_length | Object size |
| content_md5 | Object MD5 |
| etag | The HTTP protocol entity tag of object |
| storage_class | Storage class of the object |
| user_meta | If userMetadata custom meta is specified in PutObject, this item will be returned |
| expires | Cache expiration time when the object is downloaded |
| content_disposition | Set whether the browser downloads the file; available values are inline, attachment; filename="download.txt" |
| cache_control | Cache settings for downloading the object. Common values include private, no-cache, max-age, must-revalidate |
| content_range | Range of data returned to the object when the range is set up |
Get and update file meta information
File metadata, or object metadata, describes the attributes of files uploaded to BOS. It includes two types: HTTP standard attributes (HTTP headers) and user-defined metadata.
1 bos_pool_t *p = NULL;
2 int is_cname = 0;
3 bos_status_t *s = NULL;
4 bos_request_options_t *options = NULL;
5 bos_string_t bucket;
6 bos_string_t object;
7 bos_table_t *resp_headers = NULL;
8 //Create memory pool
9 bos_pool_create(&p, NULL);
10 //Initialize request options
11 options = bos_request_options_create(p);
12 init_test_request_options(options, is_cname);
13 bos_str_set(&bucket, TEST_BUCKET_NAME);
14 //Get object metadata
15 bos_str_set(&object, TEST_OBJECT_NAME1);
16 s = bos_head_object(options, &bucket, &object, NULL, &resp_headers);
17 print_headers(resp_headers);
18 if (bos_status_is_ok(s)) {
19 printf("head object succeeded\n");
20 } else {
21 printf("head object failed\n");
22 }
23 //Destroy memory pool
24 bos_pool_destroy(p);
Modify file meta information
BOS modifies object metadata by copying the object. When copying, set the destination bucket and object as the source bucket and object, then apply the new metadata. If new metadata is not specified, an error will occur.
1 bos_pool_t *p = NULL;
2 int is_cname = 0;
3 bos_status_t *s = NULL;
4 bos_request_options_t *options = NULL;
5 bos_string_t bucket;
6 bos_string_t object;
7 bos_string_t src_bucket;
8 bos_string_t src_object;
9 bos_string_t src_endpoint;
10 bos_table_t *resp_headers = NULL;
11 //Create memory pool
12 bos_pool_create(&p, NULL);
13 //Initialize request options
14 options = bos_request_options_create(p);
15 init_test_request_options(options, is_cname);
16 bos_str_set(&bucket, TEST_BUCKET_NAME);
17 //Set object replication
18 bos_str_set(&object, TEST_OBJECT_NAME2);
19 bos_str_set(&src_bucket, TEST_BUCKET_NAME);
20 bos_str_set(&src_endpoint, TEST_BOS_ENDPOINT);
21 bos_str_set(&src_object, TEST_OBJECT_NAME1);
22 bos_table_t *headers = bos_table_make(p, 2);
23 bos_copy_object_params_t *params = NULL;
24 params = bos_create_copy_object_params(p);
25 s = bos_copy_object(options, &src_bucket, &src_object, &bucket, &object, headers, params, &resp_headers);
26 if (bos_status_is_ok(s)) {
27 printf("put object copy succeeded\n");
28 } else {
29 printf("put object copy failed\n");
30 }
31 //Destroy memory pool
32 bos_pool_destroy(p);
