get_object_url_demo.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * 包括:预签名链接、对象访问 URL
  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 GeneratePresignedUrlDemo(qcloud_cos::CosAPI& cos) {
  50. std::string object_name = "test.txt";
  51. qcloud_cos::GeneratePresignedUrlReq req(bucket_name, object_name, qcloud_cos::HTTP_GET); // 可设置请求方法
  52. // 下方代码块可选则是否设置。默认签名生效的开始时间是本地当前时间,默认有效期 60s
  53. {
  54. req.SetHttps(); // 是否为 https
  55. uint64_t start_time_in_s = time(NULL);
  56. req.SetStartTimeInSec(start_time_in_s); // 设置签名生效的开始时间
  57. req.SetExpiredTimeInSec(60); // 设置签名的有效期
  58. }
  59. std::string presigned_url = cos.GeneratePresignedUrl(req);
  60. std::cout << "===================GeneratePresignedUrl=====================" << std::endl;
  61. std::cout << "Presigend Url=[" << presigned_url << "]" << std::endl;
  62. std::cout << "============================================================" << std::endl;
  63. }
  64. void GetObjectUrlDemo(qcloud_cos::CosAPI& cos) {
  65. std::string object_name = "test.txt";
  66. bool is_https = true; // 是否为 https
  67. std::string object_url = cos.GetObjectUrl(bucket_name, object_name, is_https);
  68. std::cout << "=======================GetObjectUrl=========================" << std::endl;
  69. std::cout << "object url=[" << object_url << "]" << std::endl;
  70. std::cout << "============================================================" << std::endl;
  71. }
  72. int main() {
  73. qcloud_cos::CosAPI cos = InitCosAPI();
  74. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  75. GeneratePresignedUrlDemo(cos);
  76. GetObjectUrlDemo(cos);
  77. }