Get and update file meta 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, allowing users to customize the HTTP headers of objects during uploads. Common HTTP headers are described as follows:
| 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 //Initialize meta
3ObjectMetaData meta;
4 // Set ContentType
5meta.set_content_type("application/json");
6 //Set cache-control
7meta.set_cache_control("no-cache");
8 //Set x-bce-storage-class
9meta.set_storage_class("STANDARD");
10ret = client.upload_file(bucketName, objectKey, content, meta);
11...
- 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
2meta.set_user_meta("name", "my-data");
3
4 // Upload Object
5ret = client.upload_file(bucketName, objectKey, file_name, meta);
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 the storage class of the object
The storage class attributes of an object are divided into STANDARD (standard storage), STANDARD_IA (infrequent access storage), and COLD (cold storage). This can be achieved through the following code:
1int getObjectStorageClass(){
2 HeadObjectRequest request(bucketName, objectKey);
3 HeadObjectResponse response;
4 int ret = client.head_object(request, &response);
5 if (ret != 0) {
6 return ret;
7 }
8 ObjectMetaData& meta = response.meta();
9 std::string storageClass = meta.storage_class();
10 std::cout << storageClass << std::endl;
11 return 0;
12}
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.
1void getObjectMeta(Client& client, const std::string& bucketName, const std::string& objectKey, ObjectMetaData* objectMetaData) {
2
3 HeadObjectRequest request(bucketName, objectKey);
4 HeadObjectResponse rsponse;
5
6 request.set_meta(newObjectMetaData);
7
8 int ret = client.head_object(request, &rsponse);
9 if (ret != 0) {
10 return;
11 }
12 objectMetaData = &rsponse.meta();
13
14}
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.
1void setObjectMeta(Client& client, const std::string& bucketName, const std::string& objectKey, ObjectMetaData* newObjectMetaData) {
2
3 CopyObjectRequest request(bucketName, objectKey, bucketName, objectKey);
4 //Set new ObjectMetaData
5 request.set_meta(newObjectMetaData);
6 // Copy Object
7 CopyObjectResponse copyObjectResponse;
8 int ret = client.copy_object(request, ©ObjectResponse);
9 if (ret != 0) {
10 return;
11 }
12 // Print results
13 std::cout << "ETag: " << copyObjectResponse.etag() << " LastModified: " << copyObjectResponse.last_modified() << std::endl;
14}
