有奖捉虫:云通信与企业服务文档专题,速来> HOT

简介

本文档提供关于对象的下载操作相关的 API 概览以及 SDK 示例代码。
简单操作
API
操作名
操作描述
下载对象
下载一个对象至本地

高级接口(推荐)

下载对象(断点续传)

功能说明

分块下载接口根据用户对象的长度,自动使用 Range 下载数据,实现并发下载,分块大小默认1048576(1MB),可通过 part_size 参数调整。

方法原型

cos_status_t *cos_resumable_download_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);

参数说明

参数名称
参数描述
类型
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 小于4194304(4MB),则按4194304(4MB)处理
Int
thread_num
线程池大小,默认为1
Int
enable_checkpoint
是否开启断点续传
Int
checkpoint_path
当开启断点续传时,表示保存上传进度的文件路径,默认路径为<filepath>.cp,其中 filepath 为 Object 本地文件名称
String
progress_callback
下载进度回调函数
Function

结果说明

返回结果
描述
类型
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_DOWNLOAD_NAME4[] = "multipart_download.dat";
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()
{
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 filepath;
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);
cos_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_MULTIPART_OBJECT4);
cos_str_set(&filepath, TEST_DOWNLOAD_NAME4);

clt_params = cos_create_resumable_clt_params_content(p, 5*1024*1024, 3, COS_FALSE, NULL);
s = cos_resumable_download_file(options, &bucket, &object, &filepath, NULL, NULL, clt_params, NULL);
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);
}

//设置日志级别,默认COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//设置日志输出,默认stderr
cos_log_set_output(NULL);

test_resumable();

cos_http_io_deinitialize();

return 0;
}

批量下载(从 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_download_directory()
{
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 file_name;
cos_string_t suffix = cos_string("/");
cos_table_t *resp_headers;
cos_table_t *headers = NULL;
cos_table_t *params = NULL;
int is_truncated = 1;
cos_string_t marker;
apr_status_t status;

//初始化请求选项
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 object (get bucket)
cos_list_object_params_t *list_params = NULL;
list_params = cos_create_list_object_params(p);
cos_str_set(&list_params->prefix, "folder/"); //替换为您自己的目录名称
cos_str_set(&marker, "");
while (is_truncated) {
list_params->marker = marker;
s = cos_list_object(options, &bucket, list_params, &resp_headers);
log_status(s);
if (!cos_status_is_ok(s)) {
printf("list object failed, req_id:%s\\n", s->req_id);
break;
}
cos_list_object_content_t *content = NULL;
cos_list_for_each_entry(cos_list_object_content_t, content, &list_params->object_list, node) {
cos_str_set(&file_name, content->key.data);
if (cos_ends_with(&content->key, &suffix)) {
//如果是目录需要先创建, 0x0755权限可以自己按需修改,参考apr_file_info.h中定义
status = apr_dir_make(content->key.data, 0x0755, options->pool);
if (status != APR_SUCCESS && !APR_STATUS_IS_EEXIST(status)) {
printf("mkdir: %s failed, status: %d\\n", content->key.data, status);
}
} else {
//下载对象到本地目录,这里默认下载在程序运行的当前目录
s = cos_get_object_to_file(options, &bucket, &content->key, headers, params, &file_name, &resp_headers);
if (!cos_status_is_ok(s)) {
printf("get object[%s] failed, req_id:%s\\n", content->key.data, s->req_id);
}
}
}
is_truncated = list_params->truncated;
marker = list_params->next_marker;
}

//销毁内存池
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);
}

//设置日志级别,默认COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//设置日志输出,默认stderr
cos_log_set_output(NULL);

test_download_directory();

cos_http_io_deinitialize();

return 0;
}

简单操作

下载对象

功能说明

下载一个对象到本地文件或本地内存。该操作需要对目标对象具有读权限或该目标对象已对所有人都开放了读权限(公有读)。

方法原型

//下载对象到本地文件
cos_status_t *cos_get_object_to_file(const cos_request_options_t *options,
const cos_string_t *bucket,
const cos_string_t *object,
cos_table_t *headers,
cos_table_t *params,
cos_string_t *filename,
cos_table_t **resp_headers);
//下载对象到本地内存
cos_status_t *cos_get_object_to_buffer(const cos_request_options_t *options,
const cos_string_t *bucket,
const cos_string_t *object,
cos_table_t *headers,
cos_table_t *params,
cos_list_t *buffer,
cos_table_t **resp_headers)

示例一:下载对象到本地文件

#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";
static char TEST_DOWNLOAD_NAME[] = "download_test3.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_download_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(&file, TEST_DOWNLOAD_NAME);
cos_str_set(&object, TEST_OBJECT_NAME1);
s = cos_get_object_to_file(options, &bucket, &object, NULL, NULL, &file, &resp_headers);
if (cos_status_is_ok(s)) {
printf("get object succeeded\\n");
} else {
printf("get 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);
}

//设置日志级别,默认COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//设置日志输出,默认stderr
cos_log_set_output(NULL);

test_download_object();

cos_http_io_deinitialize();

return 0;
}
参数说明
参数名称
参数描述
类型
options
COS 请求选项
Struct
bucket
存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String
object
Object 名称
String
headers
COS 请求附加头域
Struct
params
COS 请求操作参数
Struct
filename
Object 本地保存文件名称
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 "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 test_get_object_to_buffer()
{
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_buf_t *content = NULL;
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_str_set(&bucket, TEST_BUCKET_NAME);
cos_str_set(&object, TEST_OBJECT_NAME1);
cos_list_t download_buffer;
cos_list_init(&download_buffer);
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);
}
s = cos_get_object_to_buffer(options, &bucket, &object,
headers, NULL, &download_buffer, &resp_headers);
log_status(s);
print_headers(resp_headers);
int64_t len = 0;
int64_t size = 0;
int64_t pos = 0;
cos_list_for_each_entry(cos_buf_t, content, &download_buffer, node) {
len += cos_buf_size(content);
}
char *buf = cos_pcalloc(p, (apr_size_t)(len + 1));
buf[len] = '\\0';
cos_list_for_each_entry(cos_buf_t, content, &download_buffer, node) {
size = cos_buf_size(content);
memcpy(buf + pos, content->pos, (size_t)size);
pos += size;
}
cos_warn_log("Download data=%s", buf);
//销毁内存池
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);
}

//设置日志级别,默认COS_LOG_WARN
cos_log_set_level(COS_LOG_WARN);

//设置日志输出,默认stderr
cos_log_set_output(NULL);

test_get_object_to_buffer();

cos_http_io_deinitialize();

return 0;
}
参数说明
参数名称
参数描述
类型
options
COS 请求选项
Struct
bucket
存储桶名称,存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String
object
Object 名称
String
headers
COS 请求附加头域
Struct
params
COS 请求操作参数
Struct
buffer
接收对象内容的缓冲区
Struct
resp_headers
返回 HTTP 响应消息的头域
Struct
返回结果说明
返回结果
描述
类型
code
错误码
Int
error_code
错误码内容
String
error_msg
错误码描述
String
req_id
请求消息 ID
String