put_object_demo.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. */
  15. using namespace qcloud_cos;
  16. uint64_t appid = 12500000000;
  17. std::string tmp_secret_id = "AKIDXXXXXXXX";
  18. std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
  19. std::string region = "ap-guangzhou";
  20. std::string bucket_name = "examplebucket-12500000000";
  21. std::string tmp_token = "token";
  22. /*
  23. * 本方法包含调用是否正常的判断,和请求结果的输出
  24. * 可通过本方法判断是否请求成功,并输出结果信息
  25. */
  26. void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
  27. if (result.IsSucc()) {
  28. std::cout << "Request Succ." << std::endl;
  29. std::cout << resp.DebugString() << std::endl;
  30. } else {
  31. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  32. std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
  33. std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
  34. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  35. std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
  36. std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
  37. std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
  38. }
  39. }
  40. /*
  41. * 通过参数形式初始化 CosAPI 对象
  42. */
  43. qcloud_cos::CosAPI InitCosAPI() {
  44. qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
  45. config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
  46. qcloud_cos::CosAPI cos_tmp(config);
  47. return cos_tmp;
  48. }
  49. void PutObjectByFileDemo(qcloud_cos::CosAPI& cos) {
  50. std::string object_name = "test.txt";
  51. std::string file_path = "./test_file/text.txt";
  52. qcloud_cos::PutObjectByFileReq req(bucket_name, object_name, file_path);
  53. // 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
  54. // uint64_t traffic_limit = 0;
  55. // req.SetTrafficLimit(traffic_limit);
  56. qcloud_cos::PutObjectByFileResp resp;
  57. qcloud_cos::CosResult result = cos.PutObject(req, &resp);
  58. std::cout << "====================PutObjectByFile======================" << std::endl;
  59. PrintResult(result, resp);
  60. std::cout << "=========================================================" << std::endl;
  61. }
  62. void PutObjectByStreamDemo(qcloud_cos::CosAPI& cos) {
  63. std::istringstream iss("put object");
  64. std::string object_name = "text.txt";
  65. qcloud_cos::PutObjectByStreamReq req(bucket_name, object_name, iss);
  66. // 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
  67. // uint64_t traffic_limit = 0;
  68. // req.SetTrafficLimit(traffic_limit);
  69. qcloud_cos::PutObjectByStreamResp resp;
  70. qcloud_cos::CosResult result = cos.PutObject(req, &resp);
  71. std::cout << "===================PutObjectByStream=====================" << std::endl;
  72. PrintResult(result, resp);
  73. std::cout << "=========================================================" << std::endl;
  74. }
  75. void PutDirectoryDemo(qcloud_cos::CosAPI& cos) {
  76. std::string directory_name = "test_dir/"; // 目录名称,注意末尾需要有/
  77. PutDirectoryReq req(bucket_name, directory_name);
  78. PutDirectoryResp resp;
  79. CosResult result = cos.PutDirectory(req, &resp);
  80. std::cout << "=====================PutDirectory========================" << std::endl;
  81. PrintResult(result, resp);
  82. std::cout << "=========================================================" << std::endl;
  83. }
  84. /*
  85. * 该 Demo 示范如何上传本地文件夹到 cos
  86. * 本示例会将本地 test_file 目录上传到 cos 的 test_dir下。
  87. * 例如,本地当前路径的 test_file 目录下有 text1.txt 和 text2.txt 文件,上传后 cos 的路径为 test_dir/text1.txt 和 test_dir/text2.txt
  88. */
  89. void PutObjectsFromDirectoryDemo(qcloud_cos::CosAPI& cos) {
  90. std::string directory_name = "test_file"; // 目录名称,注意开头如果加./ 则上传后cos 的路径为 test_dir/test_file/text.txt
  91. std::string cos_path = "test_dir/"; // 目录名称,注意末尾需要有/
  92. PutObjectsByDirectoryReq req(bucket_name, directory_name);
  93. req.SetCosPath(cos_path);
  94. PutObjectsByDirectoryResp resp;
  95. CosResult result = cos.PutObjects(req, &resp);
  96. std::cout << "=====================PutDirectory========================" << std::endl;
  97. if (result.IsSucc()) {
  98. std::cout << "PutObjectsFromDirectory Succ." << std::endl;
  99. for (auto& r : resp.m_succ_put_objs) {
  100. std::cout << "file_name: " << r.m_file_name << "\nobject_name: " << r.m_object_name << "\nETag: " << r.m_cos_resp.GetEtag() << std::endl;
  101. std::cout << "====================================" << std::endl;
  102. }
  103. } else {
  104. std::cout << "PutObjectsFromDirectoryToCosPath Fail, ErrorMsg: "
  105. << result.GetErrorMsg() << std::endl;
  106. }
  107. std::cout << "=========================================================" << std::endl;
  108. }
  109. int main() {
  110. qcloud_cos::CosAPI cos = InitCosAPI();
  111. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  112. PutObjectByFileDemo(cos);
  113. PutObjectByStreamDemo(cos);
  114. PutDirectoryDemo(cos);
  115. PutObjectsFromDirectoryDemo(cos);
  116. }