put_get_object_acl_demo.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 进行对象 ACL 的设置和获取
  13. * 包括:通过 body 和 head 两种方式设置对象的 ACL、获取对象的 ACL
  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. std::string uin = "23300000000";
  23. std::string sub_uin = "23300000000";
  24. /*
  25. * 本方法包含调用是否正常的判断,和请求结果的输出
  26. * 可通过本方法判断是否请求成功,并输出结果信息
  27. */
  28. void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
  29. if (result.IsSucc()) {
  30. std::cout << "Request Succ." << std::endl;
  31. std::cout << resp.DebugString() << std::endl;
  32. } else {
  33. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  34. std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
  35. std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
  36. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  37. std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
  38. std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
  39. std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
  40. }
  41. }
  42. /*
  43. * 通过参数形式初始化 CosAPI 对象
  44. */
  45. qcloud_cos::CosAPI InitCosAPI() {
  46. qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
  47. config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
  48. qcloud_cos::CosAPI cos_tmp(config);
  49. return cos_tmp;
  50. }
  51. /*
  52. * 该 Demo 示范如何上传对象的 ACL
  53. * 本样例先通过 body 的方式为主账号持有的对象设置子账号 FULL_CONTROL 访问权限。再通过 head 的方式设置子账号 READ 访问权限
  54. */
  55. void PutObjectACLDemo(qcloud_cos::CosAPI& cos) {
  56. std::string object_name = "test.txt";
  57. // 1 通过Body设置ACL配置 (设置ACL可以通过Body、Header两种方式,但只能二选一,否则会有冲突)
  58. // ACL 请求构造参考 api 文档:https://cloud.tencent.com/document/product/436/7748
  59. {
  60. qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
  61. qcloud_cos::Owner owner = {"qcs::cam::uin/" + uin + ":uin/" + uin,
  62. "testacl"};
  63. qcloud_cos::Grant grant;
  64. req.SetOwner(owner);
  65. grant.m_grantee.m_type = "CanonicalUser";
  66. grant.m_grantee.m_id = "qcs::cam::uin/" + uin + ":uin/" + sub_uin;
  67. grant.m_perm = "FULL_CONTROL";
  68. req.AddAccessControlList(grant);// 可以加入多条grant
  69. qcloud_cos::PutObjectACLResp resp;
  70. qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
  71. std::cout << "===================PutObjectACLResponse======================" << std::endl;
  72. PrintResult(result, resp);
  73. std::cout << "=============================================================" << std::endl;
  74. }
  75. // 2 通过Header设置ACL配置
  76. {
  77. qcloud_cos::PutObjectACLReq req(bucket_name, object_name);
  78. req.SetXCosAcl("default"); // 对应 api 中的 x-cos-acl
  79. std::string grant_read = "id=\"" + uin + "/"+ sub_uin + "\"";
  80. req.SetXCosGrantRead(grant_read); // 对应 api 中的 x-cos-grant-read
  81. // std::string grant_full_control = "id=\"" + uin + "/"+ sub_uin + "/\"";
  82. // req.SetXCosGrantFullControl(grant_full_control); // 对应 api 中的 x-cos-grant-full-control
  83. qcloud_cos::PutObjectACLResp resp;
  84. qcloud_cos::CosResult result = cos.PutObjectACL(req, &resp);
  85. std::cout << "===================PutObjectACLResponse======================" << std::endl;
  86. PrintResult(result, resp);
  87. std::cout << "=============================================================" << std::endl;
  88. }
  89. }
  90. void GetObjectACLDemo(qcloud_cos::CosAPI& cos) {
  91. std::string object_name = "test.txt";
  92. qcloud_cos::GetObjectACLReq req(bucket_name, object_name);
  93. qcloud_cos::GetObjectACLResp resp;
  94. qcloud_cos::CosResult result = cos.GetObjectACL(req, &resp);
  95. std::cout << "======================GetObjectACLResponse========================" << std::endl;
  96. PrintResult(result, resp);
  97. std::cout << "==================================================================" << std::endl;
  98. }
  99. int main() {
  100. qcloud_cos::CosAPI cos = InitCosAPI();
  101. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  102. PutObjectACLDemo(cos);
  103. GetObjectACLDemo(cos);
  104. }