put_bucket_demo.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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@xxx";
  19. std::string bucket_name = "examplebucket-12500000000";
  20. std::string tmp_token = "token";
  21. /*
  22. * 本方法包含调用是否正常的判断,和请求结果的输出
  23. * 可通过本方法判断是否请求成功,并输出结果信息
  24. */
  25. void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
  26. if (result.IsSucc()) {
  27. std::cout << "Request Succ." << std::endl;
  28. std::cout << resp.DebugString() << std::endl;
  29. } else {
  30. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  31. std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
  32. std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
  33. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  34. std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
  35. std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
  36. std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
  37. }
  38. }
  39. /*
  40. * 通过参数形式初始化 CosAPI 对象
  41. */
  42. qcloud_cos::CosAPI InitCosAPI() {
  43. qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
  44. config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
  45. qcloud_cos::CosAPI cos_tmp(config);
  46. return cos_tmp;
  47. }
  48. void PutBucket(qcloud_cos::CosAPI& cos) {
  49. qcloud_cos::PutBucketReq req(bucket_name);
  50. //创建MAZ存储桶使用下方进行设置
  51. //req.SetMAZBucket();
  52. qcloud_cos::PutBucketResp resp;
  53. qcloud_cos::CosResult result = cos.PutBucket(req, &resp);
  54. std::cout << "===================PutBucketResponse====================="
  55. << std::endl;
  56. PrintResult(result, resp);
  57. std::cout << "========================================================="
  58. << std::endl;
  59. }
  60. int main() {
  61. try{
  62. qcloud_cos::CosAPI cos = InitCosAPI();
  63. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  64. PutBucket(cos);
  65. }
  66. catch(const std::exception& e){
  67. std::cerr << e.what() << "]]" << '\n';
  68. std::cout << (strcmp(e.what(),"Invalid region configuration in CosConfig :ap-guangzhou@xxx") == 0) << std::endl;
  69. }
  70. std::cout << "Hello, World!" << std::endl;
  71. }