get_object_demo.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include <openssl/ssl.h>
  12. /**
  13. * 本样例演示了如何使用 COS C++ SDK 进行简单下载和列出
  14. * 包括:下载到本地文件、下载到流、列出桶下的文件
  15. */
  16. using namespace qcloud_cos;
  17. uint64_t appid = 12500000000;
  18. std::string tmp_secret_id = "AKIDXXXXXXXX";
  19. std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";
  20. std::string region = "ap-guangzhou";
  21. std::string bucket_name = "examplebucket-12500000000";
  22. std::string tmp_token = "token";
  23. /**
  24. * 本方法为 SSL_CTX 的回调方法,用户可以在此方法中配置 SSL_CTX 信息
  25. */
  26. int SslCtxCallback(void *ssl_ctx, void *data) {
  27. std::cout << "ssl_ctx: " << ssl_ctx << " data: " << data << std::endl;
  28. SSL_CTX *ctx = (SSL_CTX *)ssl_ctx;
  29. std::cout << "ssl_ctx in" << std::endl;
  30. SSL_CTX_use_PrivateKey_file(ctx, "/data/cert/client.key", SSL_FILETYPE_PEM);
  31. SSL_CTX_use_certificate_chain_file(ctx, "/data/cert/client.crt");
  32. std::cout << "ssl_ctx out" << std::endl;
  33. return 0;
  34. }
  35. /*
  36. * 本方法包含调用是否正常的判断,和请求结果的输出
  37. * 可通过本方法判断是否请求成功,并输出结果信息
  38. */
  39. void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
  40. if (result.IsSucc()) {
  41. std::cout << "Request Succ." << std::endl;
  42. std::cout << resp.DebugString() << std::endl;
  43. } else {
  44. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  45. std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;
  46. std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;
  47. std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;
  48. std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;
  49. std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;
  50. std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;
  51. }
  52. }
  53. /*
  54. * 通过参数形式初始化 CosAPI 对象
  55. */
  56. qcloud_cos::CosAPI InitCosAPI() {
  57. qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);
  58. config.SetTmpToken(tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
  59. qcloud_cos::CosAPI cos_tmp(config);
  60. return cos_tmp;
  61. }
  62. void GetObjectByFileDemo(qcloud_cos::CosAPI& cos) {
  63. std::string object_name = "test_src.txt";
  64. std::string file_path = "./test_file/text2.txt";
  65. qcloud_cos::GetObjectByFileReq req(bucket_name, object_name, file_path);
  66. // 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
  67. // uint64_t traffic_limit = 0;
  68. // req.SetTrafficLimit(traffic_limit);
  69. qcloud_cos::GetObjectByFileResp resp;
  70. qcloud_cos::CosResult result = cos.GetObject(req, &resp);
  71. std::cout << "===================GetObjectResponse=====================" << std::endl;
  72. PrintResult(result, resp);
  73. std::cout << "=========================================================" << std::endl;
  74. }
  75. void GetObjectByStreamDemo(qcloud_cos::CosAPI& cos) {
  76. std::string object_name = "test.txt";
  77. std::ostringstream os;
  78. qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
  79. // 限速上传对象,默认单位为 bit/s,限速值设置范围为 819200 - 838860800
  80. // uint64_t traffic_limit = 0;
  81. // req.SetTrafficLimit(traffic_limit);
  82. qcloud_cos::GetObjectByStreamResp resp;
  83. qcloud_cos::CosResult result = cos.GetObject(req, &resp);
  84. std::cout << "===================GetObjectResponse=====================" << std::endl;
  85. PrintResult(result, resp);
  86. std::cout << "=========================================================" << std::endl;
  87. std::cout << os.str() << std::endl;
  88. }
  89. /**
  90. * 使用 https 双向认证
  91. */
  92. void GetObjectByStreamDemoWithMutualAuthentication(qcloud_cos::CosAPI& cos) {
  93. std::string object_name = "index.html";
  94. std::ostringstream os;
  95. qcloud_cos::GetObjectByStreamReq req(bucket_name, object_name, os);
  96. req.SetHttps();
  97. req.SetSSLCtxCallback(SslCtxCallback, nullptr);
  98. qcloud_cos::GetObjectByStreamResp resp;
  99. qcloud_cos::CosResult result = cos.GetObject(req, &resp);
  100. std::cout << "===================GetObjectResponse=====================" << std::endl;
  101. PrintResult(result, resp);
  102. std::cout << "=========================================================" << std::endl;
  103. std::cout << os.str() << std::endl;
  104. }
  105. void GetBucketDemo(qcloud_cos::CosAPI& cos) {
  106. qcloud_cos::GetBucketReq req(bucket_name);
  107. // 设置列出的对象名以 prefix 为前缀
  108. req.SetPrefix("test");
  109. // 设置最大列出多少个对象, 一次 listobject 最大支持1000
  110. req.SetMaxKeys(10);
  111. qcloud_cos::GetBucketResp resp;
  112. qcloud_cos::CosResult result = cos.GetBucket(req, &resp);
  113. std::cout << "===================GetBucketResponse=====================" << std::endl;
  114. if (result.IsSucc()) {
  115. // object contents 表示此次列出的对象列表
  116. std::vector<Content> contents = resp.GetContents();
  117. for (const Content& content : contents) {
  118. // 对象的 key
  119. std::string key = content.m_key;
  120. // 对象的 etag
  121. std::string etag = content.m_etag;
  122. // 对象的长度
  123. std::string file_size = content.m_size;
  124. // 对象的存储类型
  125. std::string storage_classes = content.m_storage_class;
  126. std::cout << "key:" << key << "\netag:" << etag << "\nfile_size:" << file_size << "\nstorage_classes" << storage_classes << std::endl;
  127. std::cout << "==================================" << std::endl;
  128. }
  129. if (resp.IsTruncated()) {
  130. // 表示还没有列完,被截断了
  131. // 下一次开始的位置
  132. std::string next_marker = resp.GetNextMarker();
  133. std::cout << "next_marker:" << next_marker << std::endl;
  134. }
  135. } else {
  136. std::cout << "GetBucket Fail, ErrorMsg: " << result.GetErrorMsg() << std::endl;
  137. }
  138. std::cout << "=========================================================" << std::endl;
  139. }
  140. int main() {
  141. qcloud_cos::CosAPI cos = InitCosAPI();
  142. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  143. GetObjectByFileDemo(cos);
  144. GetObjectByStreamDemo(cos);
  145. // GetObjectByStreamDemoWithMutualAuthentication(cos);
  146. GetBucketDemo(cos);
  147. }