1+
2+ #include < stdlib.h>
3+ #include < sys/stat.h>
4+
5+ #include < iostream>
6+ #include < map>
7+ #include < string>
8+ #include < thread>
9+ #include < vector>
10+
11+ #include " cos_api.h"
12+ #include " cos_sys_config.h"
13+ #include " util/auth_tool.h"
14+
15+ /* *
16+ * 本样例演示了如何使用 COS C++ SDK 进行存储桶列表的获取
17+ */
18+ using namespace qcloud_cos ;
19+
20+ uint64_t appid = 12500000000 ;
21+ std::string tmp_secret_id = " AKIDXXXXXXXX" ;
22+ std::string tmp_secret_key = " 1A2Z3YYYYYYYYYY" ;
23+ std::string region = " ap-guangzhou" ;
24+ std::string tmp_token = " token" ;
25+
26+ /*
27+ * 本方法包含调用是否正常的判断,和请求结果的输出
28+ * 可通过本方法判断是否请求成功,并输出结果信息
29+ */
30+ void PrintResult (const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
31+ if (result.IsSucc ()) {
32+ std::cout << " Request Succ." << std::endl;
33+ std::cout << resp.DebugString () << std::endl;
34+ } else {
35+ std::cout << " ErrorMsg=" << result.GetErrorMsg () << std::endl;
36+ std::cout << " HttpStatus=" << result.GetHttpStatus () << std::endl;
37+ std::cout << " ErrorCode=" << result.GetErrorCode () << std::endl;
38+ std::cout << " ErrorMsg=" << result.GetErrorMsg () << std::endl;
39+ std::cout << " ResourceAddr=" << result.GetResourceAddr () << std::endl;
40+ std::cout << " XCosRequestId=" << result.GetXCosRequestId () << std::endl;
41+ std::cout << " XCosTraceId=" << result.GetXCosTraceId () << std::endl;
42+ }
43+ }
44+
45+ /*
46+ * 通过参数形式初始化 CosAPI 对象
47+ */
48+ qcloud_cos::CosAPI InitCosAPI () {
49+ qcloud_cos::CosConfig config (appid, tmp_secret_id, tmp_secret_key, region);
50+ config.SetTmpToken (tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
51+ qcloud_cos::CosAPI cos_tmp (config);
52+ return cos_tmp;
53+ }
54+
55+ void GetService (qcloud_cos::CosAPI& cos) {
56+ qcloud_cos::GetServiceReq req;
57+ qcloud_cos::GetServiceResp resp;
58+ qcloud_cos::CosResult result = cos.GetService (req, &resp);
59+
60+ std::cout << " ===================GetService====================="
61+ << std::endl;
62+ PrintResult (result, resp);
63+ const qcloud_cos::Owner& owner = resp.GetOwner ();
64+ const std::vector<qcloud_cos::Bucket>& buckets = resp.GetBuckets ();
65+ std::cout << " owner.m_id=" << owner.m_id << " , owner.display_name=" << owner.m_display_name << std::endl;
66+ for (std::vector<qcloud_cos::Bucket>::const_iterator itr = buckets.begin ();
67+ itr != buckets.end (); ++itr) {
68+ const qcloud_cos::Bucket& bucket = *itr;
69+ std::cout << " Bucket name=" << bucket.m_name
70+ << " , location=" << bucket.m_location
71+ << " , create_date=" << bucket.m_create_date << std::endl;
72+ }
73+ std::cout << " =========================================================" << std::endl;
74+ }
75+
76+ int main () {
77+ qcloud_cos::CosAPI cos = InitCosAPI ();
78+ CosSysConfig::SetLogLevel ((LOG_LEVEL)COS_LOG_ERR);
79+ GetService (cos);
80+ }
0 commit comments