Help Center/ Object Storage Service/ SDK Reference/ C/ Versioning Management/ Setting Versioning Status for a Bucket
Updated on 2024-04-29 GMT+08:00

Setting Versioning Status for a Bucket

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

You can call set_bucket_version_configuration to set the versioning status for a bucket. OBS supports two versioning statuses.

Versioning Status

Description

Value in OBS C SDK

Enabled

  1. OBS creates a unique version ID for each uploaded object. Namesake objects are not overwritten and are distinguished by their own version IDs.
  2. Objects can be downloaded by specifying the version ID. By default, the latest object is downloaded if no version ID is specified.
  3. Objects can be deleted by specifying the version ID. If an object is deleted with no version ID specified, the object will generate a delete marker with a unique version ID but is not physically deleted.
  4. Objects of the latest version in a bucket are returned by default after list_bucket_objects is called. You can call list_versions to list a bucket's objects with all version IDs.
  5. Except for delete markers, storage space occupied by objects with all version IDs is billed.

OBS_VERSION_STATUS_ENABLED

Suspended

  1. Noncurrent object versions are not affected.
  2. OBS creates version ID null to an uploaded object and the object will be overwritten after a namesake one is uploaded.
  3. Objects can be downloaded by specifying the version ID. By default, the latest object is downloaded if no version ID is specified.
  4. Objects can be deleted by version ID. If an object is deleted with no version ID specified, the object is only attached with a deletion mark and version ID null. Objects with version ID null are physically deleted.
  5. Except for delete markers, storage space occupied by objects with all version IDs is billed.

OBS_VERSION_STATUS_SUSPENDED

The following table describes the parameters involved in this API.

Parameter Description

Field

Type

Mandatory or Optional

Description

option

The context of the bucket. For details, see Configuring option.

Mandatory

Bucket parameter

version_status

char *

Mandatory

Indicates the multi-version status of a bucket: OBS_VERSION_STATUS_ENABLED, OBS_VERSION_STATUS_SUSPENDED.

handler

obs_response_handler *

Mandatory

Callback function

callback_data

void *

Optional

Callback data

static void test_set_bucket_version()
{
    obs_status ret_status = OBS_STATUS_BUTT;
    // 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 response callback function.
    obs_response_handler response_handler =
    { 
        0, &response_complete_callback
    };
    // Enable bucket versioning.
    set_bucket_version_configuration(&option, OBS_VERSION_STATUS_ENABLED, 
        &response_handler, &ret_status);
    if (OBS_STATUS_OK == ret_status) {
        printf("set bucket version successfully. \n");
    }
    else
    {
        printf("set bucket version failed(%s).\n", obs_get_status_name(ret_status));
    }
}