Updated on 2024-12-03 GMT+08:00

Copying a Versioning Object

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

You can call copy_object to pass the version ID (version_id) to copy a versioning object. Sample code is as follows:

static void test_copy_object_version()
{
    obs_status ret_status = OBS_STATUS_BUTT;
    char eTag[OBS_COMMON_LEN_256] = {0};
    int64_t lastModified;
    // 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 destination object information.
    obs_copy_destination_object_info objectinfo ={0};
    objectinfo.destination_bucket = target_bucket;
    objectinfo.destination_key = destinationKey;
    objectinfo.etag_return = eTag;
    objectinfo.etag_return_size = sizeof(eTag);
    objectinfo.last_modified_return = &lastModified;
    // Set response callback function.
    obs_response_handler responseHandler =
    { 
        &response_properties_callback,
        &response_complete_callback 
    };
    // Copy an object.
    copy_object(&option, key, version_id, &objectinfo, 1, NULL, NULL,&responseHandler,&ret_status);
    if (OBS_STATUS_OK == ret_status) {
        printf("test_copy_object  successfully. \n");
    }
    else
    {
        printf("test_copy_object failed(%s).\n", obs_get_status_name(ret_status));
    }
}