Skip to content

Commit 7153833

Browse files
lewzylulewzylu
andauthored
Patch select object (#145)
* update select object * Add Demo Co-authored-by: lewzylu <lewzylu@tencent.com>
1 parent 8e91a6f commit 7153833

File tree

5 files changed

+350
-32
lines changed

5 files changed

+350
-32
lines changed

sample/selectObjectContent.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
require dirname(__FILE__) . '/../vendor/autoload.php';
4+
5+
$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
6+
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
7+
$region = "ap-beijing"; //设置一个默认的存储桶地域
8+
$cosClient = new Qcloud\Cos\Client(array(
9+
'region' => $region,
10+
'schema' => 'https', //协议头部,默认为http
11+
'credentials'=> array(
12+
'secretId' => $secretId ,
13+
'secretKey' => $secretKey
14+
)
15+
));
16+
try {
17+
$result = $cosClient->selectObjectContent(array(
18+
'Bucket' => $bucket, //格式:BucketName-APPID
19+
'Key' => $key,
20+
'Expression' => 'Select * from COSObject s',
21+
'ExpressionType' => 'SQL',
22+
'InputSerialization' => array(
23+
'CompressionType' => 'None',
24+
'CSV' => array(
25+
'FileHeaderInfo' => 'NONE',
26+
'RecordDelimiter' => '\n',
27+
'FieldDelimiter' => ',',
28+
'QuoteEscapeCharacter' => '"',
29+
'Comments' => '#',
30+
'AllowQuotedRecordDelimiter' => 'FALSE'
31+
)
32+
),
33+
'OutputSerialization' => array(
34+
'CSV' => array(
35+
'QuoteField' => 'ASNEEDED',
36+
'RecordDelimiter' => '\n',
37+
'FieldDelimiter' => ',',
38+
'QuoteCharacter' => '"',
39+
'QuoteEscapeCharacter' => '"'
40+
)
41+
),
42+
'RequestProgress' => array(
43+
'Enabled' => 'FALSE'
44+
)
45+
));
46+
// 请求成功
47+
foreach ($result['Data'] as $data) {
48+
// 迭代遍历select结果
49+
print_r($data);
50+
}
51+
} catch (\Exception $e) {
52+
// 请求失败
53+
echo($e);
54+
}

src/Qcloud/Cos/Client.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Client extends GuzzleClient {
2727

2828
private $api;
2929
private $desc;
30+
private $action;
31+
private $operation;
3032
private $cosConfig;
3133
private $signature;
3234
private $rawCosConfig;
@@ -81,9 +83,9 @@ public function __construct($cosConfig) {
8183

8284
public function commandToRequestTransformer(CommandInterface $command)
8385
{
84-
$action = $command->GetName();
85-
$opreation = $this->api[$action];
86-
$transformer = new CosTransformer($this->cosConfig, $opreation);
86+
$this->action = $command->GetName();
87+
$this->operation = $this->api[$this->action];
88+
$transformer = new CommandToRequestTransformer($this->cosConfig, $this->operation);
8789
$seri = new Serializer($this->desc);
8890
$request = $seri($command);
8991
$request = $transformer->bucketStyleTransformer($command, $request);
@@ -96,36 +98,17 @@ public function commandToRequestTransformer(CommandInterface $command)
9698

9799
public function responseToResultTransformer(ResponseInterface $response, RequestInterface $request, CommandInterface $command)
98100
{
99-
$action = $command->getName();
100-
if ($action == "GetObject") {
101-
if (isset($command['SaveAs'])) {
102-
$fp = fopen($command['SaveAs'], "wb");
103-
fwrite($fp, $response->getBody());
104-
fclose($fp);
105-
}
106-
}
101+
$transformer = new ResultTransformer($this->cosConfig, $this->operation);
102+
$transformer->writeDataToLocal($command, $request, $response);
107103
$deseri = new Deserializer($this->desc, true);
108-
$rsp = $deseri($response, $request, $command);
104+
$result = $deseri($response, $request, $command);
109105

110-
$headers = $response->getHeaders();
111-
$metadata = array();
112-
foreach ($headers as $key => $value) {
113-
if (strpos($key, "x-cos-meta-") === 0) {
114-
$metadata[substr($key, 11)] = $value[0];
115-
}
116-
}
117-
if (!empty($metadata)) {
118-
$rsp['Metadata'] = $metadata;
119-
}
120-
if ($command['Key'] != null && $rsp['Key'] == null) {
121-
$rsp['Key'] = $command['Key'];
122-
}
123-
if ($command['Bucket'] != null && $rsp['Bucket'] == null) {
124-
$rsp['Bucket'] = $command['Bucket'];
125-
}
126-
$rsp['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath();
127-
return $rsp;
106+
$result = $transformer->metaDataTransformer($command, $response, $result);
107+
$result = $transformer->extraHeadersTransformer($command, $request, $result);
108+
$result = $transformer->selectContentTransformer($command, $result);
109+
return $result;
128110
}
111+
129112
public function __destruct() {
130113
}
131114

@@ -264,7 +247,7 @@ public function doesObjectExist($bucket, $key, array $options = array())
264247
return False;
265248
}
266249
}
267-
250+
268251
public static function explodeKey($key) {
269252
// Remove a leading slash if one is found
270253
$split_key = explode('/', $key && $key[0] == '/' ? substr($key, 1) : $key);
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
namespace Qcloud\Cos;
4+
5+
use Guzzle\Service\Description\Parameter;
6+
use Guzzle\Service\Description\ServiceDescription;
7+
use GuzzleHttp\HandlerStack;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Qcloud\Cos\Signature;
11+
use GuzzleHttp\Command\Guzzle\Description;
12+
use GuzzleHttp\Command\Guzzle\GuzzleClient;
13+
use GuzzleHttp\Command\CommandInterface;
14+
use GuzzleHttp\Exception\RequestException;
15+
use GuzzleHttp\Middleware;
16+
use GuzzleHttp\Psr7;
17+
use GuzzleHttp\Psr7\Uri;
18+
use InvalidArgumentException;
19+
20+
21+
class CommandToRequestTransformer {
22+
private $config;
23+
private $operation;
24+
25+
public function __construct($config ,$operation) {
26+
$this->config = $config;
27+
$this->operation = $operation;
28+
}
29+
30+
// format bucket style
31+
public function bucketStyleTransformer(CommandInterface $command, RequestInterface $request) {
32+
$action = $command->getName();
33+
if ($action == 'ListBuckets') {
34+
return $request->withUri(new Uri($this->config['schema']."://service.cos.myqcloud.com/"));
35+
}
36+
$operation = $this->operation;
37+
$bucketname = $command['Bucket'];
38+
39+
$appId = $this->config['appId'];
40+
if ($appId != null && endWith($bucketname, '-'.$appId) == False)
41+
{
42+
$bucketname = $bucketname.'-'.$appId;
43+
}
44+
$command['Bucket'] = $bucketname;
45+
$path = '';
46+
$http_method = $operation['httpMethod'];
47+
$uri = $operation['uri'];
48+
49+
// Hoststyle is used by default
50+
// Pathstyle
51+
if ($this->config['pathStyle'] != true) {
52+
if (isset($operation['parameters']['Bucket']) && $command->hasParam('Bucket')) {
53+
$uri = str_replace("{Bucket}", '', $uri);
54+
}
55+
if (isset($operation['parameters']['Key']) && $command->hasParam('Key')) {
56+
$uri = str_replace("{/Key*}", encodeKey($command['Key']), $uri);
57+
}
58+
}
59+
$origin_host = $bucketname. '.cos.' . $this->config['region'] . '.' . $this->config['endpoint'];
60+
// domain
61+
if ($this->config['domain'] != null) {
62+
$origin_host = $this->config['domain'];
63+
}
64+
$host = $origin_host;
65+
if ($this->config['ip'] != null) {
66+
$host = $this->config['ip'];
67+
if ($this->config['port'] != null) {
68+
$host = $this->config['ip'] . ":" . $this->config['port'];
69+
}
70+
}
71+
72+
73+
$path = $this->config['schema'].'://'. $host . $uri;
74+
$uri = new Uri($path);
75+
$query = $request->getUri()->getQuery();
76+
if ($uri->getQuery() != $query && $uri->getQuery() != "") {
77+
$query = $uri->getQuery() . "&" . $request->getUri()->getQuery();
78+
}
79+
$uri = $uri->withQuery($query);
80+
$request = $request->withUri($uri);
81+
$request = $request->withHeader('Host', $origin_host);
82+
return $request;
83+
}
84+
85+
// format upload body
86+
public function uploadBodyTransformer(CommandInterface $command, $request, $bodyParameter = 'Body', $sourceParameter = 'SourceFile') {
87+
88+
$operation = $this->operation;
89+
if (!isset($operation['parameters']['Body'])) {
90+
return $request;
91+
}
92+
$source = isset($command[$sourceParameter]) ? $command[$sourceParameter] : null;
93+
$body = isset($command[$bodyParameter]) ? $command[$bodyParameter] : null;
94+
// If a file path is passed in then get the file handle
95+
if (is_string($source) && file_exists($source)) {
96+
$body = fopen($source, 'rb');
97+
}
98+
// Prepare the body parameter and remove the source file parameter
99+
if (null !== $body) {
100+
return $request;
101+
} else {
102+
throw new InvalidArgumentException(
103+
"You must specify a non-null value for the {$bodyParameter} or {$sourceParameter} parameters.");
104+
}
105+
}
106+
107+
// update md5
108+
public function md5Transformer(CommandInterface $command, $request) {
109+
$operation = $this->operation;
110+
if (isset($operation['data']['contentMd5'])) {
111+
$request = $this->addMd5($request);
112+
}
113+
if (isset($operation['parameters']['ContentMD5']) &&
114+
isset($command['ContentMD5'])) {
115+
$value = $command['ContentMD5'];
116+
if ($value === true) {
117+
$request = $this->addMd5($request);
118+
}
119+
}
120+
121+
return $request;
122+
}
123+
124+
// add meta
125+
public function metadataTransformer(CommandInterface $command, $request) {
126+
$operation = $this->operation;
127+
if (isset($command['Metadata'])) {
128+
$meta = $command['Metadata'];
129+
foreach ($meta as $key => $value) {
130+
$request = $request->withHeader('x-cos-meta-' . $key, $value);
131+
}
132+
}
133+
return $request;
134+
}
135+
136+
// count md5
137+
private function addMd5($request) {
138+
$body = $request->getBody();
139+
if ($body && $body->getSize() > 0) {
140+
$md5 = base64_encode(md5($body, true));
141+
return $request->withHeader('Content-MD5', $md5);
142+
}
143+
return $request;
144+
}
145+
146+
// inventoryId
147+
public function specialParamTransformer(CommandInterface $command, $request) {
148+
$action = $command->getName();
149+
if ($action == 'PutBucketInventory') {
150+
$id = $command['Id'];
151+
$uri = $request->getUri();
152+
$query = $uri->getQuery();
153+
$uri = $uri->withQuery($query . "&Id=".$id);
154+
return $request->withUri($uri);
155+
}
156+
return $request;
157+
}
158+
159+
public function __destruct() {
160+
}
161+
162+
}

0 commit comments

Comments
 (0)