简介
本文介绍对象存储 COS 通过 C++ SDK 实现检索对象内容的示例代码和描述。
注意事项
若您要使用检索对象内容功能,需要具有目标对象的读权限:在您进行 授权策略 时,action 需要设置为
cos:GetObject
,更多授权请参见 支持CAM的业务接口。COS Select 支持检索以下格式的对象数据:
CSV 格式:对象以 CSV 格式存储,并以固定的分隔符划分;
JSON 格式:对象以 JSON 格式存储,可以是 JSON 文件或者 JSON 列表。
CSV、JSON 对象需要以 UTF-8 格式编码。
COS Select 支持检索 GZIP 或者 BZIP2 压缩的 CSV、JSON 对象。
COS Select 支持检索 SSE-COS 加密的 CSV、JSON 对象。
相关示例
功能名称 | 描述 | 示例代码 |
检索对象内容 | 使用结构化查询语句(Structured Query Language,SQL)从指定对象(CSV、JSON 或者 Parquet 格式)中检索内容。 |
前期准备
创建 CosAPI
调用 COS 的接口之前,必须先创建一个 CosAPI 的实例。这个实例用来后续调用请求。
qcloud_cos::CosAPI InitCosAPI() {uint64_t appid = 12500000000;std::string region = "ap-guangzhou";// bucket 的地域,请参见 https://cloud.tencent.com/document/product/436/62std::string secret_id = "AKIDXXXXXXXX"; //用户的 SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140std::string secret_key = "1A2Z3YYYYYYYYYY"; //用户的 SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参见 https://cloud.tencent.com/document/product/598/37140qcloud_cos::CosConfig config(appid, secret_id, secret_key, region);qcloud_cos::CosAPI cos_tmp(config);return cos_tmp;}
使用临时密钥创建 CosAPI
如果要使用临时密钥请求 COS,则需要用临时密钥创建 CosAPI 实例。
qcloud_cos::CosAPI InitCosAPI() {// 需要已经获取到临时密钥的结果:tmp_secret_id、tmp_secret_key、// 临时密钥的生成参见 https://cloud.tencent.com/document/product/436/14048#cos-sts-sdkuint64_t appid = 12500000000;std::string region = "ap-guangzhou";std::string tmp_secret_id = "AKIDXXXXXXXX";std::string tmp_secret_key = "1A2Z3YYYYYYYYYY";std::string tmp_token = "token";qcloud_cos::CosConfig config(appid, tmp_secret_id, tmp_secret_key, region);config.SetTmpToken(tmp_token);qcloud_cos::CosAPI cos_tmp(config);return cos_tmp;}
使用案例
检索 CSV 格式
方法原型
CosResult CosAPI::SelectObjectContent(const SelectObjectContentReq& req, SelectObjectContentResp* resp)
请求示例
void SelectObjectContentDemo(qcloud_cos::CosAPI& cos) {std::string object_name = "test.csv.gz";int input_file_type = CSV; // 待检索对象的格式为CSV或者JSONint input_compress_type = COMPRESS_GZIP; // 压缩类型,COMPRESS_NONE, COMPRESS_GZIP, COMPRESS_BZIP2int out_file_type = CSV; // 输出格式为CSV或者JSONqcloud_cos::SelectObjectContentReq req(bucket_name, object_name, input_file_type, input_compress_type, out_file_type);req.SetSqlExpression("Select * from COSObject");qcloud_cos::SelectObjectContentResp resp;qcloud_cos::CosResult result = cos.SelectObjectContent(req, &resp);std::cout << "=====================IsObjectExist=======================" << std::endl;PrintResult(result, resp);// 支持打印最终结果至终端或写入本地文件// resp.WriteResultToLocalFile("file_name");resp.PrintResult();std::cout << "=========================================================" << std::endl;}
参数说明
参数名称 | 描述 | 类型 |
req | 检索对象请求 | SelectObjectContentReq |
resp | 检索对象响应 | SelectObjectContentResp |
SelectObjectContentReq 成员或函数说明:
成员或函数 | 描述 | 参数类型 |
bucket_name | 存储桶名,可通过构造函数或 set 方法进行设置 | string |
object_name | 对象键(Key),可通过构造函数或 set 方法进行设置 是对象在存储桶中的唯一标识。例如,在对象的访问域名 examplebucket-1250000000.cos.ap-guangzhou.myqcloud.com/doc/picture.jpg 中,对象键为 doc/picture.jpg,详情请参见 对象键 | string |
input_file_type | 待检索对象的格式,可通过构造函数或 set 方法进行设置 | string |
input_compress_type | 压缩类型,可通过构造函数或 set 方法进行设置 | string |
out_file_type | 输出格式,可通过构造函数或 set 方法进行设置 | string |
SelectObjectContentResp 成员函数说明:
成员函数 | 描述 | 返回类型 |
PrintResult | 打印最终结果至终端 | 无 |
WriteResultToLocalFile | 最终结果写入本地文件 | 无 |
GetXCosRequestId | 获取请求 ID | string |
返回说明
CosResult 主要成员函数说明如下:
成员函数 | 描述 | 返回类型 |
IsSucc | 判断是否成功,成功返回 true,失败返回 false。 | bool |
GetHttpStatus | 获得 http 状态码。 | int |
GetErrorCode | 请求失败时获取错误码。 | string |
GetErrorMsg | 请求失败时获取错误信息。 | string |
GetXCosRequestId | 获取请求 ID。 | string |
对 CosResult 的使用样例如下,用户可根据信息选择使用:
void PrintResult(const qcloud_cos::CosResult& result, const qcloud_cos::BaseResp& resp) {if (result.IsSucc()) {std::cout << "Request Succ." << std::endl;std::cout << resp.DebugString() << std::endl;} else {std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;std::cout << "HttpStatus=" << result.GetHttpStatus() << std::endl;std::cout << "ErrorCode=" << result.GetErrorCode() << std::endl;std::cout << "ErrorMsg=" << result.GetErrorMsg() << std::endl;std::cout << "ResourceAddr=" << result.GetResourceAddr() << std::endl;std::cout << "XCosRequestId=" << result.GetXCosRequestId() << std::endl;std::cout << "XCosTraceId=" << result.GetXCosTraceId() << std::endl;}}