API | 操作名 | 操作描述 |
查询对象列表 | 查询存储桶下的部分或者全部对象 |
cos_status_t *cos_list_object(const cos_request_options_t *options,const cos_string_t *bucket,cos_list_object_params_t *params,cos_table_t **resp_headers);
参数名称 | 参数描述 | 类型 |
options | COS 请求选项 | Struct |
bucket | 存储桶名称,Bucket 的命名规则为 BucketName-APPID ,此处填写的存储桶名称必须为此格式 | String |
params | 列表操作参数信息 | Struct |
encoding_type | 规定返回值的编码方式 | String |
prefix | 前缀匹配,用来规定返回的文件前缀地址 | String |
marker | 默认以 UTF-8 二进制顺序列出条目,所有列出条目从 marker 开始 | String |
delimiter | 查询分隔符,用于对对象键进行分组 | String |
max_ret | 单次返回最大的条目数量,默认1000 | Struct |
truncated | 返回条目是否被截断,'true' 或者 'false' | Boolean |
next_marker | 假如返回条目被截断,则返回 NextMarker 就是下一个条目的起点 | String |
object_list | Get Bucket 操作返回的对象信息列表 | Struct |
key | Get Bucket 操作返回的 Object 名称 | Struct |
last_modified | Get Bucket 操作返回的 Object 最后修改时间 | Struct |
etag | Get Bucket 操作返回的对象的 SHA-1 算法校验值 | Struct |
size | Get Bucket 操作返回的对象大小,单位 Byte | Struct |
owner_id | Get Bucket 操作返回的对象拥有者 UID 信息 | Struct |
storage_class | Get Bucket 操作返回的对象存储级别 | Struct |
common_prefix_list | 将 Prefix 到 delimiter 之间的相同路径归为一类,定义为 Common Prefix | 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_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 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_objects(){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_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_list_object_params_t *list_params = NULL;cos_list_object_content_t *content = NULL;list_params = cos_create_list_object_params(p);s = cos_list_object(options, &bucket, list_params, &resp_headers);if (cos_status_is_ok(s)) {printf("list object succeeded\\n");cos_list_for_each_entry(cos_list_object_content_t, content, &list_params->object_list, node) {printf("object: %.*s\\n", content->key.len, content->key.data);}} else {printf("list 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_list_objects();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>";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_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_table_t *resp_headers;int is_truncated = 1;cos_string_t marker;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);// prefix表示列出的object的key以prefix开始cos_str_set(&list_params->prefix, "folder/");// deliter表示分隔符, 设置为/表示列出当前目录下的object, 设置为空表示列出所有的objectcos_str_set(&list_params->delimiter, "/");// 设置最大遍历出多少个对象, 一次listobject最大支持1000list_params->max_ret = 1000;cos_str_set(&marker, "");while (is_truncated) {list_params->marker = marker;s = cos_list_object(options, &bucket, list_params, &resp_headers);if (!cos_status_is_ok(s)) {printf("list object failed, req_id:%s\\n", s->req_id);break;}// list_params->object_list 返回列出的object对象。cos_list_object_content_t *content = NULL;cos_list_for_each_entry(cos_list_object_content_t, content, &list_params->object_list, node) {printf("object: %s\\n", content->key.data);}// list_params->common_prefix_list 表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径cos_list_object_common_prefix_t *common_prefix = NULL;cos_list_for_each_entry(cos_list_object_common_prefix_t, common_prefix, &list_params->common_prefix_list, node) {printf("common prefix: %s\\n", common_prefix->prefix.data);}is_truncated = list_params->truncated;marker = list_params->next_marker;}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_list_directory();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>";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_all_objects(){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_table_t *resp_headers;int is_truncated = 1;cos_string_t marker;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);// 设置最大遍历出多少个对象, 一次listobject最大支持1000list_params->max_ret = 1000;cos_str_set(&marker, "");while (is_truncated) {list_params->marker = marker;cos_list_init(&list_params->object_list);s = cos_list_object(options, &bucket, list_params, &resp_headers);if (!cos_status_is_ok(s)) {printf("list object failed, req_id:%s\\n", s->req_id);break;}// list_params->object_list 返回列出的object对象。cos_list_object_content_t *content = NULL;cos_list_for_each_entry(cos_list_object_content_t, content, &list_params->object_list, node) {printf("object: %s\\n", content->key.data);}is_truncated = list_params->truncated;marker = list_params->next_marker;}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_list_all_objects();cos_http_io_deinitialize();return 0;}
本页内容是否解决了您的问题?