本文介紹如何將源Bucket中的檔案(Object)複製到同一地區下相同或不同目標Bucket中。
注意事項
本文以華東1(杭州)外網Endpoint為例。如果您希望通過與OSS同地區的其他阿里雲產品訪問OSS,請使用內網Endpoint。關於OSS支援的Region與Endpoint的對應關係,請參見訪問網域名稱和資料中心。
本文以OSS網域名稱建立OSSClient為例。如果您希望通過自訂網域名、STS等方式建立OSSClient,請參見初始化。
針對不同檔案大小,需選擇CopyObject或者UploadPartCopy進行拷貝檔案。關於拷貝時的檔案大小限制說明,請參見CopyObject。
通過CopyObject拷貝檔案
說明
拷貝檔案時,您必須擁有源檔案的讀許可權及目標Bucket的讀寫權限。
不支援跨地區拷貝。例如不能將華東1(杭州)地區儲存空間中的檔案拷貝到華北1(青島)地區。
以下代碼用於通過CopyObject拷貝檔案:
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫源Bucket名稱。*/
const char *source_bucket_name = "yourSourceBucketName";
/* 填寫源Object的完整路徑。Object完整路徑中不能包含Bucket名稱。*/
const char *source_object_name = "yourSourceObjectName";
/* 填寫與源Bucket處於同一地區的目標Bucket名稱。*/
const char *dest_bucket_name = "yourDestBucketName";
/* 填寫目標Object的完整路徑。Object完整路徑中不能包含Bucket名稱。*/
const char *dest_object_name = "yourDestObjectName";
void init_options(oss_request_options_t *options)
{
options->config = oss_config_create(options->pool);
/* 用char*類型的字串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
/* 是否使用了CNAME。0表示不使用。*/
options->config->is_cname = 0;
/* 用於設定網路相關參數,比如逾時時間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char *argv[])
{
/* 在程式入口調用aos_http_io_initialize方法來初始化網路、記憶體等全域資源。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 用於記憶體管理的記憶體池(pool),等價於apr_pool_t。其實現代碼在apr庫中。*/
aos_pool_t *pool;
/* 重新建立一個記憶體池,第二個參數是NULL,表示沒有繼承其它記憶體池。*/
aos_pool_create(&pool, NULL);
/* 建立並初始化options,該參數包括endpoint、access_key_id、acces_key_secret、is_cname、curl等全域配置資訊。*/
oss_request_options_t *oss_client_options;
/* 在記憶體池中分配記憶體給options。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數。*/
aos_string_t source_bucket;
aos_string_t source_object;
aos_string_t dest_bucket;
aos_string_t dest_object;
aos_table_t *headers = NULL;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
aos_str_set(&source_bucket, source_bucket_name);
aos_str_set(&source_object, source_object_name);
aos_str_set(&dest_bucket, dest_bucket_name);
aos_str_set(&dest_object, dest_object_name);
headers = aos_table_make(pool, 0);
/* 拷貝檔案。*/
resp_status = oss_copy_object(oss_client_options, &source_bucket, &source_object, &dest_bucket, &dest_object, headers, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("copy object succeeded\n");
} else {
printf("copy object failed\n");
}
/* 釋放記憶體池,相當於釋放了請求過程中各資源分派的記憶體。*/
aos_pool_destroy(pool);
/* 釋放之前分配的全域資源。*/
aos_http_io_deinitialize();
return 0;
}
通過UploadPartCopy拷貝檔案
以下代碼用於通過UploadPartCopy分區拷貝檔案:
#include "oss_api.h"
#include "aos_http_io.h"
#include <sys/stat.h>
/* yourEndpoint填寫Bucket所在地區對應的Endpoint。以華東1(杭州)為例,Endpoint填寫為https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 填寫源Bucket名稱。*/
const char *source_bucket_name = "yourSourceBucketName";
/* 填寫源Object的完整路徑。Object完整路徑中不能包含Bucket名稱。*/
const char *source_object_name = "yourSourceObjectName";
/* 填寫與源Bucket處於同一地區的目標Bucket名稱。*/
const char *dest_bucket_name = "yourDestBucketName";
/* 填寫目標Object的完整路徑。Object完整路徑中不能包含Bucket名稱。*/
const char *dest_object_name = "yourDestObjectName";
void init_options(oss_request_options_t* options)
{
options->config = oss_config_create(options->pool);
/* 用char*類型的字串初始化aos_string_t類型。*/
aos_str_set(&options->config->endpoint, endpoint);
/* 從環境變數中擷取訪問憑證。運行本程式碼範例之前,請確保已設定環境變數OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。*/
aos_str_set(&options->config->access_key_id, getenv("OSS_ACCESS_KEY_ID"));
aos_str_set(&options->config->access_key_secret, getenv("OSS_ACCESS_KEY_SECRET"));
/* 是否使用了CNAME。0表示不使用。*/
options->config->is_cname = 0;
/* 用於設定網路相關參數,比如逾時時間等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
}
int main(int argc, char* argv[])
{
/* 在程式入口調用aos_http_io_initialize方法來初始化網路、記憶體等全域資源。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
}
/* 用於記憶體管理的記憶體池(pool),等價於apr_pool_t。其實現代碼在apr庫中。*/
aos_pool_t* pool;
/* 重新建立一個記憶體池,第二個參數是NULL,表示沒有繼承其它記憶體池。*/
aos_pool_create(&pool, NULL);
/* 建立並初始化options,該參數包括endpoint、access_key_id、acces_key_secret、is_cname、curl等全域配置資訊。*/
oss_request_options_t* oss_client_options;
/* 在記憶體池中分配記憶體給options。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的選項oss_client_options。*/
init_options(oss_client_options);
/* 初始化參數。*/
aos_string_t src_bucket;
aos_string_t src_object;
aos_string_t dest_bucket;
aos_string_t dest_object;
aos_string_t upload_id;
aos_table_t* headers = NULL;
aos_table_t* resp_headers = NULL;
aos_status_t* resp_status = NULL;
oss_list_upload_part_params_t* list_upload_part_params;
oss_upload_part_copy_params_t* upload_part_copy_params1;
oss_upload_part_copy_params_t* upload_part_copy_params2;
oss_list_part_content_t* part_content;
aos_list_t complete_part_list;
oss_complete_part_content_t* complete_content;
aos_table_t* list_part_resp_headers = NULL;
aos_table_t* complete_resp_headers = NULL;
int part1 = 1;
int part2 = 2;
int64_t range_start1 = 0;
int64_t range_end1 = 6000000;//not less than 5MB
int64_t range_start2 = 6000001;
int64_t range_end2;
int max_ret = 1000;
int64_t content_length = 0;
/* 擷取源Object的大小。*/
headers = aos_table_make(pool, 0);
aos_str_set(&src_bucket, source_bucket_name);
aos_str_set(&src_object, source_object_name);
resp_status = oss_head_object(oss_client_options, &src_bucket, &src_object, headers, &resp_headers);
if (aos_status_is_ok(resp_status)) {
char *content_length_str = (char*)apr_table_get(resp_headers, OSS_CONTENT_LENGTH);
if (content_length_str != NULL) {
content_length = atoll(content_length_str);
}
printf("head object succeeded\n");
}
else {
printf("head object failed\n");
}
/* 初始化分區。*/
headers = aos_table_make(pool, 0);
aos_str_set(&dest_bucket, dest_bucket_name);
aos_str_set(&dest_object, dest_object_name);
resp_status = oss_init_multipart_upload(oss_client_options, &dest_bucket, &dest_object, &upload_id, headers, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("init multipart upload succeeded, upload_id is %s\n", upload_id.data);
}
else {
printf("init multipart upload failed\n");
}
/* 拷貝第一個分區資料。*/
upload_part_copy_params1 = oss_create_upload_part_copy_params(pool);
aos_str_set(&upload_part_copy_params1->source_bucket, source_bucket_name);
aos_str_set(&upload_part_copy_params1->source_object, source_object_name);
aos_str_set(&upload_part_copy_params1->dest_bucket, dest_bucket_name);
aos_str_set(&upload_part_copy_params1->dest_object, dest_object_name);
aos_str_set(&upload_part_copy_params1->upload_id, upload_id.data);
upload_part_copy_params1->part_num = part1;
upload_part_copy_params1->range_start = range_start1;
upload_part_copy_params1->range_end = range_end1;
headers = aos_table_make(pool, 0);
resp_status = oss_upload_part_copy(oss_client_options, upload_part_copy_params1, headers, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("upload part 1 copy succeeded\n");
}
else {
printf("upload part 1 copy failed\n");
}
/* 拷貝第二個分區資料。*/
range_end2 = content_length - 1;
upload_part_copy_params2 = oss_create_upload_part_copy_params(pool);
aos_str_set(&upload_part_copy_params2->source_bucket, source_bucket_name);
aos_str_set(&upload_part_copy_params2->source_object, source_object_name);
aos_str_set(&upload_part_copy_params2->dest_bucket, dest_bucket_name);
aos_str_set(&upload_part_copy_params2->dest_object, dest_object_name);
aos_str_set(&upload_part_copy_params2->upload_id, upload_id.data);
upload_part_copy_params2->part_num = part2;
upload_part_copy_params2->range_start = range_start2;
upload_part_copy_params2->range_end = range_end2;
headers = aos_table_make(pool, 0);
resp_status = oss_upload_part_copy(oss_client_options, upload_part_copy_params2, headers, &resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("upload part 2 copy succeeded\n");
}
else {
printf("upload part 2 copy failed\n");
}
/* 列出分區。*/
headers = aos_table_make(pool, 0);
list_upload_part_params = oss_create_list_upload_part_params(pool);
list_upload_part_params->max_ret = max_ret;
aos_list_init(&complete_part_list);
resp_status = oss_list_upload_part(oss_client_options, &dest_bucket, &dest_object, &upload_id, list_upload_part_params, &list_part_resp_headers);
aos_list_for_each_entry(oss_list_part_content_t, part_content, &list_upload_part_params->part_list, node) {
complete_content = oss_create_complete_part_content(pool);
aos_str_set(&complete_content->part_number, part_content->part_number.data);
aos_str_set(&complete_content->etag, part_content->etag.data);
aos_list_add_tail(&complete_content->node, &complete_part_list);
}
/* 完成分區拷貝。*/
resp_status = oss_complete_multipart_upload(oss_client_options, &dest_bucket, &dest_object, &upload_id, &complete_part_list, headers, &complete_resp_headers);
if (aos_status_is_ok(resp_status)) {
printf("complete multipart upload succeeded\n");
}
else {
printf("complete multipart upload failed\n");
}
/* 釋放記憶體池,相當於釋放了請求過程中各資源分派的記憶體。*/
aos_pool_destroy(pool);
/* 釋放之前分配的全域資源。*/
aos_http_io_deinitialize();
return 0;
}
相關文檔
拷貝小檔案
關於拷貝小檔案的API介面說明,請參見CopyObject。
拷貝大檔案
關於拷貝大檔案的API介面說明,請參見UploadPartCopy。