From 71dc8aa79aeb1910efb233896356bcfa08b95577 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Tue, 15 Aug 2017 19:28:21 +0800 Subject: [PATCH 1/8] Update test file --- src/Qcloud/Cos/Tests/ObjectTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Qcloud/Cos/Tests/ObjectTest.php b/src/Qcloud/Cos/Tests/ObjectTest.php index 2db45c0b..324829d3 100644 --- a/src/Qcloud/Cos/Tests/ObjectTest.php +++ b/src/Qcloud/Cos/Tests/ObjectTest.php @@ -71,4 +71,13 @@ public function testGetObjectUrl() { $this->assertFalse(true, $e); } } + + public function testGetObjectUrl() { + try{ + $this->cosClient->createBucket(array('Bucket' => 'testbucket')); + $this->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); + } catch (\Exception $e) { + $this->assertFalse(true, $e); + } + } } \ No newline at end of file From 8f0c1b2e12f82ff0a82c0287a2f5c0bc44521418 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Tue, 15 Aug 2017 19:55:12 +0800 Subject: [PATCH 2/8] Update test file --- src/Qcloud/Cos/Tests/ObjectTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Qcloud/Cos/Tests/ObjectTest.php b/src/Qcloud/Cos/Tests/ObjectTest.php index 324829d3..f251b179 100644 --- a/src/Qcloud/Cos/Tests/ObjectTest.php +++ b/src/Qcloud/Cos/Tests/ObjectTest.php @@ -75,7 +75,7 @@ public function testGetObjectUrl() { public function testGetObjectUrl() { try{ $this->cosClient->createBucket(array('Bucket' => 'testbucket')); - $this->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); + $this->cosClient->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); } catch (\Exception $e) { $this->assertFalse(true, $e); } From 863ec817165996dcc0b6f4f74699db80f7a9f002 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Tue, 15 Aug 2017 19:28:21 +0800 Subject: [PATCH 3/8] Update test file --- src/Qcloud/Cos/Tests/ObjectTest.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Qcloud/Cos/Tests/ObjectTest.php b/src/Qcloud/Cos/Tests/ObjectTest.php index f251b179..e627c6d1 100644 --- a/src/Qcloud/Cos/Tests/ObjectTest.php +++ b/src/Qcloud/Cos/Tests/ObjectTest.php @@ -80,4 +80,13 @@ public function testGetObjectUrl() { $this->assertFalse(true, $e); } } + + public function testGetObjectUrl() { + try{ + $this->cosClient->createBucket(array('Bucket' => 'testbucket')); + $this->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); + } catch (\Exception $e) { + $this->assertFalse(true, $e); + } + } } \ No newline at end of file From 05956a6e17f7a28594e17ca93c628f707682be43 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Tue, 15 Aug 2017 19:55:12 +0800 Subject: [PATCH 4/8] Update test file --- src/Qcloud/Cos/Tests/ObjectTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Qcloud/Cos/Tests/ObjectTest.php b/src/Qcloud/Cos/Tests/ObjectTest.php index e627c6d1..e227045e 100644 --- a/src/Qcloud/Cos/Tests/ObjectTest.php +++ b/src/Qcloud/Cos/Tests/ObjectTest.php @@ -84,7 +84,7 @@ public function testGetObjectUrl() { public function testGetObjectUrl() { try{ $this->cosClient->createBucket(array('Bucket' => 'testbucket')); - $this->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); + $this->cosClient->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); } catch (\Exception $e) { $this->assertFalse(true, $e); } From 337d86c6d688d45c1484c6b82a70ba6e7f7403ed Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Tue, 22 Aug 2017 15:24:13 +0800 Subject: [PATCH 5/8] Update test file --- src/Qcloud/Cos/Tests/ObjectTest.php | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/src/Qcloud/Cos/Tests/ObjectTest.php b/src/Qcloud/Cos/Tests/ObjectTest.php index e227045e..2db45c0b 100644 --- a/src/Qcloud/Cos/Tests/ObjectTest.php +++ b/src/Qcloud/Cos/Tests/ObjectTest.php @@ -71,22 +71,4 @@ public function testGetObjectUrl() { $this->assertFalse(true, $e); } } - - public function testGetObjectUrl() { - try{ - $this->cosClient->createBucket(array('Bucket' => 'testbucket')); - $this->cosClient->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); - } catch (\Exception $e) { - $this->assertFalse(true, $e); - } - } - - public function testGetObjectUrl() { - try{ - $this->cosClient->createBucket(array('Bucket' => 'testbucket')); - $this->cosClient->getObjectUrl('testbucket', 'hello.txt', '+10 minutes'); - } catch (\Exception $e) { - $this->assertFalse(true, $e); - } - } } \ No newline at end of file From 2e58395c3b40c02bbba02417db8752baa2cd9992 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Mon, 4 Sep 2017 16:46:45 +0800 Subject: [PATCH 6/8] Add listBucket implement --- cos-autoloader.php | 1 + src/Qcloud/Cos/BucketStyleListener.php | 2 +- src/Qcloud/Cos/Client.php | 1 + src/Qcloud/Cos/GetServiceListener.php | 45 ++++++++++++++++++++++++++ src/Qcloud/Cos/Service.php | 13 ++++++++ src/Qcloud/Cos/TokenListener.php | 2 +- 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/Qcloud/Cos/GetServiceListener.php diff --git a/cos-autoloader.php b/cos-autoloader.php index b764ba62..6d872e18 100644 --- a/cos-autoloader.php +++ b/cos-autoloader.php @@ -248,6 +248,7 @@ 'Qcloud\Cos\Exception\ServiceResponseException' => 'src/Qcloud/Cos/Exception/ServiceResponseException.php', 'Qcloud\Cos\ExceptionListener' => 'src/Qcloud/Cos/ExceptionListener.php', 'Qcloud\Cos\ExceptionParser' => 'src/Qcloud/Cos/ExceptionParser.php', + 'Qcloud\Cos\GetServiceListener' => 'src/Qcloud/Cos/GetServiceListener.php', 'Qcloud\Cos\MultipartUpload' => 'src/Qcloud/Cos/MultipartUpload.php', 'Qcloud\Cos\Service' => 'src/Qcloud/Cos/Service.php', 'Qcloud\Cos\Signature' => 'src/Qcloud/Cos/Signature.php', diff --git a/src/Qcloud/Cos/BucketStyleListener.php b/src/Qcloud/Cos/BucketStyleListener.php index 987ea11e..11460810 100644 --- a/src/Qcloud/Cos/BucketStyleListener.php +++ b/src/Qcloud/Cos/BucketStyleListener.php @@ -17,7 +17,7 @@ public function __construct($appId) { } public static function getSubscribedEvents() { - return array('command.after_prepare' => array('onCommandAfterPrepare', -255)); + return array('command.after_prepare' => array('onCommandAfterPrepare', -230)); } /** diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index 5559f2b0..2c0b02fc 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -42,6 +42,7 @@ public function __construct($config) { $this->setUserAgent('cos-php-sdk-v5/' . Client::VERSION, true); $this->addSubscriber(new ExceptionListener()); +$this->addSubscriber(new GetServiceListener()); $this->addSubscriber(new TokenListener($this->token)); $this->addSubscriber(new SignatureListener($this->secretId, $this->secretKey)); $this->addSubscriber(new BucketStyleListener($this->appId)); diff --git a/src/Qcloud/Cos/GetServiceListener.php b/src/Qcloud/Cos/GetServiceListener.php new file mode 100644 index 00000000..fecf6f4c --- /dev/null +++ b/src/Qcloud/Cos/GetServiceListener.php @@ -0,0 +1,45 @@ + array('onRequestBeforeSend', -253)); + } + + /** + * Signs requests before they are sent + * + * @param Event $event Event emitted + */ + public function onRequestBeforeSend(Event $event) { + if($event['request']->getPath() == '/' && $event['request']->getMethod() == 'GET') + {$event['request']->setUrl('http://service.cos.myqcloud.com');} +/* + if(!$this->credentials instanceof NullCredentials) { + $this->signature->signRequest($event['request'], $this->credentials); + } +*/ + } +} diff --git a/src/Qcloud/Cos/Service.php b/src/Qcloud/Cos/Service.php index fb25acd1..0cec8bb9 100644 --- a/src/Qcloud/Cos/Service.php +++ b/src/Qcloud/Cos/Service.php @@ -11,6 +11,19 @@ public static function getService() { 'description' => 'Cos V5 API Service', 'operations' => array( + 'ListBuckets' => array( + 'httpMethod' => 'GET', + 'uri' => '/', + 'class' => 'Qcloud\\Cos\\Command', + 'responseClass' => 'ListBucketsOutput', + 'responseType' => 'model', + 'parameters' => array( + 'command.expects' => array( + 'static' => true, + 'default' => 'application/xml', + ), + ), + ), 'AbortMultipartUpload' => array( 'httpMethod' => 'DELETE', 'uri' => '/{Bucket}{/Key*}', diff --git a/src/Qcloud/Cos/TokenListener.php b/src/Qcloud/Cos/TokenListener.php index d439c5b4..8bde9998 100644 --- a/src/Qcloud/Cos/TokenListener.php +++ b/src/Qcloud/Cos/TokenListener.php @@ -27,7 +27,7 @@ public function __construct($token) { public static function getSubscribedEvents() { return array( - 'request.before_send' => array('onRequestBeforeSend', -254)); + 'request.before_send' => array('onRequestBeforeSend', -240)); } /** From 4f4dbf0ad89dd2dac766fc24acd44beee52a419e Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Mon, 4 Sep 2017 16:49:05 +0800 Subject: [PATCH 7/8] Update sample --- sample.php | 25 ++++++++---- sample2.php | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 sample2.php diff --git a/sample.php b/sample.php index 13fffec0..8d4f00ce 100644 --- a/sample.php +++ b/sample.php @@ -9,10 +9,19 @@ 'appId' => '', 'secretId' => '', 'secretKey' => ''))); +#listBuckets +try { + $result = $cosClient->listBuckets(); + print_r($result); +} catch (\Exception $e) { + echo "$e\n"; +} + + #createBucket try { $result = $cosClient->createBucket(array('Bucket' => 'testbucket')); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } @@ -23,7 +32,7 @@ $bucket='testbucket', '111.txt', str_repeat('a', 5* 1024 * 1024)); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } @@ -34,7 +43,7 @@ 'Bucket' => 'testbucket', 'Key' => '111', 'Body' => 'Hello World!')); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } @@ -45,7 +54,7 @@ 'Bucket' => 'testbucket', 'Key' => '111', 'Body' => 'Hello World!')); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } @@ -55,7 +64,7 @@ $result = $cosClient->deleteObject(array( 'Bucket' => 'testbucket', 'Key' => '111')); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } @@ -65,7 +74,7 @@ try { $result = $cosClient->deleteBucket(array( 'Bucket' => 'testbucket')); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } @@ -75,7 +84,7 @@ $result = $cosClient->headObject(array( 'Bucket' => 'testbucket', 'Key' => 'hello.txt')); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } @@ -84,7 +93,7 @@ try { $result = $cosClient->listObjects(array( 'Bucket' => 'testbucket')); - var_dump($result); + print_r($result); } catch (\Exception $e) { echo "$e\n"; } diff --git a/sample2.php b/sample2.php new file mode 100644 index 00000000..13fffec0 --- /dev/null +++ b/sample2.php @@ -0,0 +1,107 @@ + 'cn-north', + 'credentials'=> array( + 'appId' => '', + 'secretId' => '', + 'secretKey' => ''))); +#createBucket +try { + $result = $cosClient->createBucket(array('Bucket' => 'testbucket')); + var_dump($result); + } catch (\Exception $e) { + echo "$e\n"; +} + +#uploadbigfile +try { + $result = $cosClient->upload( + $bucket='testbucket', + '111.txt', + str_repeat('a', 5* 1024 * 1024)); + var_dump($result); + } catch (\Exception $e) { + echo "$e\n"; +} + +#putObject +try { + $result = $cosClient->putObject(array( + 'Bucket' => 'testbucket', + 'Key' => '111', + 'Body' => 'Hello World!')); + var_dump($result); +} catch (\Exception $e) { + echo "$e\n"; +} + +#getObject +try { + $result = $cosClient->getObject(array( + 'Bucket' => 'testbucket', + 'Key' => '111', + 'Body' => 'Hello World!')); + var_dump($result); +} catch (\Exception $e) { + echo "$e\n"; +} + +#deleteObject +try { + $result = $cosClient->deleteObject(array( + 'Bucket' => 'testbucket', + 'Key' => '111')); + var_dump($result); +} catch (\Exception $e) { + echo "$e\n"; +} + + +#deleteBucket +try { + $result = $cosClient->deleteBucket(array( + 'Bucket' => 'testbucket')); + var_dump($result); +} catch (\Exception $e) { + echo "$e\n"; +} + +#headObject +try { + $result = $cosClient->headObject(array( + 'Bucket' => 'testbucket', + 'Key' => 'hello.txt')); + var_dump($result); +} catch (\Exception $e) { + echo "$e\n"; +} + +#listObjects +try { + $result = $cosClient->listObjects(array( + 'Bucket' => 'testbucket')); + var_dump($result); +} catch (\Exception $e) { + echo "$e\n"; +} + + +#getObjectUrl +try { + $bucket = 'testbucket'; + $key = 'hello.txt'; + $region = 'cn-south'; + $url = "/{$key}"; + $request = $cosClient->get($url); + $signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes'); + echo ($signedUrl); + +} catch (\Exception $e) { + echo "$e\n"; +} + + From d53e63de5dee6b334464ad2783c7adec79d809d6 Mon Sep 17 00:00:00 2001 From: lewzylu <327874225@qq.com> Date: Mon, 4 Sep 2017 16:55:43 +0800 Subject: [PATCH 8/8] Update README --- README.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README.md b/README.md index b32ad594..09968c6d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,24 @@ ## 开发准备 +### 获取bucket列表 listbuckets +#### 方法原型 +```php +public Guzzle\Service\Resource\Model listBucket(array $args = array()) +``` +#### 成功返回值 + +| 返回值类型 | 返回值描述 | +| :-------------: | :-----------------: | +| Guzzle\Service\Resource\Model | bucket列表的信息 | + +#### 示例 + +```php +//获取bucket列表 +$result = $cosClient->listBuckets(); +``` + + ### 简单文件上传 putobject #### 方法原型