检索对象内容

最近更新时间:2024-08-14 18:05:41

我的收藏

简介

本文档提供关于检索对象内容操作相关的 API 概览以及 SDK 示例代码。

注意事项

若您要使用检索对象内容功能,需要具有目标对象的读权限:在您进行 授权策略 时,action 需要设置为cos:GetObject,更多授权请参见 支持CAM的业务接口
COS Select 支持检索以下格式的对象数据:
JSON 格式:对象以 JSON 格式存储,可以是 JSON 文件或者 JSON 列表。
JSON 对象需要以 UTF-8 格式编码。
COS Select 支持检索 GZIP 或者 BZIP2 压缩的 JSON 对象。
COS Select 支持检索 SSE-COS 加密的 JSON 对象。

相关示例

功能名称
描述
示例代码
检索对象内容
从指定对象(JSON 格式)中检索内容

前期准备:初始化 COS 服务实例

public class UploadObject { private CosXml cosXml; //将服务用户设置成数据成员 // 初始化COS服务实例 private void InitCosXml() { string region = Environment.GetEnvironmentVariable("COS_REGION"); CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(region) // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 .Build(); string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); this.cosXml = new CosXmlServer(config, qCloudCredentialProvider); } }

使用案例

检索对象内容

public void SelectObject() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; string key = "exampleObject"; //对象键,文件内容格式为json SelectObjectRequest request = new SelectObjectRequest(bucket, key);
ObjectSelectionFormat.JSONFormat jSONFormat = new ObjectSelectionFormat.JSONFormat(); jSONFormat.Type = "DOCUMENT"; jSONFormat.RecordDelimiter = "\\n"; string outputFile = "select_local_file.json"; request.SetExpression("Select * from COSObject") .SetInputFormat(new ObjectSelectionFormat(null, jSONFormat)) .SetOutputFormat(new ObjectSelectionFormat(null, jSONFormat)) .OutputToFile(outputFile); request.SetCosProgressCallback(delegate(long progress, long total) { Console.WriteLine("OnProgress : " + progress + "," + total); }); SelectObjectResult selectObjectResult = cosXml.SelectObject(request); Console.WriteLine(selectObjectResult.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx.StackTrace); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

API 操作

关于检索对象内容涉及的 API 接口说明,请参见 SELECT Object Content