API | 操作名 | 操作描述 |
设置对象复制(修改属性) | 复制文件到目标路径 | |
删除单个对象 | 在存储桶中删除指定对象 |
API | 操作名 | 操作描述 |
复制分块 | 将其他对象复制为一个分块 |
cos_status_t *cos_copy_object(const cos_request_options_t *options,const cos_string_t *src_bucket,const cos_string_t *src_object,const cos_string_t *src_endpoint,const cos_string_t *dest_bucket,const cos_string_t *dest_object,cos_table_t *headers,cos_copy_object_params_t *copy_object_param,cos_table_t **resp_headers);
参数名称 | 参数描述 | 类型 |
options | COS 请求选项 | Struct |
src_bucket | 源存储桶 | String |
src_object | 源对象名称 | String |
src_endpoint | 源对象访问域名信息 | String |
dest_bucket | 目的存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式 | String |
dest_object | 目的 Object 名称 | String |
headers | COS 请求附加头域 | Struct |
copy_object_param | Put Object Copy 操作参数 | Struct |
etag | 返回文件的 MD5 算法校验值 | String |
last_modify | 返回文件最后修改时间,GMT 格式 | String |
resp_headers | 返回 HTTP 响应消息的头域 | Struct |
返回结果 | 描述 | 类型 |
code | 错误码 | Int |
error_code | 错误码内容 | String |
error_msg | 错误码描述 | String |
req_id | 请求消息 ID | String |
#include "cos_http_io.h"#include "cos_api.h"#include "cos_log.h"// endpoint 是 COS 访问域名信息,详情请参见 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1 文档static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";// 开发者拥有的项目身份ID/密钥,可在 https://console.tencentcloud.com/cam/capi 页面获取static char *TEST_ACCESS_KEY_ID; //your secret_idstatic char *TEST_ACCESS_KEY_SECRET; //your secret_key// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.tencentcloud.com/cam/capi 页面获取static char TEST_APPID[] = "<APPID>"; //your appid//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.tencentcloud.com/cos5/bucket 查看static char TEST_BUCKET_NAME[] = "<bucketname-appid>";// 对象键,对象(Object)在存储桶(Bucket)中的唯一标识。有关对象与对象键的进一步说明,请参见 https://www.tencentcloud.com/document/product/436/13324?from_cn_redirect=1 文档static char TEST_OBJECT_NAME1[] = "1.txt";static char TEST_OBJECT_NAME2[] = "test2.dat";void init_test_config(cos_config_t *config, int is_cname){cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);cos_str_set(&config->appid, TEST_APPID);config->is_cname = is_cname;}void init_test_request_options(cos_request_options_t *options, int is_cname){options->config = cos_config_create(options->pool);init_test_config(options->config, is_cname);options->ctl = cos_http_controller_create(options->pool, 0);}void test_copy(){cos_pool_t *p = NULL;int is_cname = 0;cos_status_t *s = NULL;cos_request_options_t *options = NULL;cos_string_t bucket;cos_string_t object;cos_string_t src_bucket;cos_string_t src_object;cos_string_t src_endpoint;cos_table_t *resp_headers = NULL;//创建内存池cos_pool_create(&p, NULL);//初始化请求选项options = cos_request_options_create(p);init_test_request_options(options, is_cname);cos_str_set(&bucket, TEST_BUCKET_NAME);//设置对象复制cos_str_set(&object, TEST_OBJECT_NAME2);cos_str_set(&src_bucket, TEST_BUCKET_NAME);cos_str_set(&src_endpoint, TEST_COS_ENDPOINT);cos_str_set(&src_object, TEST_OBJECT_NAME1);cos_copy_object_params_t *params = NULL;params = cos_create_copy_object_params(p);s = cos_copy_object(options, &src_bucket, &src_object, &src_endpoint, &bucket, &object, NULL, params, &resp_headers);if (cos_status_is_ok(s)) {printf("put object copy succeeded\\n");} else {printf("put object copy failed\\n");}//销毁内存池cos_pool_destroy(p);}int main(int argc, char *argv[]){// 通过环境变量获取 SECRETID 和 SECRETKEYTEST_ACCESS_KEY_ID = getenv("COS_SECRETID");TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");if (cos_http_io_initialize(NULL, 0) != COSE_OK) {exit(1);}//set log level, default COS_LOG_WARNcos_log_set_level(COS_LOG_WARN);//set log output, default stderrcos_log_set_output(NULL);test_copy();cos_http_io_deinitialize();return 0;}
#include "cos_http_io.h"#include "cos_api.h"#include "cos_log.h"// endpoint 是 COS 访问域名信息,详情请参见 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1 文档static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";// 开发者拥有的项目身份ID/密钥,可在 https://console.tencentcloud.com/cam/capi 页面获取static char *TEST_ACCESS_KEY_ID; //your secret_idstatic char *TEST_ACCESS_KEY_SECRET; //your secret_key// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.tencentcloud.com/cam/capi 页面获取static char TEST_APPID[] = "<APPID>"; //your appid//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.tencentcloud.com/cos5/bucket 查看static char TEST_BUCKET_NAME[] = "<bucketname-appid>";// 对象键,对象(Object)在存储桶(Bucket)中的唯一标识。有关对象与对象键的进一步说明,请参见 https://www.tencentcloud.com/document/product/436/13324?from_cn_redirect=1 文档static char TEST_OBJECT_NAME1[] = "1.txt";void log_status(cos_status_t *s){cos_warn_log("status->code: %d", s->code);if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);}void init_test_config(cos_config_t *config, int is_cname){cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);cos_str_set(&config->appid, TEST_APPID);config->is_cname = is_cname;}void init_test_request_options(cos_request_options_t *options, int is_cname){options->config = cos_config_create(options->pool);init_test_config(options->config, is_cname);options->ctl = cos_http_controller_create(options->pool, 0);}void test_modify_storage_class(){cos_pool_t *p = NULL;int is_cname = 0;cos_status_t *s = NULL;cos_request_options_t *options = NULL;cos_string_t bucket;cos_string_t object;cos_string_t src_bucket;cos_string_t src_object;cos_string_t src_endpoint;cos_table_t *resp_headers = NULL;cos_pool_create(&p, NULL);options = cos_request_options_create(p);init_test_request_options(options, is_cname);cos_str_set(&bucket, TEST_BUCKET_NAME);cos_str_set(&object, TEST_OBJECT_NAME1);cos_str_set(&src_bucket, TEST_BUCKET_NAME);cos_str_set(&src_endpoint, TEST_COS_ENDPOINT);cos_str_set(&src_object, TEST_OBJECT_NAME1);// 设置x-cos-metadata-directive和x-cos-storage-class头域(替换为自己要更改的存储类型)cos_table_t *headers = cos_table_make(p, 2);apr_table_add(headers, "x-cos-metadata-directive", "Replaced");// 存储类型包括NTELLIGENT_TIERING,MAZ_INTELLIGENT_TIERING,STANDARD_IA,ARCHIVE,DEEP_ARCHIVEapr_table_add(headers, "x-cos-storage-class", "ARCHIVE");cos_copy_object_params_t *params = NULL;params = cos_create_copy_object_params(p);s = cos_copy_object(options, &src_bucket, &src_object, &src_endpoint, &bucket, &object, headers, params, &resp_headers);log_status(s);cos_pool_destroy(p);}int main(int argc, char *argv[]){// 通过环境变量获取 SECRETID 和 SECRETKEYTEST_ACCESS_KEY_ID = getenv("COS_SECRETID");TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");if (cos_http_io_initialize(NULL, 0) != COSE_OK) {exit(1);}//set log level, default COS_LOG_WARNcos_log_set_level(COS_LOG_WARN);//set log output, default stderrcos_log_set_output(NULL);test_modify_storage_class();cos_http_io_deinitialize();return 0;}
cos_status_t *cos_upload_part_copy(const cos_request_options_t *options,cos_upload_part_copy_params_t *params,cos_table_t *headers,cos_table_t **resp_headers);
参数名称 | 参数描述 | 类型 |
options | COS 请求选项 | Struct |
params | 复制分块参数信息 | Struct |
copy_source | 源文件路径 | String |
dest_bucket | 目的 存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式 | String |
dest_object | 目的 Object 名称 | String |
upload_id | 上传任务编号 | String |
part_num | 分块编号 | Int |
range_start | 源文件起始偏移 | Int |
range_end | 源文件终止偏移 | Int |
rsp_content | 复制分块结果信息 | Struct |
etag | 返回文件的 MD5 算法校验值 | String |
last_modify | 返回文件最后修改时间,GMT 格式 | String |
resp_headers | 返回 HTTP 响应消息的头域 | Struct |
返回结果 | 描述 | 类型 |
code | 错误码 | Int |
error_code | 错误码内容 | String |
error_msg | 错误码描述 | String |
req_id | 请求消息 ID | String |
#include "cos_http_io.h"#include "cos_api.h"#include "cos_log.h"#include <sys/stat.h>// endpoint 是 COS 访问域名信息,详情请参见 https://www.tencentcloud.com/document/product/436/6224?from_cn_redirect=1 文档static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";// 开发者拥有的项目身份ID/密钥,可在 https://console.tencentcloud.com/cam/capi 页面获取static char *TEST_ACCESS_KEY_ID; //your secret_idstatic char *TEST_ACCESS_KEY_SECRET; //your secret_key// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.tencentcloud.com/cam/capi 页面获取static char TEST_APPID[] = "<APPID>"; //your appid//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.tencentcloud.com/cos5/bucket 查看static char TEST_BUCKET_NAME[] = "<bucketname-appid>";void log_status(cos_status_t *s){cos_warn_log("status->code: %d", s->code);if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);}void init_test_config(cos_config_t *config, int is_cname){cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);cos_str_set(&config->appid, TEST_APPID);config->is_cname = is_cname;}void init_test_request_options(cos_request_options_t *options, int is_cname){options->config = cos_config_create(options->pool);init_test_config(options->config, is_cname);options->ctl = cos_http_controller_create(options->pool, 0);}void make_rand_string(cos_pool_t *p, int len, cos_string_t *data){char *str = NULL;int i = 0;str = (char *)cos_palloc(p, len + 1);for ( ; i < len; i++) {str[i] = 'a' + rand() % 32;}str[len] = '\\0';cos_str_set(data, str);}unsigned long get_file_size(const char *file_path){unsigned long filesize = -1;struct stat statbuff;if(stat(file_path, &statbuff) < 0){return filesize;} else {filesize = statbuff.st_size;}return filesize;}void test_part_copy(){cos_pool_t *p = NULL;cos_request_options_t *options = NULL;cos_string_t bucket;cos_string_t object;cos_string_t file;int is_cname = 0;cos_string_t upload_id;cos_list_upload_part_params_t *list_upload_part_params = NULL;cos_upload_part_copy_params_t *upload_part_copy_params1 = NULL;cos_upload_part_copy_params_t *upload_part_copy_params2 = NULL;cos_table_t *headers = NULL;cos_table_t *query_params = NULL;cos_table_t *resp_headers = NULL;cos_table_t *list_part_resp_headers = NULL;cos_list_t complete_part_list;cos_list_part_content_t *part_content = NULL;cos_complete_part_content_t *complete_content = NULL;cos_table_t *complete_resp_headers = NULL;cos_status_t *s = NULL;int part1 = 1;int part2 = 2;char *local_filename = "test_upload_part_copy.file";char *download_filename = "test_upload_part_copy.file.download";char *source_object_name = "cos_test_upload_part_copy_source_object";char *dest_object_name = "cos_test_upload_part_copy_dest_object";FILE *fd = NULL;cos_string_t download_file;cos_string_t dest_bucket;cos_string_t dest_object;int64_t range_start1 = 0;int64_t range_end1 = 6000000;int64_t range_start2 = 6000001;int64_t range_end2;cos_string_t data;cos_pool_create(&p, NULL);options = cos_request_options_create(p);// create multipart upload local filemake_rand_string(p, 10 * 1024 * 1024, &data);fd = fopen(local_filename, "w");fwrite(data.data, sizeof(data.data[0]), data.len, fd);fclose(fd);init_test_request_options(options, is_cname);cos_str_set(&bucket, TEST_BUCKET_NAME);cos_str_set(&object, source_object_name);cos_str_set(&file, local_filename);s = cos_put_object_from_file(options, &bucket, &object, &file, NULL, &resp_headers);log_status(s);//init mulitipartcos_str_set(&object, dest_object_name);s = cos_init_multipart_upload(options, &bucket, &object,&upload_id, NULL, &resp_headers);log_status(s);//upload part copy 1upload_part_copy_params1 = cos_create_upload_part_copy_params(p);cos_str_set(&upload_part_copy_params1->copy_source, "bucket-appid.cn-south.myqcloud.com/cos_test_upload_part_copy_source_object");cos_str_set(&upload_part_copy_params1->dest_bucket, TEST_BUCKET_NAME);cos_str_set(&upload_part_copy_params1->dest_object, dest_object_name);cos_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 = cos_table_make(p, 0);s = cos_upload_part_copy(options, upload_part_copy_params1, headers, &resp_headers);log_status(s);printf("last modified:%s, etag:%s\\n", upload_part_copy_params1->rsp_content->last_modify.data, upload_part_copy_params1->rsp_content->etag.data);//upload part copy 2resp_headers = NULL;range_end2 = get_file_size(local_filename) - 1;upload_part_copy_params2 = cos_create_upload_part_copy_params(p);cos_str_set(&upload_part_copy_params2->copy_source, "bucket-appid.cn-south.myqcloud.com/cos_test_upload_part_copy_source_object");cos_str_set(&upload_part_copy_params2->dest_bucket, TEST_BUCKET_NAME);cos_str_set(&upload_part_copy_params2->dest_object, dest_object_name);cos_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 = cos_table_make(p, 0);s = cos_upload_part_copy(options, upload_part_copy_params2, headers, &resp_headers);log_status(s);printf("last modified:%s, etag:%s\\n", upload_part_copy_params1->rsp_content->last_modify.data, upload_part_copy_params1->rsp_content->etag.data);//list partlist_upload_part_params = cos_create_list_upload_part_params(p);list_upload_part_params->max_ret = 10;cos_list_init(&complete_part_list);cos_str_set(&dest_bucket, TEST_BUCKET_NAME);cos_str_set(&dest_object, dest_object_name);s = cos_list_upload_part(options, &dest_bucket, &dest_object, &upload_id,list_upload_part_params, &list_part_resp_headers);log_status(s);cos_list_for_each_entry(cos_list_part_content_t, part_content, &list_upload_part_params->part_list, node) {complete_content = cos_create_complete_part_content(p);cos_str_set(&complete_content->part_number, part_content->part_number.data);cos_str_set(&complete_content->etag, part_content->etag.data);cos_list_add_tail(&complete_content->node, &complete_part_list);}//complete multipartheaders = cos_table_make(p, 0);s = cos_complete_multipart_upload(options, &dest_bucket, &dest_object,&upload_id, &complete_part_list, headers, &complete_resp_headers);log_status(s);//check upload copy part content equal to local fileheaders = cos_table_make(p, 0);cos_str_set(&download_file, download_filename);s = cos_get_object_to_file(options, &dest_bucket, &dest_object, headers,query_params, &download_file, &resp_headers);log_status(s);printf("local file len = %"APR_INT64_T_FMT", download file len = %"APR_INT64_T_FMT, get_file_size(local_filename), get_file_size(download_filename));remove(download_filename);remove(local_filename);cos_pool_destroy(p);printf("test part copy ok\\n");}int main(int argc, char *argv[]){// 通过环境变量获取 SECRETID 和 SECRETKEYTEST_ACCESS_KEY_ID = getenv("COS_SECRETID");TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");if (cos_http_io_initialize(NULL, 0) != COSE_OK) {exit(1);}//set log level, default COS_LOG_WARNcos_log_set_level(COS_LOG_WARN);//set log output, default stderrcos_log_set_output(NULL);test_part_copy();cos_http_io_deinitialize();return 0;}
#include "cos_http_io.h"#include "cos_api.h"#include "cos_log.h"// endpoint 是 COS 访问域名信息,详情请参见 https://www.tencentcloud.com/document/product/436/6224 文档static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";// 开发者拥有的项目身份ID/密钥,可在 https://console.tencentcloud.com/cam/capi 页面获取static char *TEST_ACCESS_KEY_ID; //your secret_idstatic char *TEST_ACCESS_KEY_SECRET; //your secret_key// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.tencentcloud.com/cam/capi 页面获取static char TEST_APPID[] = "<APPID>"; //your appid//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.tencentcloud.com/cos5/bucket 查看static char TEST_BUCKET_NAME[] = "<bucketname-appid>";// 对象键,对象(Object)在存储桶(Bucket)中的唯一标识。有关对象与对象键的进一步说明,请参见 https://www.tencentcloud.com/document/product/436/13324 文档static char TEST_OBJECT_NAME1[] = "1.txt";static char TEST_OBJECT_NAME2[] = "test2.dat";void log_status(cos_status_t *s){cos_warn_log("status->code: %d", s->code);if (s->error_code) cos_warn_log("status->error_code: %s", s->error_code);if (s->error_msg) cos_warn_log("status->error_msg: %s", s->error_msg);if (s->req_id) cos_warn_log("status->req_id: %s", s->req_id);}void init_test_config(cos_config_t *config, int is_cname){cos_str_set(&config->endpoint, TEST_COS_ENDPOINT);cos_str_set(&config->access_key_id, TEST_ACCESS_KEY_ID);cos_str_set(&config->access_key_secret, TEST_ACCESS_KEY_SECRET);cos_str_set(&config->appid, TEST_APPID);config->is_cname = is_cname;}void init_test_request_options(cos_request_options_t *options, int is_cname){options->config = cos_config_create(options->pool);init_test_config(options->config, is_cname);options->ctl = cos_http_controller_create(options->pool, 0);}void test_move(){cos_pool_t *p = NULL;int is_cname = 0;cos_status_t *s = NULL;cos_request_options_t *options = NULL;cos_string_t bucket;cos_string_t object;cos_string_t src_object;cos_string_t src_endpoint;cos_table_t *resp_headers = NULL;//创建内存池cos_pool_create(&p, NULL);//初始化请求选项options = cos_request_options_create(p);init_test_request_options(options, is_cname);cos_str_set(&bucket, TEST_BUCKET_NAME);//设置对象复制cos_str_set(&object, TEST_OBJECT_NAME1);cos_str_set(&src_endpoint, TEST_COS_ENDPOINT);cos_str_set(&src_object, TEST_OBJECT_NAME2);cos_copy_object_params_t *params = NULL;params = cos_create_copy_object_params(p);s = cos_copy_object(options, &bucket, &src_object, &src_endpoint, &bucket, &object, NULL, params, &resp_headers);log_status(s);if (cos_status_is_ok(s)) {s = cos_delete_object(options, &bucket, &src_object, &resp_headers);log_status(s);printf("move object succeeded\\n");} else {printf("move object failed\\n");}//销毁内存池cos_pool_destroy(p);}int main(int argc, char *argv[]){// 通过环境变量获取 SECRETID 和 SECRETKEYTEST_ACCESS_KEY_ID = getenv("COS_SECRETID");TEST_ACCESS_KEY_SECRET = getenv("COS_SECRETKEY");if (cos_http_io_initialize(NULL, 0) != COSE_OK) {exit(1);}//set log level, default COS_LOG_WARNcos_log_set_level(COS_LOG_WARN);//set log output, default stderrcos_log_set_output(NULL);test_move();cos_http_io_deinitialize();return 0;}
本页内容是否解决了您的问题?