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+ /* *
13+ * 本样例演示了如何使用 COS C++ SDK 进行对象检索
14+ * 包括:对象检索
15+ */
16+ using namespace qcloud_cos ;
17+
18+ uint64_t appid = 12500000000 ;
19+ std::string tmp_secret_id = " AKIDXXXXXXXX" ;
20+ std::string tmp_secret_key = " 1A2Z3YYYYYYYYYY" ;
21+ std::string region = " ap-guangzhou" ;
22+ std::string bucket_name = " examplebucket-12500000000" ;
23+ std::string tmp_token = " token" ;
24+
25+ /*
26+ * 本方法包含调用是否正常的判断,和请求结果的输出
27+ * 可通过本方法判断是否请求成功,并输出结果信息
28+ */
29+ void PrintResult (const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {
30+ if (result.IsSucc ()) {
31+ std::cout << " Request Succ." << std::endl;
32+ std::cout << resp.DebugString () << std::endl;
33+ } else {
34+ std::cout << " ErrorMsg=" << result.GetErrorMsg () << std::endl;
35+ std::cout << " HttpStatus=" << result.GetHttpStatus () << std::endl;
36+ std::cout << " ErrorCode=" << result.GetErrorCode () << std::endl;
37+ std::cout << " ErrorMsg=" << result.GetErrorMsg () << std::endl;
38+ std::cout << " ResourceAddr=" << result.GetResourceAddr () << std::endl;
39+ std::cout << " XCosRequestId=" << result.GetXCosRequestId () << std::endl;
40+ std::cout << " XCosTraceId=" << result.GetXCosTraceId () << std::endl;
41+ }
42+ }
43+
44+ /*
45+ * 通过参数形式初始化 CosAPI 对象
46+ */
47+ qcloud_cos::CosAPI InitCosAPI () {
48+ qcloud_cos::CosConfig config (appid, tmp_secret_id, tmp_secret_key, region);
49+ config.SetTmpToken (tmp_token); // 推荐使用临时密钥初始化 CosAPI 对象, 如果您使用永久密钥初始化 CosAPI 对象,请注释
50+ qcloud_cos::CosAPI cos_tmp (config);
51+ return cos_tmp;
52+ }
53+
54+ void SelectObjectContentDemo (qcloud_cos::CosAPI& cos) {
55+ std::string object_name = " test.csv.gz" ;
56+ int input_file_type = CSV; // 待检索对象的格式为CSV或者JSON
57+ int input_compress_type = COMPRESS_GZIP; // 压缩类型,COMPRESS_NONE, COMPRESS_GZIP, COMPRESS_BZIP2
58+ int out_file_type = CSV; // 输出格式为CSV或者JSON
59+
60+ qcloud_cos::SelectObjectContentReq req (bucket_name, object_name, input_file_type, input_compress_type, out_file_type);
61+ req.SetSqlExpression (" Select * from COSObject" );
62+ qcloud_cos::SelectObjectContentResp resp;
63+ qcloud_cos::CosResult result = cos.SelectObjectContent (req, &resp);
64+
65+ std::cout << " =====================IsObjectExist=======================" << std::endl;
66+ PrintResult (result, resp);
67+ // 支持打印最终结果至终端或写入本地文件
68+ // resp.WriteResultToLocalFile("file_name");
69+ resp.PrintResult ();
70+ std::cout << " =========================================================" << std::endl;
71+ }
72+
73+ int main () {
74+ qcloud_cos::CosAPI cos = InitCosAPI ();
75+ CosSysConfig::SetLogLevel ((LOG_LEVEL)COS_LOG_ERR);
76+ SelectObjectContentDemo (cos);
77+ }
0 commit comments