有奖:文档满意度调查问卷,诚邀您填写> HOT

简介

本文档提供关于对象的高级上传、简单上传、分块上传等操作相关的 API 概览以及 SDK 示例代码。
简单操作
API
操作名
操作描述
简单上传对象(创建文件夹)
上传一个对象至存储桶
追加上传对象
使用分块追加的方式上传对象
分块操作
API
操作名
操作描述
查询分块上传
查询正在进行中的分块上传信息
初始化分块上传
初始化分块上传任务
上传分块
分块上传文件
查询已上传块
查询特定分块上传操作中的已上传的块
完成分块上传
完成整个文件的分块上传
终止分块上传
终止一个分块上传操作并删除已上传的块

高级接口(推荐)

上传对象(断点续传)

功能说明

上传接口根据用户文件的长度,自动切分数据, 降低用户的使用门槛,用户无需关心分块上传的每个步骤, 且可以对分块上传未完成的文件会进行断点续传,分块大小默认 1048576(1MB),可通过 part_size 参数调整。

方法原型

cos_status_t *cos_resumable_upload_file(cos_request_options_t *options,
cos_string_t *bucket,
cos_string_t *object,
cos_string_t *filepath,
cos_table_t *headers,
cos_table_t *params,
cos_resumable_clt_params_t *clt_params,
cos_progress_callback progress_callback,
cos_table_t **resp_headers,
cos_list_t *resp_body)

参数说明

参数名称
参数描述
类型
options
COS 请求选项
Struct
bucket
存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String
object
Object 名称
String
filepath
Object 本地文件名称
String
headers
COS 请求附加头域
Struct
params
COS 请求操作参数
Struct
clt_params
上传对象控制参数
Struct
part_size
块大小,单位为 bytes,如果用户指定的 part_size 小于1048576(1MB), 由 C SDK 自动切分, 分块大小默认1048576(1MB),如果分块数超过10000,则根据文件大小调整
Int
thread_num
线程池大小,默认为1
Int
enable_checkpoint
是否使能断点续传
Int
checkpoint_path
当使能断点续传时,表示保存上传进度的文件路径,默认路径为<filepath>.cp,其中 filepath 为 Object 本地文件名称
String
progress_callback
上传进度回调函数
Function
resp_headers
返回 HTTP 响应消息的头域
Struct
resp_body
保存完成分块上传请求时返回的数据
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://cloud.tencent.com/document/product/436/6224 文档
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char TEST_APPID[] = "<APPID>"; //your appid
//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.cloud.tencent.com/cos5/bucket 查看
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";
static char TEST_MULTIPART_FILE[] = "test.zip";
static char TEST_MULTIPART_OBJECT4[] = "multipart4.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_resumable_upload_with_multi_threads()
{
cos_pool_t *p = NULL;
cos_string_t bucket;
cos_string_t object;
cos_string_t filename;
cos_status_t *s = NULL;
int is_cname = 0;
cos_table_t *headers = NULL;
cos_table_t *resp_headers = NULL;
cos_request_options_t *options = NULL;
cos_resumable_clt_params_t *clt_params;

cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
headers = cos_table_make(p, 0);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_MULTIPART_OBJECT4);
cos_str_set(&filename, TEST_MULTIPART_FILE);

// upload
clt_params = cos_create_resumable_clt_params_content(p, 1024 * 1024, 8, COS_FALSE, NULL);
s = cos_resumable_upload_file(options, &bucket, &object, &filename, headers, NULL,
clt_params, NULL, &resp_headers, NULL);

if (cos_status_is_ok(s)) {
printf("upload succeeded\\n");
} else {
printf("upload failed\\n");
}

cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// 通过环境变量获取 SECRETID 和 SECRETKEY
TEST_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_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_resumable_upload_with_multi_threads();

cos_http_io_deinitialize();

return 0;
}

简单操作

简单上传对象(创建文件夹)

功能说明

上传一个对象至存储桶,最大支持上传不超过5GB的对象,5GB以上对象请使用 分块上传高级接口 上传。

方法原型

cos_status_t *cos_put_object_from_file(const cos_request_options_t *options,
const cos_string_t *bucket,
const cos_string_t *object,
const cos_string_t *filename,
cos_table_t *headers,
cos_table_t **resp_headers);

参数说明

参数名称
参数描述
类型
options
COS 请求选项
Struct
bucket
存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String
object
Object 名称
String
filename
Object 本地保存文件名称
String
headers
COS 请求附加头域
Struct
resp_headers
返回 HTTP 响应消息的头域
Struct

返回结果说明

返回结果
描述
类型
code
错误码
Int
error_code
错误码内容
String
error_msg
错误码描述
String
req_id
请求消息 ID
String

示例1:上传对象

#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"

// endpoint 是 COS 访问域名信息,详情请参见 https://cloud.tencent.com/document/product/436/6224 文档
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char TEST_APPID[] = "<APPID>"; //your appid
//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.cloud.tencent.com/cos5/bucket 查看
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";
// 对象键,对象(Object)在存储桶(Bucket)中的唯一标识。有关对象与对象键的进一步说明,请参见 https://cloud.tencent.com/document/product/436/13324 文档
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_put_object_from_file()
{
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_table_t *resp_headers;
cos_string_t file;
int traffic_limit = 0;

cos_pool_create(&p, NULL);
options = cos_request_options_create(p);
init_test_request_options(options, is_cname);
cos_table_t *headers = NULL;
if (traffic_limit) {
// 限速值设置范围为819200 - 838860800,单位默认为 bit/s,即800Kb/s - 800Mb/s,如果超出该范围将返回400错误
headers = cos_table_make(p, 1);
cos_table_add_int(headers, "x-cos-traffic-limit", 819200);
}
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&file, TEST_OBJECT_NAME1);
cos_str_set(&object, TEST_OBJECT_NAME1);
s = cos_put_object_from_file(options, &bucket, &object, &file, headers, &resp_headers);
log_status(s);

cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// 通过环境变量获取 SECRETID 和 SECRETKEY
TEST_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_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_put_object_from_file();

cos_http_io_deinitialize();

return 0;
}

示例2:创建文件夹

COS 上可以将以 / 分隔的对象路径看做一个虚拟文件夹,根据此特性,可以上传一个空的流,并且命名以 / 结尾,可实现在 COS 上创建一个空文件夹。
#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"

// endpoint 是 COS 访问域名信息,详情请参见 https://cloud.tencent.com/document/product/436/6224 文档
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char TEST_APPID[] = "<APPID>"; //your appid
//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.cloud.tencent.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 test_create_dir()
{
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_table_t *resp_headers;
cos_table_t *headers = NULL;
cos_list_t buffer;

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, "folder/");

//上传文件夹
cos_list_init(&buffer);
s = cos_put_object_from_buffer(options, &bucket, &object,
&buffer, headers, &resp_headers);
if (cos_status_is_ok(s)) {
printf("put object succeeded\\n");
} else {
printf("put object failed\\n");
}
cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// 通过环境变量获取 SECRETID 和 SECRETKEY
TEST_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_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_create_dir();

cos_http_io_deinitialize();

return 0;
}

示例3:上传文件到虚拟目录

上传由 / 分隔的对象名,可自动创建包含文件的文件夹。想要在此文件夹中添加新文件时,只需要在上传文件至 COS 时,将 Key 填写为此目录前缀即可。
#include "cos_http_io.h"
#include "cos_api.h"
#include "cos_log.h"

// endpoint 是 COS 访问域名信息,详情请参见 https://cloud.tencent.com/document/product/436/6224 文档
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char TEST_APPID[] = "<APPID>"; //your appid
//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.cloud.tencent.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 test_upload_file_to_dir()
{
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 file;
cos_table_t *resp_headers;

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(&file, "examplefile");
cos_str_set(&object, "folder/exampleobject");
s = cos_put_object_from_file(options, &bucket, &object, &file, NULL, &resp_headers);
if (cos_status_is_ok(s)) {
printf("put object succeeded\\n");
} else {
printf("put object failed\\n");
}
cos_pool_destroy(p);
}

int main(int argc, char *argv[])
{
// 通过环境变量获取 SECRETID 和 SECRETKEY
TEST_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_WARN
cos_log_set_level(COS_LOG_WARN);

//set log output, default stderr
cos_log_set_output(NULL);

test_upload_file_to_dir();

cos_http_io_deinitialize();

return 0;
}

追加上传对象

功能说明

使用分块追加的方式上传对象,追加上传的对象,每个分块大小默认最大为5GB,无最小限制,同时通过追加方式产生的对象大小不得超过5GB。如果 Position 的值和当前对象的长度不一致,COS 将返回409错误。如果追加一个 normal 属性的文件,COS 将返回409 ObjectNotAppendable。

方法原型

cos_status_t *cos_append_object_from_file(const cos_request_options_t *options,
const cos_string_t *bucket,
const cos_string_t *object,
int64_t position,
const cos_string_t *append_file,
cos_table_t *headers,
cos_table_t **resp_headers);

参数说明

参数名称
参数描述
类型
options
COS 请求选项
Struct
bucket
存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String
object
Object 名称
String
position
追加操作的起始点,单位为字节。首次追加则设置 Position=0,后续追加则设置 Position 为当前 Object 的 content-length
String
append_file
Object 本地保存文件名称
String
headers
COS 请求附加头域
Struct
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://cloud.tencent.com/document/product/436/6224 文档
static char TEST_COS_ENDPOINT[] = "cos.ap-guangzhou.myqcloud.com";
// 开发者拥有的项目身份ID/密钥,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char *TEST_ACCESS_KEY_ID; //your secret_id
static char *TEST_ACCESS_KEY_SECRET; //your secret_key
// 开发者访问 COS 服务时拥有的用户维度唯一资源标识,用以标识资源,可在 https://console.cloud.tencent.com/cam/capi 页面获取
static char TEST_APPID[] = "<APPID>"; //your appid
//the cos bucket name, syntax: [bucket]-[appid], for example: mybucket-1253666666,可在 https://console.cloud.tencent.com/cos5/bucket 查看
static char TEST_BUCKET_NAME[] = "<bucketname-appid>";
static char TEST_OBJECT_NAME3[] = "test3.dat";
static char *TEST_APPEND_NAMES[] = {"test.7z.001", "test.7z.002"};

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_append_object()
{
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 file;
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_NAME3);
int32_t count = sizeof(TEST_APPEND_NAMES)/sizeof(char*);
int32_t index = 0;
int64_t position = 0;
s = cos_head_object(options, &bucket, &object, NULL, &resp_headers);
if(s->code == 200) {
char *content_length_str = (char*)apr_table_get(resp_headers, COS_CONTENT_LENGTH);
if (content_length_str != NULL) {
position = atol(content_length_str);
}
}
for (; index < count; index++)
{
cos_str_set(&file, TEST_APPEND_NAMES[index]);
s = cos_append_object_from_file(options, &bucket, &object,
position, &file,