head_object_demo.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 进行 Head Object 相关操作
  13. * 包括:Head Object、判断对象是否存在
  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 IsObjectExistDemo(qcloud_cos::CosAPI& cos) {
  50. bool is_exist = cos.IsObjectExist(bucket_name, "test.txt");
  51. std::cout << "=====================IsObjectExist=======================" << std::endl;
  52. std::cout << (is_exist ? "true" : "false") << std::endl;
  53. std::cout << "=========================================================" << std::endl;
  54. }
  55. void HeadObjectDemo(qcloud_cos::CosAPI& cos) {
  56. std::string object_name = "test.txt";
  57. qcloud_cos::HeadObjectReq req(bucket_name, object_name);
  58. qcloud_cos::HeadObjectResp resp;
  59. qcloud_cos::CosResult result = cos.HeadObject(req, &resp);
  60. std::cout << "===================HeadObjectResponse=====================" << std::endl;
  61. std::map<std::string, std::string> cos_metas = resp.GetXCosMetas(); // 获取自定义的元数据map
  62. std::string cos_meta = resp.GetXCosMeta("x-cos-meta-*"); // 获取指定自定义的元数据
  63. std::string restore = resp.GetXCosRestore(); // 获得 archive 类型对象的当前恢复状态
  64. std::string sse = resp.GetXCosServerSideEncryption(); // Server端加密使用的算法
  65. PrintResult(result, resp);
  66. std::cout << "==========================================================" << std::endl;
  67. }
  68. int main() {
  69. qcloud_cos::CosAPI cos = InitCosAPI();
  70. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  71. IsObjectExistDemo(cos);
  72. HeadObjectDemo(cos);
  73. }