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'; diff --git a/src/Qcloud/Cos/BucketStyleListener.php b/src/Qcloud/Cos/BucketStyleListener.php index a7f29b8e..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) { @@ -47,7 +48,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 +81,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); @@ -94,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 966ac294..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.2'; + 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( @@ -121,6 +144,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 +155,8 @@ public function Upload($bucket, $key, $body, $options = array()) { ) + $options['params']); $rt = $multipartUpload->performUploading(); + unset($rt['Bucket']); + unset($rt['Key']); } return $rt; } 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);