查询存储桶列表

最近更新时间:2024-12-13 19:20:04

我的收藏

简介

本文档提供关于查询存储桶列表的 API 概览以及 SDK 示例代码。

注意事项

在您使用存储桶相关操作之前,需要先具有相关权限:
若想查询存储桶列表,在您进行 授权策略 时,action 需要设置为 cos:GetService,更多授权请参见 支持 CAM 的业务接口

相关示例

功能名称
描述
示例代码
查询存储桶列表
查询指定账号下所有的存储桶列表

前期准备

使用永久密钥创建 CosAPI

调用 COS 的接口之前,必须先创建一个 CosAPI 的实例,用于后续调用请求。
qcloud_cos::CosAPI InitCosAPI() {
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";// bucket 的地域,请参见 https://cloud.tencent.com/document/product/436/62
std::string secret_id = "************************************"; //用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
std::string secret_key = "************************************"; //用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140
qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

使用临时密钥创建 CosAPI

如果要使用临时密钥请求 COS,则需要用临时密钥创建 CosAPI 实例。
qcloud_cos::CosAPI InitCosAPI() {
// 需要已经获取到临时密钥的结果:tmp_secret_id、tmp_secret_key、
// 临时密钥的生成参见 https://cloud.tencent.com/document/product/436/14048#cos-sts-sdk
uint64_t appid = 12500000000;
std::string region = "ap-guangzhou";
std::string tmp_secret_id = "************************************";
std::string tmp_secret_key = "************************************";
std::string tmp_token = "token";
qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
config.SetTmpToken(tmp_token);
qcloud_cos::CosAPI cos_tmp(config);
return cos_tmp;
}

使用案例:查询存储桶列表

查询指定账号下所有的存储桶列表。

方法原型

CosResult CosAPI::GetService(const GetServiceReq& req, GetServiceResp* resp)

请求示例

void GetService(qcloud_cos::CosAPI& cos) {
qcloud_cos::GetServiceReq req;
qcloud_cos::GetServiceResp resp;
qcloud_cos::CosResult result = cos.GetService(req, &resp);

std::cout << "===================GetService====================="
<< std::endl;
PrintResult(result, resp);
const qcloud_cos::Owner& owner = resp.GetOwner();
const std::vector<qcloud_cos::Bucket>& buckets = resp.GetBuckets();
std::cout << "owner.m_id=" << owner.m_id << ", owner.display_name=" << owner.m_display_name << std::endl;
for (std::vector<qcloud_cos::Bucket>::const_iterator itr = buckets.begin();
itr != buckets.end(); ++itr) {
const qcloud_cos::Bucket& bucket = *itr;
std::cout << "Bucket name=" << bucket.m_name
<< ", location=" << bucket.m_location
<< ", create_date=" << bucket.m_create_date << std::endl;
}
std::cout << "=========================================================" << std::endl;
}

参数说明

参数名称
描述
类型
req
查询存储桶列表请求。
GetServiceReq
resp
查询存储桶列表响应。
GetServiceResp
GetServiceResp 成员或函数说明:
成员或函数
描述
参数类型
GetXCosRequestId
获取请求 ID。
string
GetOwner
Owner 持有者的信息。
struct
GetBuckets
Bucket 列表信息。
vector

返回说明

CosResult 主要成员函数说明如下:
成员函数
描述
返回类型
IsSucc
判断是否成功,成功返回 true,失败返回 false。
bool
GetHttpStatus
获取 HTTP 状态码。
int
GetErrorCode
获取请求失败时获取错误码。
string
GetErrorMsg
获取请求失败时获取错误信息。
string
GetXCosRequestId
获取请求 ID。
string
对 CosResult 的使用样例如下,用户可根据信息选择使用:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
if (result.IsSucc()) {
std::cout << "Request Succ." << std::endl;
std::cout << resp.DebugString() << std::endl;
} else {
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
}
}

API 操作

关于查询存储桶列表的 API 接口说明,请参见 GET Service (List Buckets) 文档。