From f532615f392842d5c8b9d6d1c707e4b07da61bb3 Mon Sep 17 00:00:00 2001 From: lewzylu Date: Thu, 6 Aug 2020 21:42:24 +0800 Subject: [PATCH] feature-custom-header --- src/Qcloud/Cos/Client.php | 23 ++++++++++++------- .../Cos/CommandToRequestTransformer.php | 1 + src/Qcloud/Cos/Common.php | 13 +++++++++++ src/Qcloud/Cos/Tests/Test.php | 1 - 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index 9dc25dc6..1c51e2a3 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -49,6 +49,7 @@ public function __construct($cosConfig) { $this->cosConfig['endpoint'] = isset($cosConfig['endpoint']) ? $cosConfig['endpoint'] : 'myqcloud.com'; $this->cosConfig['domain'] = isset($cosConfig['domain']) ? $cosConfig['domain'] : null; $this->cosConfig['proxy'] = isset($cosConfig['proxy']) ? $cosConfig['proxy'] : null; + $this->cosConfig['retry'] = isset($cosConfig['retry']) ? $cosConfig['retry'] : 1; $this->cosConfig['userAgent'] = isset($cosConfig['userAgent']) ? $cosConfig['userAgent'] : 'cos-php-sdk-v5.'. Client::VERSION; $this->cosConfig['pathStyle'] = isset($cosConfig['pathStyle']) ? $cosConfig['pathStyle'] : false; @@ -113,14 +114,20 @@ public function __destruct() { } public function __call($method, array $args) { - try { - return parent::__call(ucfirst($method), $args); - } catch (CommandException $e) { - $previous = $e->getPrevious(); - if ($previous !== null) { - throw $previous; - } else { - throw $e; + for ($i = 1; $i <= $this->cosConfig['retry']; $i++) { + try { + return parent::__call(ucfirst($method), $args); + } catch (CommandException $e) { + if ($i != $this->cosConfig['retry']) { + sleep(1 << ($i-1)); + continue; + } + $previous = $e->getPrevious(); + if ($previous !== null) { + throw $previous; + } else { + throw $e; + } } } } diff --git a/src/Qcloud/Cos/CommandToRequestTransformer.php b/src/Qcloud/Cos/CommandToRequestTransformer.php index e39b7a0c..7d2bb46c 100644 --- a/src/Qcloud/Cos/CommandToRequestTransformer.php +++ b/src/Qcloud/Cos/CommandToRequestTransformer.php @@ -130,6 +130,7 @@ public function metadataTransformer(CommandInterface $command, $request) { $request = $request->withHeader('x-cos-meta-' . $key, $value); } } + $request = headersMap($command, $request); return $request; } diff --git a/src/Qcloud/Cos/Common.php b/src/Qcloud/Cos/Common.php index c188002e..e962ed47 100644 --- a/src/Qcloud/Cos/Common.php +++ b/src/Qcloud/Cos/Common.php @@ -33,3 +33,16 @@ function endWith($haystack, $needle) { } return (substr($haystack, -$length) === $needle); } + +function headersMap($command, $request) { + $headermap = array( + 'TransferEncoding'=>'Transfer-Encoding', + 'ChannelId'=>'x-cos-channel-id' + ); + foreach ($headermap as $key => $value) { + if (isset($command[$key])) { + $request = $request->withHeader($value, $command[$key]); + } + } + return $request; +} diff --git a/src/Qcloud/Cos/Tests/Test.php b/src/Qcloud/Cos/Tests/Test.php index e78724a3..b0ad6d7a 100644 --- a/src/Qcloud/Cos/Tests/Test.php +++ b/src/Qcloud/Cos/Tests/Test.php @@ -68,7 +68,6 @@ public function testValidRegionBucket() $regionlist = array('cn-east','ap-shanghai', 'cn-south','ap-guangzhou', 'cn-north','ap-beijing-1', - 'cn-south-2','ap-guangzhou-2', 'cn-southwest','ap-chengdu', 'sg','ap-singapore', 'tj','ap-beijing-1',