Skip to content

Commit ad54712

Browse files
authored
Fix bug of metadata (#136)
1 parent 6a216d1 commit ad54712

File tree

4 files changed

+66
-52
lines changed

4 files changed

+66
-52
lines changed

src/Qcloud/Cos/Client.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function commandToRequestTransformer(CommandInterface $command)
8787
$request = $seri($command);
8888
$request = $transformer->bucketStyleTransformer($command, $request);
8989
$request = $transformer->uploadBodyTransformer($command, $request);
90+
$request = $transformer->metadataTransformer($command, $request);
9091
$request = $transformer->md5Transformer($command, $request);
9192
$request = $transformer->specialParamTransformer($command, $request);
9293
return $request;
@@ -103,16 +104,26 @@ public function responseToResultTransformer(ResponseInterface $response, Request
103104
}
104105
}
105106
$deseri = new Deserializer($this->desc, true);
106-
$response = $deseri($response, $request, $command);
107-
if ($command['Key'] != null && $response['Key'] == null) {
108-
$response['Key'] = $command['Key'];
107+
$rsp = $deseri($response, $request, $command);
108+
109+
$headers = $response->getHeaders();
110+
$metadata = array();
111+
foreach ($headers as $key => $value) {
112+
if (strpos($key, "x-cos-meta-") === 0) {
113+
$metadata[substr($key, 11)] = $value[0];
114+
}
109115
}
110-
if ($command['Bucket'] != null && $response['Bucket'] == null) {
111-
$response['Bucket'] = $command['Bucket'];
116+
if (!empty($metadata)) {
117+
$rsp['Metadata'] = $metadata;
112118
}
113-
$response['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath();
114-
115-
return $response;
119+
if ($command['Key'] != null && $rsp['Key'] == null) {
120+
$rsp['Key'] = $command['Key'];
121+
}
122+
if ($command['Bucket'] != null && $rsp['Bucket'] == null) {
123+
$rsp['Bucket'] = $command['Bucket'];
124+
}
125+
$rsp['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath();
126+
return $rsp;
116127
}
117128
public function __destruct() {
118129
}

src/Qcloud/Cos/CosTransformer.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ public function md5Transformer(CommandInterface $command, $request) {
118118
return $request;
119119
}
120120

121+
// add meta
122+
public function metadataTransformer(CommandInterface $command, $request) {
123+
$operation = $this->operation;
124+
if (isset($command['Metadata'])) {
125+
$meta = $command['Metadata'];
126+
foreach ($meta as $key => $value) {
127+
$request = $request->withHeader('x-cos-meta-' . $key, $value);
128+
}
129+
}
130+
return $request;
131+
}
132+
121133
// count md5
122134
private function addMd5($request) {
123135
$body = $request->getBody();
@@ -128,7 +140,7 @@ private function addMd5($request) {
128140
return $request;
129141
}
130142

131-
// count md5
143+
// inventoryId
132144
public function specialParamTransformer(CommandInterface $command, $request) {
133145
$action = $command->getName();
134146
if ($action == 'PutBucketInventory') {

src/Qcloud/Cos/Service.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,6 @@ public static function getService() {
201201
'Qcloud\\Cos\\Client::explodeKey'
202202
)
203203
),
204-
'Metadata' => array(
205-
'type' => 'object',
206-
'location' => 'header',
207-
'sentAs' => 'x-cos-meta-',
208-
'additionalProperties' => array(
209-
'type' => 'string',
210-
),
211-
),
212204
'ServerSideEncryption' => array(
213205
'type' => 'string',
214206
'location' => 'header',
@@ -381,14 +373,6 @@ public static function getService() {
381373
'filters' => array(
382374
'Qcloud\\Cos\\Client::explodeKey')
383375
),
384-
'Metadata' => array(
385-
'type' => 'object',
386-
'location' => 'header',
387-
'sentAs' => 'x-cos-meta-',
388-
'additionalProperties' => array(
389-
'type' => 'string',
390-
),
391-
),
392376
'MetadataDirective' => array(
393377
'type' => 'string',
394378
'location' => 'header',
@@ -1160,14 +1144,6 @@ public static function getService() {
11601144
'Qcloud\\Cos\\Client::explodeKey'
11611145
)
11621146
),
1163-
'Metadata' => array(
1164-
'type' => 'object',
1165-
'location' => 'header',
1166-
'sentAs' => 'x-cos-meta-',
1167-
'additionalProperties' => array(
1168-
'type' => 'string'
1169-
)
1170-
),
11711147
'ServerSideEncryption' => array(
11721148
'type' => 'string',
11731149
'location' => 'header',
@@ -3081,14 +3057,6 @@ public static function getService() {
30813057
'location' => 'header',
30823058
'sentAs' => 'x-cos-server-side-encryption',
30833059
),
3084-
'Metadata' => array(
3085-
'type' => 'object',
3086-
'location' => 'header',
3087-
'sentAs' => 'x-cos-meta-',
3088-
'additionalProperties' => array(
3089-
'type' => 'string',
3090-
),
3091-
),
30923060
'SSECustomerAlgorithm' => array(
30933061
'type' => 'string',
30943062
'location' => 'header',
@@ -4647,14 +4615,6 @@ public static function getService() {
46474615
'location' => 'header',
46484616
'sentAs' => 'x-cos-server-side-encryption',
46494617
),
4650-
'Metadata' => array(
4651-
'type' => 'object',
4652-
'location' => 'header',
4653-
'sentAs' => 'x-cos-meta-',
4654-
'additionalProperties' => array(
4655-
'type' => 'string',
4656-
),
4657-
),
46584618
'SSECustomerAlgorithm' => array(
46594619
'type' => 'string',
46604620
'location' => 'header',

src/Qcloud/Cos/Tests/Test.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,13 +1006,44 @@ public function testPutObjectExisted() {
10061006
*/
10071007
public function testPutObjectMeta() {
10081008
try {
1009+
$key = '你好.txt';
1010+
$meta = array(
1011+
'test' => str_repeat('a', 1 * 1024),
1012+
'test-meta' => 'qwe-23ds-ad-xcz.asd.*qweqw'
1013+
);
10091014
$this->cosClient->putObject(array(
10101015
'Bucket' => $this->bucket,
10111016
'Key' => '你好.txt',
10121017
'Body' => '1234124',
1013-
'Metadata' => array(
1014-
'lew' => str_repeat('a', 1 * 1024),
1015-
)));
1018+
'Metadata' => $meta
1019+
1020+
));
1021+
$rt = $this->cosClient->headObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
1022+
$this->assertEquals($rt['Metadata'], $meta);
1023+
} catch (ServiceResponseException $e) {
1024+
print $e;
1025+
$this->assertFalse(TRUE);
1026+
}
1027+
}
1028+
1029+
/*
1030+
* upload large object,请求头部携带自定义头部x-cos-meta-
1031+
* 200
1032+
*/
1033+
public function testUploadLargeObjectMeta() {
1034+
try {
1035+
$key = '你好.txt';
1036+
$meta = array(
1037+
'test' => str_repeat('a', 1 * 1024),
1038+
'test-meta' => 'qwe-23ds-ad-xcz.asd.*qweqw'
1039+
);
1040+
$body = $this->generateRandomString(2*1024*1024+1023);
1041+
$this->cosClient->upload($bucket=$this->bucket,
1042+
$key=$key,
1043+
$body=$body,
1044+
$options=['PartSize'=>1024 * 1024 + 1, 'Metadata'=>$meta]);
1045+
$rt = $this->cosClient->headObject(['Bucket'=>$this->bucket, 'Key'=>$key]);
1046+
$this->assertEquals($rt['Metadata'], $meta);
10161047
} catch (ServiceResponseException $e) {
10171048
print $e;
10181049
$this->assertFalse(TRUE);

0 commit comments

Comments
 (0)