select_objec_demo.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 SelectObjectContentDemo(qcloud_cos::CosAPI& cos) {
  50. std::string object_name = "test.csv.gz";
  51. int input_file_type = CSV; // 待检索对象的格式为CSV或者JSON
  52. int input_compress_type = COMPRESS_GZIP; // 压缩类型,COMPRESS_NONE, COMPRESS_GZIP, COMPRESS_BZIP2
  53. int out_file_type = CSV; // 输出格式为CSV或者JSON
  54. qcloud_cos::SelectObjectContentReq req(bucket_name, object_name, input_file_type, input_compress_type, out_file_type);
  55. req.SetSqlExpression("Select * from COSObject");
  56. qcloud_cos::SelectObjectContentResp resp;
  57. qcloud_cos::CosResult result = cos.SelectObjectContent(req, &resp);
  58. std::cout << "=====================IsObjectExist=======================" << std::endl;
  59. PrintResult(result, resp);
  60. // 支持打印最终结果至终端或写入本地文件
  61. // resp.WriteResultToLocalFile("file_name");
  62. resp.PrintResult();
  63. std::cout << "=========================================================" << std::endl;
  64. }
  65. int main() {
  66. qcloud_cos::CosAPI cos = InitCosAPI();
  67. CosSysConfig::SetLogLevel((LOG_LEVEL)COS_LOG_ERR);
  68. SelectObjectContentDemo(cos);
  69. }