Updated on 2023-11-09 GMT+08:00

Setting Object Properties

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

You can set properties for an object when uploading it. Object properties include the object length, MIME type, MD5 value (for verification), storage class, and customized metadata. Object attributes can be uploaded in several modes (streaming upload, file-based upload, multipart upload, browser-based upload) or be configured when Copying an Object. Parameters to set object properties are in the obs_put_properties structure.

The following table describes object properties.

Property Name

Description

Default Value

Object length (content_length)

Indicates the object length. If the object length exceeds the stream or file length, the object will be truncated.

Actual length of the stream or file

Object MIME type (content_type)

Indicates the MIME type of the object, which defines the type and network code of the object as well as in which mode and coding will the browser read the object.

N/A

MD5 value of the object (md5)

Indicates the base64-encoded digest of the object data. It is provided for the OBS server to verify data integrity.

N/A

Storage class

Indicates the storage class of the object. Different storage classes meet different needs for storage performance and costs. The value defaults to be the same as the object's residing bucket and can be changed.

N/A

Customized metadata

Indicates the user-defined description of properties of the object uploaded to the bucket. It is used to facilitate the customized management on the object.

N/A

Setting the MIME Type for an Object

You can set the object MIME type by assigning a value to content_type in the obs_put_properties structure. The following uses file upload as an example to describe how to set the MIME type of an object:
static void test_put_object_from_file2()
{
    // Create and initialize option.
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.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.
    option.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // Initialize put_properties which can be used to set object properties.
    obs_put_properties put_properties;
    init_put_properties(&put_properties);
    // Set the MIME type.
    put_properties.content_type = "text/html";
    // Callback data
    put_file_object_callback_data data;
    memset(&data, 0, sizeof(put_file_object_callback_data));
    // Read the file to be uploaded to the callback data.
    data.infile = 0;
    data.noStatus = 1;
    content_length = read_bytes_from_file("<Uploaded filename>", &data);
    // Callback function
    obs_put_object_handler putobjectHandler =
    {
        { &response_properties_callback, &response_complete_callback },
        &put_object_data_callback
     };
    // Upload data streams.
    put_object(&option,"<object key>", content_length, &put_properties, 0, &putobjectHandler, &data);
    if (OBS_STATUS_OK == data.ret_status) {
        printf("put object from file successfully. \n");
    }
    else
    {
        printf("put object failed(%s).\n",  
               obs_get_status_name(data.ret_status));
    }
}

If this property is not specified, the SDK will automatically identify the MIME type according to the suffix of the uploaded object. For example, if the suffix of the object is .xml (.html), the object will be identified as an application/xml (text/html) file.

Setting the Storage Class for an Object

You can set the object storage class by assigning a value to storage_class in obs_bucket_context. Sample code:

static void test_put_object_from_file3()
{
    // Create and initialize option.
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.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.
    option.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // Set the storage class to Archive.
    option.bucket_options.storage_class = OBS_STORAGE_CLASS_GLACIER;
    // Initialize put_properties which can be used to set object properties.
    obs_put_properties put_properties;
    init_put_properties(&put_properties);
    // Callback data
    put_file_object_callback_data data;
    memset(&data, 0, sizeof(put_file_object_callback_data));
    // Read the file to be uploaded to the callback data.
    data.infile = 0;
    data.noStatus = 1;
    content_length = read_bytes_from_file("<Uploaded filename>", &data);
    // Callback function
    obs_put_object_handler putobjectHandler =
    {
        { &response_properties_callback, &response_complete_callback },
        &put_object_data_callback
    };
     // Upload data streams.
    put_object(&option,"<object key>", content_length, &put_properties, 0, &putobjectHandler, &data);
    if (OBS_STATUS_OK == data.ret_status) {
        printf("put object from file successfully. \n");
    }
    else
    {
        printf("put object failed(%s).\n",  
           obs_get_status_name(data.ret_status));
    }

}
  • The storage class of the objects in a bucket is the same as that of the bucket.
  • There are three object storage classes. Their meanings are the same as those described in Storage Class.
  • Before downloading an Archive object, you must restore it.

Customizing Metadata for an Object

You can set the object customized metadata by assigning a value to meta_data in obs_put_properties. Sample code:

static void test_put_object_from_file4()
{
    // Create and initialize option.
    obs_options option;
    init_obs_options(&option);
    option.bucket_options.host_name = "<your-endpoint>";
    option.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.
    option.bucket_options.access_key = getenv("ACCESS_KEY_ID");
    option.bucket_options.secret_access_key = getenv("SECRET_ACCESS_KEY");
    // Initialize put_properties which can be used to set object properties.
    obs_put_properties put_properties;
    init_put_properties(&put_properties);
    // Customize metadata.
    obs_name_value matadata;
    matadata.name ="property1";
    matadata.value ="property-value1";
    put_properties.meta_data = matadata;
    // Callback data
    put_file_object_callback_data data;
    memset(&data, 0, sizeof(put_file_object_callback_data));
    // Read the file to be uploaded to the callback data.
    data.infile = 0;
    data.noStatus = 1;
    content_length = read_bytes_from_file("<Uploaded filename>", &data);
    // Callback function
    obs_put_object_handler putobjectHandler =
    {
        { &response_properties_callback, &response_complete_callback },
        &put_object_data_callback
     };
     // Upload data streams.
    put_object(&option,"<object key>", content_length, &put_properties, 0, &putobjectHandler, &data);
    if (OBS_STATUS_OK == data.ret_status) {
        printf("put object from file successfully. \n");
    }
    else
    {
        printf("put object failed(%s).\n",  
               obs_get_status_name(data.ret_status));
    }
}
  • In the preceding sample code for customizing metadata for an object, a user has defined a metadata named property1 and whose value is property-value1.
  • An object can have multiple pieces of metadata. The total metadata size cannot exceed 8 KB.
  • You can obtain the customized metadata of an object by using get_object_metadata. For details, see Obtaining Object Properties.
  • When you use get_object to download an object, its customized metadata will also be downloaded.