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 #### 方法原型 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/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"; +} + + 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)); } /**