get_bucket_list_demo.cpp 2.9 KB

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