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

Performing a Multipart Copy

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

As a special case of multipart upload, multipart copy implements multipart upload by copying the whole or part of an object in a bucket. You can copy parts by using copy_part. The following table describes the parameters.

Field

Type

Mandatory or Optional

Description

option

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

Mandatory

Bucket parameter

key

char *

Mandatory

Object name

object_info

obs_copy_destination_object_info *

Mandatory

Indicates a multipart upload.

object_info->destination_bucket

char *

Mandatory

Bucket where the target object is located

object_info->destination_key

char *

Mandatory

Target object name

object_info->last_modified_return

int64_t *

Mandatory

Latest time when the object was modified

object_info->etag_return_size

int

Mandatory

Cache size of ETag

object_info->etag_return

char *

Mandatory

Cache of ETag

copypart

obs_upload_part_info *

Mandatory

Information about the part to be uploaded

copypart->part_number

unsigned int

Mandatory

ID of the part to be uploaded

copypart->upload_id

char *

Mandatory

Task ID of a multipart upload

put_properties

obs_put_properties*

Optional

Properties of the uploaded object

encryption_params

server_side_encryption_params *

Optional

Configuring server-side encryption

handler

obs_response_handler *

Mandatory

Callback function

callback_data

void *

Optional

Callback data

Sample code:

static void test_copy_part()
{
    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");

    // SSE-KMS encryption
    server_side_encryption_params encryption_params;
    memset(&encryption_params, 0, sizeof(server_side_encryption_params));
    // ETag value returned after the part is copied
    char etagreturn[256] ={0};
    
    char *key= "<Source object key>"
    // Define the information of copied parts.
    obs_copy_destination_object_info object_info;
    memset(&object_info, 0, sizeof(obs_copy_destination_object_info));
    object_info.destination_bucket = "<Your destination bucketname>";
    object_info.destination_key = "<Your destination object key>";
    object_info.etag_return = etagreturn;
    object_info.etag_return_size = 256;
    obs_upload_part_info copypart;
    memset(&copypart, 0, sizeof(obs_upload_part_info));
     // Set response callback function.
    obs_response_handler responseHandler =
    { 
        &response_properties_callback,
        &response_complete_callback
    };
    // Copy the first part.
    copypart.part_number = "1";
    copypart.upload_id = "<upload id>";
    copy_part(&option, key, &object_info, &copypart, 
                  &putProperties,&encryption_params,&responseHandler, &ret_status);
    if (OBS_STATUS_OK == ret_status) {
        printf(" copy part 1 successfully. \n");
    }
    else
    {
        printf("copy part 1 failed(%s).\n", obs_get_status_name(ret_status));
    }
    // Copy the second part.
    copypart.part_number = "2";
    copypart.upload_id = "<upload id>";
    copy_part(&option, key, &object_info, &copypart, 
                  &putProperties,&encryption_params,&responseHandler, &ret_status);
    if (ret_status == OBS_STATUS_OK) {
        printf(" copy part 2 successfully. \n");
    }
    else
    {
        printf("copy part 2 failed(%s).\n", obs_get_status_name(ret_status));
    }
}