tencent cloud

文档反馈

上传对象

最后更新时间:2024-01-04 17:06:08

    简介

    本文档提供关于对象的上传操作相关的 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://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_id
    static 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>";
    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://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_id
    static 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";
    
    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,即100KB/s - 100MB/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://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_id
    static 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 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://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_id
    static 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 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://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_id
    static 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>";
    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, NULL, &resp_headers);
    log_status(s);
    
    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);
    }
    }
    }
    
    //销毁内存池
    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_append_object();
    
    cos_http_io_deinitialize();
    
    return 0;
    }

    分块操作

    查询分块上传

    功能说明

    查询正在进行中的分块上传信息。单次最多列出1000个正在进行中的分块上传。

    方法原型

    cos_status_t *cos_list_multipart_upload(const cos_request_options_t *options,
    const cos_string_t *bucket,
    cos_list_multipart_upload_params_t *params,
    cos_table_t **resp_headers);

    参数说明

    参数名称
    参数描述
    类型
    options
    COS 请求选项
    Struct
    bucket
    存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
    String
    params
    List Multipart Uploads 操作参数
    Struct
    encoding_type
    规定返回值的编码方式
    String
    prefix
    前缀匹配,用来规定返回的文件前缀地址
    String
    delimiter
    界符为一个符号:
    如果有 Prefix,则将 Prefix 到 delimiter 之间的相同路径归为一类,定义为 Common Prefix,然后列出所有 Common Prefix
    如果没有 Prefix,则从路径起点开始
    String
    max_ret
    单次返回最大的条目数量,默认1000
    String
    key_marker
    与 upload-id-marker 一起使用:
    当 upload-id-marker 未被指定时,ObjectName 字母顺序大于 key-marker 的条目将被列出
    当 upload-id-marker 被指定时,ObjectName 字母顺序大于 key-marker 的条目被列出,ObjectName 字母顺序等于 key-marker 同时 UploadID 大于 upload-id-marker 的条目将被列出
    String
    upload_id_marker
    与 key-marker 一起使用:
    当 key-marker 未被指定时,upload-id-marker 将被忽略
    当 key-marker 被指定时,ObjectName 字母顺序大于 key-marker 的条目被列出,ObjectName 字母顺序等于 key-marker 同时 UploadID 大于 upload-id-marker 的条目将被列出
    String
    truncated
    返回条目是否被截断,'true' 或者 'false'
    Boolean
    next_key_marker
    假如返回条目被截断,则返回 NextKeyMarker 就是下一个条目的起点
    String
    next_upload_id_marker
    假如返回条目被截断,则返回 UploadId 就是下一个条目的起点
    String
    upload_list
    分块上传的信息
    Struct
    key
    Object 的名称
    String
    upload_id
    标示本次分块上传的 ID
    String
    initiated
    标示本次分块上传任务的启动时间
    String
    resp_headers
    返回 HTTP 响应消息的头域
    Struct
    typedef struct {
    cos_list_t node;
    cos_string_t key;
    cos_string_t upload_id;
    cos_string_t initiated;
    } cos_list_multipart_upload_content_t;

    返回结果说明

    返回结果
    描述
    类型
    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 文档
    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_id
    static 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>";
    static char TEST_MULTIPART_OBJECT[] = "multipart.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 list_multipart()
    {
    cos_pool_t *p = NULL;
    cos_string_t bucket;
    cos_string_t object;
    int is_cname = 0;
    cos_table_t *resp_headers = NULL;
    cos_request_options_t *options = NULL;
    cos_status_t *s = NULL;
    cos_list_multipart_upload_params_t *list_multipart_params = NULL;
    cos_list_upload_part_params_t *list_upload_param = 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);
    
    list_multipart_params = cos_create_list_multipart_upload_params(p);
    list_multipart_params->max_ret = 999;
    s = cos_list_multipart_upload(options, &bucket, list_multipart_params, &resp_headers);
    log_status(s);
    
    list_upload_param = cos_create_list_upload_part_params(p);
    list_upload_param->max_ret = 1000;
    cos_string_t upload_id;
    cos_str_set(&upload_id,"149373379126aee264fecbf5fe8ddb8b9cd23b76c73ab1af0bcfd50683cc4254f81ebe2386");
    cos_str_set(&object, TEST_MULTIPART_OBJECT);
    s = cos_list_upload_part(options, &bucket, &object, &upload_id,
    list_upload_param, &resp_headers);
    if (cos_status_is_ok(s)) {
    printf("List upload part succeeded, upload_id::%.*s\\n",
    upload_id.len, upload_id.data);
    cos_list_part_content_t *part_content = NULL;
    cos_list_for_each_entry(cos_list_part_content_t, part_content, &list_upload_param->part_list, node) {
    printf("part_number = %s, size = %s, last_modified = %s, etag = %s\\n",
    part_content->part_number.data,
    part_content->size.data,
    part_content->last_modified.data,
    part_content->etag.data);
    }
    } else {
    printf("List upload part 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);
    
    list_multipart();
    
    cos_http_io_deinitialize();
    
    return 0;
    }

    初始化分块上传

    功能说明

    Initiate Multipart Upload 请求实现初始化分片上传,成功执行此请求以后会返回 Upload ID 用于后续的 Upload Part 请求。

    方法原型

    cos_status_t *cos_init_multipart_upload(const cos_request_options_t *options,
    const cos_string_t *bucket,
    const cos_string_t *object,
    cos_string_t *upload_id,
    cos_table_t *headers,
    cos_table_t **resp_headers);

    参数说明

    参数名称
    参数描述
    类型
    options
    COS 请求选项
    Struct
    bucket
    存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
    String
    object
    Object 名称
    String
    upload_id
    操作返回的 Upload ID
    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://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_id
    static 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>";
    static char TEST_MULTIPART_OBJECT[] = "multipart.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_init_multipart_upload()
    {
    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 upload_id;
    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_MULTIPART_OBJECT);
    s = cos_init_multipart_upload(options, &bucket, &object,
    &upload_id, NULL, &resp_headers);
    if (cos_status_is_ok(s)) {
    printf("init multipart upload succeeded\\n");
    } else {
    printf("init multipart 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_init_multipart_upload();
    
    cos_http_io_deinitialize();
    
    return 0;
    }

    上传分块

    功能说明

    分块上传文件。Upload Part 请求实现在初始化以后的分块上传,支持的块的数量为1 - 10000,块的大小为1MB - 5GB。在每次请求 Upload Part 时,需要携带 partNumber 和 uploadID,partNumber 为块的编号,支持乱序上传。

    方法原型

    cos_status_t *cos_upload_part_from_file(const cos_request_options_t *options,
    const cos_string_t *bucket,
    const cos_string_t *object,
    const cos_string_t *upload_id,
    int part_num,
    cos_upload_file_t *upload_file,
    cos_table_t **resp_headers);

    参数说明

    参数名称
    参数描述
    类型
    options
    COS 请求选项
    Struct
    bucket
    存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
    String
    object
    Object 名称
    String
    upload_id
    上传任务编号
    String
    part_num
    分块编号
    Int
    upload_file
    待上传本地文件信息
    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://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_id
    static 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>";
    static char TEST_MULTIPART_OBJECT[] = "multipart.dat";
    static char TEST_MULTIPART_FILE[] = "test.zip";
    
    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_part()
    {
    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 upload_id = cos_string("xxxxxxxxxxxxxxxxxxx"); // 替换您自己的uploadid
    cos_table_t *resp_headers = NULL;
    int part_num = 1;
    int64_t pos = 0;
    int64_t file_length = 0;
    
    //创建内存池
    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_MULTIPART_OBJECT);
    
    //上传分块
    int res = COSE_OK;
    cos_upload_file_t *upload_file = NULL;
    cos_file_buf_t *fb = cos_create_file_buf(p);
    res = cos_open_file_for_all_read(p, TEST_MULTIPART_FILE, fb);
    if (res != COSE_OK) {
    cos_error_log("Open read file fail, filename:%s\\n", TEST_MULTIPART_FILE);
    return;
    }
    file_length = fb->file_last;
    apr_file_close(fb->file);
    while(pos < file_length) {
    upload_file = cos_create_upload_file(p);
    cos_str_set(&upload_file->filename, TEST_MULTIPART_FILE);
    upload_file->file_pos = pos;
    pos += 2 * 1024 * 1024;
    upload_file->file_last = pos < file_length ? pos : file_length; //2MB
    s = cos_upload_part_from_file(options, &bucket, &object, &upload_id,
    part_num++, upload_file, &resp_headers);
    
    if (cos_status_is_ok(s)) {
    printf("upload part succeeded\\n");
    } else {
    printf("upload part 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_part();
    
    cos_http_io_deinitialize();
    
    return 0;
    }

    查询已上传块

    功能说明

    查询特定分块上传操作中的已上传的块。

    方法原型

    cos_status_t *cos_list_upload_part(const cos_request_options_t *options,
    const cos_string_t *bucket,
    const cos_string_t *object,
    const cos_string_t *upload_id,
    cos_list_upload_part_params_t *params,
    cos_table_t **resp_headers);

    参数说明

    参数名称
    参数描述
    类型
    options
    COS 请求选项
    Struct
    bucket
    存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
    String
    object
    Object 名称
    String
    upload_id
    上传任务编号
    String
    params
    List Parts 操作参数
    Struct
    part_number_marker
    默认以 UTF-8 二进制顺序列出条目,所有列出条目从 marker 开始
    String
    encoding_type
    规定返回值的编码方式
    String
    max_ret
    单次返回最大的条目数量,默认1000
    String
    truncated
    返回条目是否被截断,'true' 或者 'false'
    Boolean
    next_part_number_marker
    假如返回条目被截断,则返回 NextMarker 就是下一个条目的起点
    String
    part_list
    完成分块的信息
    Struct
    part_number
    分块编号
    String
    size
    分块大小,单位 Byte
    String
    etag
    分块的 SHA-1 算法校验值
    String
    last_modified
    分块最后修改时间
    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 文档
    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_id
    static 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>";
    static char TEST_MULTIPART_OBJECT[] = "multipart.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_list_upload_part()
    {
    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_list_upload_part_params_t *params = NULL;
    cos_list_t complete_part_list;
    cos_string_t upload_id = cos_string("xxxxxxxxxxxxxxxxxxx"); // 替换您自己的uploadid
    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_MULTIPART_OBJECT);
    
    //查询已上传块
    params = cos_create_list_upload_part_params(p);
    params->max_ret = 1000;
    cos_list_init(&complete_part_list);
    s = cos_list_upload_part(options, &bucket, &object, &upload_id,
    params, &resp_headers);
    
    if (cos_status_is_ok(s)) {
    printf("List multipart succeeded\\n");
    } else {
    printf("List multipart 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_list_upload_part();
    
    cos_http_io_deinitialize();
    
    return 0;
    }

    完成分块上传

    功能说明

    完成整个文件的分块上传。当您已经使用 Upload Parts 上传所有块以后,您可以用该 API 完成上传。在使用该 API 时,您必须在 Body 中给出每一个块的 PartNumber 和 ETag,用来校验块的准确性。

    方法原型

    cos_status_t *cos_complete_multipart_upload(const cos_request_options_t *options,
    const cos_string_t *bucket,
    const cos_string_t *object,
    const cos_string_t *upload_id,
    cos_list_t *part_list,
    cos_table_t *headers,
    cos_table_t **resp_headers);

    参数说明

    参数名称
    参数描述
    类型
    options
    COS 请求选项
    Struct
    bucket
    存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
    String
    object
    Object 名称
    String
    upload_id
    上传任务编号
    String
    part_list
    完成分块上传的参数
    Struct
    part_number
    分块编号
    String
    etag
    分块的 ETag 值,为 sha1 校验值,需要在校验值前后加上双引号,如 "3a0f1fd698c235af9cf098cb74aa25bc"
    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://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_id
    static 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>";
    static char TEST_MULTIPART_OBJECT[] = "multipart.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_complete_multipart_upload()
    {
    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_list_upload_part_params_t *params = NULL;
    cos_list_t complete_part_list;
    cos_string_t upload_id = cos_string("xxxxxxxxxxxxxxxxxxx"); // 替换您自己的uploadid
    cos_table_t *resp_headers = NULL;
    cos_list_part_content_t *part_content = NULL;
    cos_complete_part_content_t *complete_part_content = 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_MULTIPART_OBJECT);
    
    //查询已上传分块
    params = cos_create_list_upload_part_params(p);
    params->max_ret = 1000;
    cos_list_init(&complete_part_list);
    s = cos_list_upload_part(options, &bucket, &object, &upload_id,
    params, &resp_headers);
    
    if (cos_status_is_ok(s)) {
    printf("List multipart succeeded\\n");
    } else {
    printf("List multipart failed\\n");
    cos_pool_destroy(p);
    return;
    }
    
    cos_list_for_each_entry(cos_list_part_content_t, part_content, &params->part_list, node) {
    complete_part_content = cos_create_complete_part_content(p);
    cos_str_set(&complete_part_content->part_number, part_content->part_number.data);
    cos_str_set(&complete_part_content->etag, part_content->etag.data);
    cos_list_add_tail(&complete_part_content->node, &complete_part_list);
    }
    
    //完成分块上传
    s = cos_complete_multipart_upload(options, &bucket, &object, &upload_id,
    &complete_part_list, NULL, &resp_headers);
    
    if (cos_status_is_ok(s)) {
    printf("Complete multipart upload from file succeeded, upload_id:%.*s\\n",
    upload_id.len, upload_id.data);
    } else {
    printf("Complete multipart upload from file 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_complete_multipart_upload();
    
    cos_http_io_deinitialize();
    
    return 0;
    }

    终止分块上传

    功能说明

    终止一个分块上传操作并删除已上传的块。当您调用 Abort Multipart Upload 时,如果有正在使用这个 Upload Parts 上传块的请求,则 Upload Parts 会返回失败。

    方法原型

    cos_status_t *cos_abort_multipart_upload(const cos_request_options_t *options,
    const cos_string_t *bucket,
    const cos_string_t *object,
    cos_string_t *upload_id,
    cos_table_t **resp_headers);

    参数说明

    参数名称
    参数描述
    类型
    options
    COS 请求选项
    Struct
    bucket
    存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
    String
    object
    Object 名称
    String
    upload_id
    上传任务编号
    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 文档
    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_id
    static 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>";
    static char TEST_MULTIPART_OBJECT[] = "multipart.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 abort_multipart_upload()
    {
    cos_pool_t *p = NULL;
    cos_string_t bucket;
    cos_string_t object;
    int is_cname = 0;
    cos_table_t *headers = NULL;
    cos_table_t *resp_headers = NULL;
    cos_request_options_t *options = NULL;
    cos_string_t upload_id;
    cos_status_t *s = NULL;
    
    cos_pool_create(&p, NULL);
    options = cos_request_options_create(p);
    init_test_request_options(options, is_cname);
    headers = cos_table_make(p, 1);
    cos_str_set(&bucket, TEST_BUCKET_NAME);
    cos_str_set(&object, TEST_MULTIPART_OBJECT);
    
    s = cos_init_multipart_upload(options, &bucket, &object,
    &upload_id, headers, &resp_headers);
    
    if (cos_status_is_ok(s)) {
    printf("Init multipart upload succeeded, upload_id:%.*s\\n",
    upload_id.len, upload_id.data);
    } else {
    printf("Init multipart upload failed\\n");
    cos_pool_destroy(p);
    return;
    }
    
    s = cos_abort_multipart_upload(options, &bucket, &object, &upload_id,
    &resp_headers);
    
    if (cos_status_is_ok(s)) {
    printf("Abort multipart upload succeeded, upload_id::%.*s\\n",
    upload_id.len, upload_id.data);
    } else {
    printf("Abort multipart 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);
    
    abort_multipart_upload();
    
    cos_http_io_deinitialize();
    
    return 0;
    }
    
    联系我们

    联系我们,为您的业务提供专属服务。

    技术支持

    如果你想寻求进一步的帮助,通过工单与我们进行联络。我们提供7x24的工单服务。

    7x24 电话支持