From a2dfbd5e0c3d82ad4dcb9b909dbe172248512326 Mon Sep 17 00:00:00 2001 From: lewzylu Date: Tue, 24 Dec 2019 17:39:06 +0800 Subject: [PATCH] Fix bug of metadata --- src/Qcloud/Cos/Client.php | 27 ++++++++++++++------- src/Qcloud/Cos/CosTransformer.php | 14 ++++++++++- src/Qcloud/Cos/Service.php | 40 ------------------------------- src/Qcloud/Cos/Tests/Test.php | 37 +++++++++++++++++++++++++--- 4 files changed, 66 insertions(+), 52 deletions(-) diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index 4ba76160..9804294c 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -87,6 +87,7 @@ public function commandToRequestTransformer(CommandInterface $command) $request = $seri($command); $request = $transformer->bucketStyleTransformer($command, $request); $request = $transformer->uploadBodyTransformer($command, $request); + $request = $transformer->metadataTransformer($command, $request); $request = $transformer->md5Transformer($command, $request); $request = $transformer->specialParamTransformer($command, $request); return $request; @@ -103,16 +104,26 @@ public function responseToResultTransformer(ResponseInterface $response, Request } } $deseri = new Deserializer($this->desc, true); - $response = $deseri($response, $request, $command); - if ($command['Key'] != null && $response['Key'] == null) { - $response['Key'] = $command['Key']; + $rsp = $deseri($response, $request, $command); + + $headers = $response->getHeaders(); + $metadata = array(); + foreach ($headers as $key => $value) { + if (strpos($key, "x-cos-meta-") === 0) { + $metadata[substr($key, 11)] = $value[0]; + } } - if ($command['Bucket'] != null && $response['Bucket'] == null) { - $response['Bucket'] = $command['Bucket']; + if (!empty($metadata)) { + $rsp['Metadata'] = $metadata; } - $response['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath(); - - return $response; + if ($command['Key'] != null && $rsp['Key'] == null) { + $rsp['Key'] = $command['Key']; + } + if ($command['Bucket'] != null && $rsp['Bucket'] == null) { + $rsp['Bucket'] = $command['Bucket']; + } + $rsp['Location'] = $request->getHeader("Host")[0] . $request->getUri()->getPath(); + return $rsp; } public function __destruct() { } diff --git a/src/Qcloud/Cos/CosTransformer.php b/src/Qcloud/Cos/CosTransformer.php index a2ecba10..70cb47aa 100644 --- a/src/Qcloud/Cos/CosTransformer.php +++ b/src/Qcloud/Cos/CosTransformer.php @@ -118,6 +118,18 @@ public function md5Transformer(CommandInterface $command, $request) { return $request; } + // add meta + public function metadataTransformer(CommandInterface $command, $request) { + $operation = $this->operation; + if (isset($command['Metadata'])) { + $meta = $command['Metadata']; + foreach ($meta as $key => $value) { + $request = $request->withHeader('x-cos-meta-' . $key, $value); + } + } + return $request; + } + // count md5 private function addMd5($request) { $body = $request->getBody(); @@ -128,7 +140,7 @@ private function addMd5($request) { return $request; } - // count md5 + // inventoryId public function specialParamTransformer(CommandInterface $command, $request) { $action = $command->getName(); if ($action == 'PutBucketInventory') { diff --git a/src/Qcloud/Cos/Service.php b/src/Qcloud/Cos/Service.php index 28ed794f..e1e7e17d 100644 --- a/src/Qcloud/Cos/Service.php +++ b/src/Qcloud/Cos/Service.php @@ -201,14 +201,6 @@ public static function getService() { 'Qcloud\\Cos\\Client::explodeKey' ) ), - 'Metadata' => array( - 'type' => 'object', - 'location' => 'header', - 'sentAs' => 'x-cos-meta-', - 'additionalProperties' => array( - 'type' => 'string', - ), - ), 'ServerSideEncryption' => array( 'type' => 'string', 'location' => 'header', @@ -381,14 +373,6 @@ public static function getService() { 'filters' => array( 'Qcloud\\Cos\\Client::explodeKey') ), - 'Metadata' => array( - 'type' => 'object', - 'location' => 'header', - 'sentAs' => 'x-cos-meta-', - 'additionalProperties' => array( - 'type' => 'string', - ), - ), 'MetadataDirective' => array( 'type' => 'string', 'location' => 'header', @@ -1160,14 +1144,6 @@ public static function getService() { 'Qcloud\\Cos\\Client::explodeKey' ) ), - 'Metadata' => array( - 'type' => 'object', - 'location' => 'header', - 'sentAs' => 'x-cos-meta-', - 'additionalProperties' => array( - 'type' => 'string' - ) - ), 'ServerSideEncryption' => array( 'type' => 'string', 'location' => 'header', @@ -3081,14 +3057,6 @@ public static function getService() { 'location' => 'header', 'sentAs' => 'x-cos-server-side-encryption', ), - 'Metadata' => array( - 'type' => 'object', - 'location' => 'header', - 'sentAs' => 'x-cos-meta-', - 'additionalProperties' => array( - 'type' => 'string', - ), - ), 'SSECustomerAlgorithm' => array( 'type' => 'string', 'location' => 'header', @@ -4647,14 +4615,6 @@ public static function getService() { 'location' => 'header', 'sentAs' => 'x-cos-server-side-encryption', ), - 'Metadata' => array( - 'type' => 'object', - 'location' => 'header', - 'sentAs' => 'x-cos-meta-', - 'additionalProperties' => array( - 'type' => 'string', - ), - ), 'SSECustomerAlgorithm' => array( 'type' => 'string', 'location' => 'header', diff --git a/src/Qcloud/Cos/Tests/Test.php b/src/Qcloud/Cos/Tests/Test.php index 19c9f645..544b83fb 100644 --- a/src/Qcloud/Cos/Tests/Test.php +++ b/src/Qcloud/Cos/Tests/Test.php @@ -1006,13 +1006,44 @@ public function testPutObjectExisted() { */ public function testPutObjectMeta() { try { + $key = '你好.txt'; + $meta = array( + 'test' => str_repeat('a', 1 * 1024), + 'test-meta' => 'qwe-23ds-ad-xcz.asd.*qweqw' + ); $this->cosClient->putObject(array( 'Bucket' => $this->bucket, 'Key' => '你好.txt', 'Body' => '1234124', - 'Metadata' => array( - 'lew' => str_repeat('a', 1 * 1024), - ))); + 'Metadata' => $meta + + )); + $rt = $this->cosClient->headObject(['Bucket'=>$this->bucket, 'Key'=>$key]); + $this->assertEquals($rt['Metadata'], $meta); + } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); + } + } + + /* + * upload large object,请求头部携带自定义头部x-cos-meta- + * 200 + */ + public function testUploadLargeObjectMeta() { + try { + $key = '你好.txt'; + $meta = array( + 'test' => str_repeat('a', 1 * 1024), + 'test-meta' => 'qwe-23ds-ad-xcz.asd.*qweqw' + ); + $body = $this->generateRandomString(2*1024*1024+1023); + $this->cosClient->upload($bucket=$this->bucket, + $key=$key, + $body=$body, + $options=['PartSize'=>1024 * 1024 + 1, 'Metadata'=>$meta]); + $rt = $this->cosClient->headObject(['Bucket'=>$this->bucket, 'Key'=>$key]); + $this->assertEquals($rt['Metadata'], $meta); } catch (ServiceResponseException $e) { print $e; $this->assertFalse(TRUE);