From 12ad746e38a2ad2976f47b2698b53a954d863f50 Mon Sep 17 00:00:00 2001 From: lewzylu Date: Wed, 7 Aug 2019 17:46:44 +0800 Subject: [PATCH 1/6] update upload response --- src/Qcloud/Cos/Client.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index 966ac294..e81c1be9 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -121,6 +121,7 @@ public function Upload($bucket, $key, $body, $options = array()) { ) + $options['params']); $rt['Location'] = $rt['ObjectURL']; + $rt['Location'] = ltrim($rt['Location'], $this->schema. "://"); unset($rt['ObjectURL']); } else { @@ -131,6 +132,8 @@ public function Upload($bucket, $key, $body, $options = array()) { ) + $options['params']); $rt = $multipartUpload->performUploading(); + unset($rt['Bucket']); + unset($rt['Key']); } return $rt; } From cd1b9cefa04521eaf125a82eb53552d9a87aae4d Mon Sep 17 00:00:00 2001 From: lewzylu Date: Wed, 7 Aug 2019 18:15:47 +0800 Subject: [PATCH 2/6] update to 1.3.3 --- src/Qcloud/Cos/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index e81c1be9..53bb78db 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -12,7 +12,7 @@ use Qcloud\Cos\TokenListener; class Client extends GSClient { - const VERSION = '1.3.2'; + const VERSION = '1.3.3'; private $region; // string: region. private $credentials; From d9ddb744c5a43aa2f10592286468f7b9e7713710 Mon Sep 17 00:00:00 2001 From: lewzylu Date: Mon, 2 Sep 2019 20:01:51 +0800 Subject: [PATCH 3/6] support ip port --- src/Qcloud/Cos/BucketStyleListener.php | 3 +-- src/Qcloud/Cos/Signature.php | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Qcloud/Cos/BucketStyleListener.php b/src/Qcloud/Cos/BucketStyleListener.php index a7f29b8e..0288d32a 100644 --- a/src/Qcloud/Cos/BucketStyleListener.php +++ b/src/Qcloud/Cos/BucketStyleListener.php @@ -47,7 +47,6 @@ public static function getSubscribedEvents() { * @param Event $event Event emitted. */ public function onCommandAfterPrepare(Event $event) { - $command = $event['command']; $bucket = $command['Bucket']; $request = $command->getRequest(); @@ -81,7 +80,7 @@ public function onCommandAfterPrepare(Event $event) { $bucket = $bucket.'-'.$this->appId; } $request->getParams()->set('bucket', $bucket)->set('key', $key); - + $realHost = $bucket. '.' . $request->getHost(); if($this->ipport != null) { $request->setHost($this->ipport); diff --git a/src/Qcloud/Cos/Signature.php b/src/Qcloud/Cos/Signature.php index 2b1b46cf..28eb41eb 100644 --- a/src/Qcloud/Cos/Signature.php +++ b/src/Qcloud/Cos/Signature.php @@ -11,9 +11,10 @@ public function __construct($accessKey, $secretKey) { public function __destruct() { } public function signRequest(RequestInterface $request) { + $host = $request->getHeader('Host'); $signTime = (string)(time() - 60) . ';' . (string)(time() + 3600); $httpString = strtolower($request->getMethod()) . "\n" . urldecode($request->getPath()) . - "\n\nhost=" . $request->getHost() . "\n"; + "\n\nhost=" . $host . "\n"; $sha1edHttpString = sha1($httpString); $stringToSign = "sha1\n$signTime\n$sha1edHttpString\n"; $signKey = hash_hmac('sha1', $signTime, $this->secretKey); @@ -27,9 +28,10 @@ public function createAuthorization( RequestInterface $request, $expires = "10 minutes" ) { + $host = $request->getHeader('Host'); $signTime = (string)(time() - 60) . ';' . (string)(strtotime($expires)); $httpString = strtolower($request->getMethod()) . "\n" . urldecode($request->getPath()) . - "\n\nhost=" . $request->getHost() . "\n"; + "\n\nhost=" . $host . "\n"; $sha1edHttpString = sha1($httpString); $stringToSign = "sha1\n$signTime\n$sha1edHttpString\n"; $signKey = hash_hmac('sha1', $signTime, $this->secretKey); From 1b32aa422f6dffe4ea411e5095e4b0da9135551b Mon Sep 17 00:00:00 2001 From: lewzylu Date: Mon, 2 Sep 2019 20:08:44 +0800 Subject: [PATCH 4/6] update to 1.3.4 --- src/Qcloud/Cos/Client.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index 53bb78db..1042386f 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -12,7 +12,7 @@ use Qcloud\Cos\TokenListener; class Client extends GSClient { - const VERSION = '1.3.3'; + const VERSION = '1.3.4'; private $region; // string: region. private $credentials; From 0dba8605924c310dae3fb4a3e41ba48ec2d7ab4c Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Tue, 31 Mar 2020 14:54:49 +0800 Subject: [PATCH 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 87bc3959..b22de19e 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ require '/path/to/cos-sdk-v5.phar'; ### 源码方式 源码方式安装 SDK 的步骤如下: -1. 在 [GitHub 发布页面](https://github.com/tencentyun/cos-php-sdk-v5/releases) 下载相应的 zip 文件。 +1. 在 [GitHub 发布页面](https://github.com/tencentyun/cos-php-sdk-v5/releases) 下载相应的 tar.gz 文件。 2. 解压通过 autoload.php 脚本加载 SDK: ``` require '/path/to/sdk/vendor/autoload.php'; From 747d3203d503b41f958811bd6567c23e7e422237 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Wed, 16 Jun 2021 15:08:26 +0800 Subject: [PATCH 6/6] feature retry with domain (#193) Co-authored-by: lewzylu --- src/Qcloud/Cos/BucketStyleListener.php | 8 +++++++- src/Qcloud/Cos/Client.php | 27 ++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Qcloud/Cos/BucketStyleListener.php b/src/Qcloud/Cos/BucketStyleListener.php index 0288d32a..6adb67b8 100644 --- a/src/Qcloud/Cos/BucketStyleListener.php +++ b/src/Qcloud/Cos/BucketStyleListener.php @@ -24,12 +24,13 @@ class BucketStyleListener implements EventSubscriberInterface { private $ipport; private $endpoint; - public function __construct($appId, $ip=null, $port=null, $endpoint=null) { + public function __construct($appId, $ip=null, $port=null, $endpoint=null, $domain=null) { $this->appId = $appId; $this->ip = $ip; $this->port = $port; $this->ipport = null; $this->endpoint = $endpoint; + $this->domain = $domain; if ($ip != null) { $this->ipport = $ip; if ($port != null) { @@ -93,6 +94,11 @@ public function onCommandAfterPrepare(Event $event) { $request->setHost($realHost); } } + if ($this->domain != null ) { + $realHost = $this->domain; + $request->setHost($realHost); + $request->setHeader('Host', $realHost); + } if (!$bucket) { $request->getParams()->set('cos.resource', '/'); } else { diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index 1042386f..f9512831 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -2,6 +2,7 @@ namespace Qcloud\Cos; +use Exception; use Guzzle\Service\Description\Parameter; use Guzzle\Service\Description\ServiceDescription; use Guzzle\Service\Client as GSClient; @@ -12,7 +13,7 @@ use Qcloud\Cos\TokenListener; class Client extends GSClient { - const VERSION = '1.3.4'; + const VERSION = '1.3.5'; private $region; // string: region. private $credentials; @@ -45,6 +46,7 @@ public function __construct($config) { $this->ip = isset($config['ip']) ? $config['ip'] : null; $this->port = isset($config['port']) ? $config['port'] : null; $this->endpoint = isset($config['endpoint']) ? $config['endpoint'] : null; + $this->domain = isset($config['domain']) ? $config['domain'] : null; $this->region = isset($regionmap[$this->region]) ? $regionmap[$this->region] : $this->region; $this->credentials = $config['credentials']; $this->appId = isset($config['credentials']['appId']) ? $config['credentials']['appId'] : null; @@ -65,7 +67,7 @@ public function __construct($config) { $this->addSubscriber(new Md5Listener($this->signature)); $this->addSubscriber(new TokenListener($this->token)); $this->addSubscriber(new SignatureListener($this->secretId, $this->secretKey)); - $this->addSubscriber(new BucketStyleListener($this->appId, $this->ip, $this->port, $this->endpoint)); + $this->addSubscriber(new BucketStyleListener($this->appId, $this->ip, $this->port, $this->endpoint, $this->domain)); $this->addSubscriber(new UploadBodyListener(array('PutObject', 'UploadPart'))); } @@ -107,6 +109,27 @@ public function getObjectUrl($bucket, $key, $expires = null, array $args = array return $expires ? $this->createPresignedUrl($request, $expires) : $request->getUrl(); } + + public function upload_with_retry($bucket, $key, $body, $options = array()) { + try { + $rt = $this->upload($bucket, $key, $body, $options); + return $rt; + } catch (\Exception $e) { + if (isset($options["RetryDomain"])) { + echo($e); + $this->domain = $options["RetryDomain"]; + $this->addSubscriber(new BucketStyleListener($this->appId, $this->ip, $this->port, $this->endpoint, $this->domain)); + try { + $rt = $this->upload($bucket, $key, $body, $options); + } catch (\Exception $e2) { + throw $e2; + } + } else { + throw $e; + } + } + } + public function Upload($bucket, $key, $body, $options = array()) { $body = EntityBody::factory($body); $options = Collection::fromConfig(array_change_key_case($options), array(