delete_object_demo.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 DeleteObjectDemo(qcloud_cos::CosAPI& cos) {
  50. std::string object_name = "test.txt";
  51. qcloud_cos::DeleteObjectReq req(bucket_name, object_name);
  52. // req.SetXCosVersionId("xxxxx");// 可以指定删除的版本
  53. qcloud_cos::DeleteObjectResp resp;
  54. qcloud_cos::CosResult result = cos.DeleteObject(req, &resp);
  55. std::cout << "===================DeleteObjectResponse=====================" << std::endl;
  56. PrintResult(result, resp);
  57. std::cout << "============================================================" << std::endl;
  58. }
  59. /*
  60. * 该 Demo 示范如何批量删除对象
  61. * 该方法是否成功,需要判断resp.GetErrorMsgs()是否为空
  62. */
  63. void DeleteObjectsDemo(qcloud_cos::CosAPI& cos) {
  64. std::vector<ObjectVersionPair> to_be_deleted;
  65. {
  66. ObjectVersionPair pair;
  67. std::string object_name = "test_dir/audio.mp3";
  68. std::string version_id = ""; // 有需要可以指定删除的版本,如果为空则不指定
  69. to_be_deleted.push_back(ObjectVersionPair(object_name, version_id));
  70. }
  71. {
  72. std::string object_name = "test_dir/video.mp4";
  73. std::string version_id = ""; // 有需要可以指定删除的版本,如果为空则不指定
  74. to_be_deleted.push_back(ObjectVersionPair(object_name, version_id));
  75. }
  76. qcloud_cos::DeleteObjectsReq req(bucket_name, to_be_deleted);
  77. // req.SetQuiet(); // 设置为 Quiet模式, 则不返回删除成功的对象信息,默认为 Verbose 模式
  78. qcloud_cos::DeleteObjectsResp resp;
  79. qcloud_cos::CosResult result = cos.DeleteObjects(req, &resp);
  80. std::cout << "===================DeleteObjectsResponse=====================" << std::endl;
  81. std::vector<DeletedInfo> deleted_infos = resp.GetDeletedInfos(); // 单个删除成功的对象条目,仅当使用 Verbose 模式才会返回该元素
  82. std::vector<ErrorInfo> error_infos = resp.GetErrorMsgs(); // 单个删除失败的对象条目
  83. if (!error_infos.empty()) {
  84. std::cout << "==================Failed part message==================" << std::endl;
  85. for (ErrorInfo& error_info : error_infos) {
  86. std::cout << "key: " << error_info.m_key << "\ncode: " << error_info.m_code << "\nmessage: " << error_info.m_message << std::endl;
  87. std::cout << "====================================" << std::endl;
  88. }
  89. } else {
  90. std::cout << "DeleteObjects All Succ." << std::endl;
  91. }
  92. std::cout << "=============================================================" << std::endl;
  93. }
  94. void DeleteDirectoryDemo(qcloud_cos::CosAPI& cos) {
  95. std::string directory_name = "test_dir/"; // 目录名称,注意末尾需要有/
  96. DeleteObjectsByPrefixReq req(bucket_name, directory_name);
  97. DeleteObjectsByPrefixResp resp;
  98. CosResult result = cos.DeleteObjects(req, &resp);
  99. std::cout << "===================DeleteDirectory=====================" << std::endl;
  100. if (result.IsSucc()) {
  101. std::cout << "DeleteDirectory Succ." << std::endl;
  102. } else {
  103. std::cout << "DeleteDirectory Fail, ErrorMsg: " << result.GetErrorMsg() << std::endl;
  104. }
  105. std::cout << "Succ del objs:" << std::endl;
  106. for (auto& obj : resp.m_succ_del_objs) {
  107. std::cout << obj << std::endl;
  108. }
  109. std::cout << "=======================================================" << std::endl;
  110. }
  111. void DeleteObjectsByPrefixDemo(qcloud_cos::CosAPI& cos) {
  112. std::string prefix = "test_dir"; // 指定前缀,test_dir目录下的所有对象均会被删除,且以test_dir开头的对象也会被删除
  113. DeleteObjectsByPrefixReq req(bucket_name, prefix);
  114. DeleteObjectsByPrefixResp resp;
  115. CosResult result = cos.DeleteObjects(req, &resp);
  116. std::cout << "===================DeleteObjectsByPrefix=====================" << std::endl;
  117. if (result.IsSucc()) {
  118. std::cout << "DeleteObjectsByPrefix Succ." << std::endl;
  119. } else {
  120. std::cout << "DeleteObjectsByPrefix Fail, ErrorMsg: "
  121. << result.GetErrorMsg() << std::endl;
  122. }
  123. std::cout << "Succ del objs:" << std::endl;
  124. for (auto& obj : resp.m_succ_del_objs) {
  125. std::cout << obj << std::endl;
  126. }
  127. std::cout << "=============================================================" << std::endl;
  128. }
  129. int main() {
  130. qcloud_cos::CosAPI cos = InitCosAPI();
  131. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  132. DeleteObjectDemo(cos);
  133. DeleteObjectsDemo(cos);
  134. DeleteDirectoryDemo(cos);
  135. DeleteObjectsByPrefixDemo(cos);
  136. }