本文档提供关于检索对象内容操作相关的 API 概览以及 SDK 示例代码。
API | 操作名 | 操作描述 |
---|---|---|
SELECT Object Content | 检索对象内容 | 从指定对象(CSV 格式或者 JSON 格式)中检索内容 |
简单操作由 COSClient 类型发起请求,使用简单操作之前必须先创建一个 COSClient 实例。
COSClient 实例是并发安全的,这里推荐一个进程只创建一个 COSClient 实例,当不会再通过这个实例发起请求的时候,再选择关闭这个实例。
调用 COS 的接口之前,必须先创建一个 COSClient 的实例。
// 创建 COSClient 实例,这个实例用来后续调用请求
COSClient createCOSClient() {
// 设置用户身份信息。
// SECRETID 和 SECRETKEY 请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
String secretId = "SECRETID";
String secretKey = "SECRETKEY";
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// ClientConfig 中包含了后续请求 COS 的客户端设置:
ClientConfig clientConfig = new ClientConfig();
// 设置 bucket 的地域
// COS_REGION 请参照 https://intl.cloud.tencent.com/document/product/436/6224
clientConfig.setRegion(new Region("COS_REGION"));
// 设置请求协议, http 或者 https
// 5.6.53 及更低的版本,建议设置使用 https 协议
// 5.6.54 及更高版本,默认使用了 https
clientConfig.setHttpProtocol(HttpProtocol.https);
// 以下的设置,是可选的:
// 设置 socket 读取超时,默认 30s
clientConfig.setSocketTimeout(30*1000);
// 设置建立连接超时,默认 30s
clientConfig.setConnectionTimeout(30*1000);
// 如果需要的话,设置 http 代理,ip 以及 port
clientConfig.setHttpProxyIp("httpProxyIp");
clientConfig.setHttpProxyPort(80);
// 生成 cos 客户端。
return new COSClient(cred, clientConfig);
}
如果要使用临时密钥请求 COS,则需要用临时密钥创建 COSClient。
本 SDK 并不能生成临时密钥,而需要使用额外的操作来生成,参考 临时密钥生成。
// 创建 COSClient 实例,这个实例用来后续调用请求
COSClient createCOSClient() {
// 这里需要已经获取到临时密钥的结果。
// 临时密钥的生成参考 https://intl.cloud.tencent.com/document/product/436/14048
String tmpSecretId = "TMPSECRETID";
String tmpSecretKey = "TMPSECRETKEY";
String sessionToken = "SESSIONTOKEN";
COSCredentials cred = new BasicSessionCredentials(tmpSecretId, tmpSecretKey, sessionToken);
// ClientConfig 中包含了后续请求 COS 的客户端设置:
ClientConfig clientConfig = new ClientConfig();
// 设置 bucket 的地域
// COS_REGION 请参照 https://intl.cloud.tencent.com/document/product/436/6224
clientConfig.setRegion(new Region("COS_REGION"));
// 设置请求协议, http 或者 https
// 5.6.53 及更低的版本,建议设置使用 https 协议
// 5.6.54 及更高版本,默认使用了 https
clientConfig.setHttpProtocol(HttpProtocol.https);
// 以下的设置,是可选的:
// 设置 socket 读取超时,默认 30s
clientConfig.setSocketTimeout(30*1000);
// 设置建立连接超时,默认 30s
clientConfig.setConnectionTimeout(30*1000);
// 如果需要的话,设置 http 代理,ip 以及 port
clientConfig.setHttpProxyIp("httpProxyIp");
clientConfig.setHttpProxyPort(80);
// 生成 cos 客户端。
return new COSClient(cred, clientConfig);
}
COS Select 支持检索以下格式的对象数据:
注意:
cos:GetObject
的授权。public SelectObjectContentResult selectObjectContent(SelectObjectContentRequest selectRequest) throws CosClientException, CosServiceException {
// 调用 COS 接口之前必须保证本进程存在一个 COSClient 实例,如果没有则创建
// 详细代码参见本页:简单操作 -> 创建 COSClient
COSClient cosClient = createCOSClient();
// 存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String bucketName = "examplebucket-1250000000";
// 对象键(Key)是对象在存储桶中的唯一标识。详情请参见 [对象键](https://intl.cloud.tencent.com/document/product/436/13324)
String key = "exampleobject";
String query = "select s._1 from COSObject s";
SelectObjectContentRequest request = new SelectObjectContentRequest();
request.setBucketName(bucketName);
request.setKey(key);
request.setExpression(query);
request.setExpressionType(ExpressionType.SQL);
InputSerialization inputSerialization = new InputSerialization();
CSVInput csvInput = new CSVInput();
csvInput.setFieldDelimiter(",");
csvInput.setRecordDelimiter("\n");
inputSerialization.setCsv(csvInput);
inputSerialization.setCompressionType(CompressionType.NONE);
request.setInputSerialization(inputSerialization);
OutputSerialization outputSerialization = new OutputSerialization();
outputSerialization.setCsv(new CSVOutput());
request.setOutputSerialization(outputSerialization);
final AtomicBoolean isResultComplete = new AtomicBoolean(false);
SelectObjectContentResult result = cosclient.selectObjectContent(request);
InputStream resultInputStream = result.getPayload().getRecordsInputStream(
new SelectObjectContentEventVisitor() {
@Override
public void visit(SelectObjectContentEvent.StatsEvent event)
{
System.out.println(
"Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned()
+ " Bytes Processed: " + event.getDetails().getBytesProcessed());
}
@Override
public void visit(SelectObjectContentEvent.EndEvent event)
{
isResultComplete.set(true);
System.out.println("Received End Event. Result is complete.");
}
}
);
BufferedReader reader = new BufferedReader(new InputStreamReader(resultInputStream));
StringBuffer stringBuffer = new StringBuffer();
String line;
while((line = reader.readLine())!= null){
stringBuffer.append(line).append("\n");
}
System.out.println(stringBuffer.toString());
// 检查结果是否接受完整
if (!isResultComplete.get()) {
throw new Exception("result was incomplete");
}
// 确认本进程不再使用 cosClient 实例之后,关闭之
cosClient.shutdown();
参数名称 | 参数描述 | 类型 |
---|---|---|
selectRequest | 请求 | SelectObjectContentRequest |
SelectObjectContentRequest 成员说明:
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucketName | set 方法 | Bucket 的命名格式为 BucketName-APPID ,详情请参见 命名规范 | String |
key | set 方法 | 指定分块上传到 COS 上的路径,即 对象键。例如对象键为 folder/picture.jpg | String |
expression | set 方法 | 请求表达式 | String |
expressionType | set 方法 | 请求表达式类型 | String |
inputSerialization | set 方法 | 描述待检索对象的格式 | InputSerialization |
outputSerialization | set 方法 | 描述检索结果的输出格式 | OutputSerialization |
public SelectObjectContentResult selectObjectContent(SelectObjectContentRequest selectRequest) throws CosClientException, CosServiceException {
// 调用 COS 接口之前必须保证本进程存在一个 COSClient 实例,如果没有则创建
// 详细代码参见本页:简单操作 -> 创建 COSClient
COSClient cosClient = createCOSClient();
// 存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式
String bucketName = "examplebucket-1250000000";
// 对象键(Key)是对象在存储桶中的唯一标识。详情请参见 [对象键](https://intl.cloud.tencent.com/document/product/436/13324)
String key = "exampleobject";
String query = "select * from COSObject s where mathScore > 85'";
SelectObjectContentRequest request = new SelectObjectContentRequest();
request.setBucketName(bucketName);
request.setKey(key);
request.setExpression(query);
request.setExpressionType(ExpressionType.SQL);
InputSerialization inputSerialization = new InputSerialization();
JSONInput jsonInput = new JSONInput();
jsonInput.setType(JSONType.LINES);
inputSerialization.setJson(jsonInput);
inputSerialization.setCompressionType(CompressionType.NONE);
request.setInputSerialization(inputSerialization);
OutputSerialization outputSerialization = new OutputSerialization();
outputSerialization.setJson(new JSONOutput());
request.setOutputSerialization(outputSerialization);
final AtomicBoolean isResultComplete = new AtomicBoolean(false);
SelectObjectContentResult result = cosclient.selectObjectContent(request);
InputStream resultInputStream = result.getPayload().getRecordsInputStream(
new SelectObjectContentEventVisitor() {
@Override
public void visit(SelectObjectContentEvent.StatsEvent event)
{
System.out.println(
"Received Stats, Bytes Scanned: " + event.getDetails().getBytesScanned()
+ " Bytes Processed: " + event.getDetails().getBytesProcessed());
}
@Override
public void visit(SelectObjectContentEvent.EndEvent event)
{
isResultComplete.set(true);
System.out.println("Received End Event. Result is complete.");
}
}
);
BufferedReader reader = new BufferedReader(new InputStreamReader(resultInputStream));
StringBuffer stringBuffer = new StringBuffer();
String line;
while((line = reader.readLine())!= null){
stringBuffer.append(line).append("\n");
}
System.out.println(stringBuffer.toString());
// 检查结果是否接受完整
if (!isResultComplete.get()) {
throw new Exception("result was incomplete");
}
// 确认本进程不再使用 cosClient 实例之后,关闭之
cosClient.shutdown();
参数名称 | 参数描述 | 类型 |
---|---|---|
selectRequest | 请求 | SelectObjectContentRequest |
SelectObjectContentRequest 成员说明:
参数名称 | 设置方法 | 描述 | 类型 |
---|---|---|---|
bucketName | set 方法 | Bucket 的命名格式为 BucketName-APPID ,详情请参见 命名规范 | String |
key | set 方法 | 指定分块上传到 COS 上的路径,即 对象键。例如对象键为 folder/picture.jpg | String |
expression | set 方法 | 请求表达式 | String |
expressionType | set 方法 | 请求表达式类型 | String |
inputSerialization | set 方法 | 描述待检索对象的格式 | InputSerialization |
outputSerialization | set 方法 | 描述检索结果的输出格式 | OutputSerialization |
本页内容是否解决了您的问题?