Skip to content

Commit 2dac923

Browse files
authored
Fix listbuckets (#7)
* Add listBucket implement * Update sample * Update README
1 parent 1deba4e commit 2dac923

File tree

9 files changed

+205
-10
lines changed

9 files changed

+205
-10
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
## 开发准备
22

3+
### 获取bucket列表 listbuckets
4+
#### 方法原型
5+
```php
6+
public Guzzle\Service\Resource\Model listBucket(array $args = array())
7+
```
8+
#### 成功返回值
9+
10+
| 返回值类型 | 返回值描述 |
11+
| :-------------: | :-----------------: |
12+
| Guzzle\Service\Resource\Model | bucket列表的信息 |
13+
14+
#### 示例
15+
16+
```php
17+
//获取bucket列表
18+
$result = $cosClient->listBuckets();
19+
```
20+
21+
322
### 简单文件上传 putobject
423

524
#### 方法原型

cos-autoloader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@
248248
'Qcloud\Cos\Exception\ServiceResponseException' => 'src/Qcloud/Cos/Exception/ServiceResponseException.php',
249249
'Qcloud\Cos\ExceptionListener' => 'src/Qcloud/Cos/ExceptionListener.php',
250250
'Qcloud\Cos\ExceptionParser' => 'src/Qcloud/Cos/ExceptionParser.php',
251+
'Qcloud\Cos\GetServiceListener' => 'src/Qcloud/Cos/GetServiceListener.php',
251252
'Qcloud\Cos\MultipartUpload' => 'src/Qcloud/Cos/MultipartUpload.php',
252253
'Qcloud\Cos\Service' => 'src/Qcloud/Cos/Service.php',
253254
'Qcloud\Cos\Signature' => 'src/Qcloud/Cos/Signature.php',

sample.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99
'appId' => '',
1010
'secretId' => '',
1111
'secretKey' => '')));
12+
#listBuckets
13+
try {
14+
$result = $cosClient->listBuckets();
15+
print_r($result);
16+
} catch (\Exception $e) {
17+
echo "$e\n";
18+
}
19+
20+
1221
#createBucket
1322
try {
1423
$result = $cosClient->createBucket(array('Bucket' => 'testbucket'));
15-
var_dump($result);
24+
print_r($result);
1625
} catch (\Exception $e) {
1726
echo "$e\n";
1827
}
@@ -23,7 +32,7 @@
2332
$bucket='testbucket',
2433
'111.txt',
2534
str_repeat('a', 5* 1024 * 1024));
26-
var_dump($result);
35+
print_r($result);
2736
} catch (\Exception $e) {
2837
echo "$e\n";
2938
}
@@ -34,7 +43,7 @@
3443
'Bucket' => 'testbucket',
3544
'Key' => '111',
3645
'Body' => 'Hello World!'));
37-
var_dump($result);
46+
print_r($result);
3847
} catch (\Exception $e) {
3948
echo "$e\n";
4049
}
@@ -45,7 +54,7 @@
4554
'Bucket' => 'testbucket',
4655
'Key' => '111',
4756
'Body' => 'Hello World!'));
48-
var_dump($result);
57+
print_r($result);
4958
} catch (\Exception $e) {
5059
echo "$e\n";
5160
}
@@ -55,7 +64,7 @@
5564
$result = $cosClient->deleteObject(array(
5665
'Bucket' => 'testbucket',
5766
'Key' => '111'));
58-
var_dump($result);
67+
print_r($result);
5968
} catch (\Exception $e) {
6069
echo "$e\n";
6170
}
@@ -65,7 +74,7 @@
6574
try {
6675
$result = $cosClient->deleteBucket(array(
6776
'Bucket' => 'testbucket'));
68-
var_dump($result);
77+
print_r($result);
6978
} catch (\Exception $e) {
7079
echo "$e\n";
7180
}
@@ -75,7 +84,7 @@
7584
$result = $cosClient->headObject(array(
7685
'Bucket' => 'testbucket',
7786
'Key' => 'hello.txt'));
78-
var_dump($result);
87+
print_r($result);
7988
} catch (\Exception $e) {
8089
echo "$e\n";
8190
}
@@ -84,7 +93,7 @@
8493
try {
8594
$result = $cosClient->listObjects(array(
8695
'Bucket' => 'testbucket'));
87-
var_dump($result);
96+
print_r($result);
8897
} catch (\Exception $e) {
8998
echo "$e\n";
9099
}

sample2.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
require(__DIR__ . DIRECTORY_SEPARATOR . 'cos-autoloader.php');
4+
5+
$cosClient = new Qcloud\Cos\Client(
6+
array(
7+
'region' => 'cn-north',
8+
'credentials'=> array(
9+
'appId' => '',
10+
'secretId' => '',
11+
'secretKey' => '')));
12+
#createBucket
13+
try {
14+
$result = $cosClient->createBucket(array('Bucket' => 'testbucket'));
15+
var_dump($result);
16+
} catch (\Exception $e) {
17+
echo "$e\n";
18+
}
19+
20+
#uploadbigfile
21+
try {
22+
$result = $cosClient->upload(
23+
$bucket='testbucket',
24+
'111.txt',
25+
str_repeat('a', 5* 1024 * 1024));
26+
var_dump($result);
27+
} catch (\Exception $e) {
28+
echo "$e\n";
29+
}
30+
31+
#putObject
32+
try {
33+
$result = $cosClient->putObject(array(
34+
'Bucket' => 'testbucket',
35+
'Key' => '111',
36+
'Body' => 'Hello World!'));
37+
var_dump($result);
38+
} catch (\Exception $e) {
39+
echo "$e\n";
40+
}
41+
42+
#getObject
43+
try {
44+
$result = $cosClient->getObject(array(
45+
'Bucket' => 'testbucket',
46+
'Key' => '111',
47+
'Body' => 'Hello World!'));
48+
var_dump($result);
49+
} catch (\Exception $e) {
50+
echo "$e\n";
51+
}
52+
53+
#deleteObject
54+
try {
55+
$result = $cosClient->deleteObject(array(
56+
'Bucket' => 'testbucket',
57+
'Key' => '111'));
58+
var_dump($result);
59+
} catch (\Exception $e) {
60+
echo "$e\n";
61+
}
62+
63+
64+
#deleteBucket
65+
try {
66+
$result = $cosClient->deleteBucket(array(
67+
'Bucket' => 'testbucket'));
68+
var_dump($result);
69+
} catch (\Exception $e) {
70+
echo "$e\n";
71+
}
72+
73+
#headObject
74+
try {
75+
$result = $cosClient->headObject(array(
76+
'Bucket' => 'testbucket',
77+
'Key' => 'hello.txt'));
78+
var_dump($result);
79+
} catch (\Exception $e) {
80+
echo "$e\n";
81+
}
82+
83+
#listObjects
84+
try {
85+
$result = $cosClient->listObjects(array(
86+
'Bucket' => 'testbucket'));
87+
var_dump($result);
88+
} catch (\Exception $e) {
89+
echo "$e\n";
90+
}
91+
92+
93+
#getObjectUrl
94+
try {
95+
$bucket = 'testbucket';
96+
$key = 'hello.txt';
97+
$region = 'cn-south';
98+
$url = "/{$key}";
99+
$request = $cosClient->get($url);
100+
$signedUrl = $cosClient->getObjectUrl($bucket, $key, '+10 minutes');
101+
echo ($signedUrl);
102+
103+
} catch (\Exception $e) {
104+
echo "$e\n";
105+
}
106+
107+

src/Qcloud/Cos/BucketStyleListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct($appId) {
1717
}
1818

1919
public static function getSubscribedEvents() {
20-
return array('command.after_prepare' => array('onCommandAfterPrepare', -255));
20+
return array('command.after_prepare' => array('onCommandAfterPrepare', -230));
2121
}
2222

2323
/**

src/Qcloud/Cos/Client.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function __construct($config) {
4242
$this->setUserAgent('cos-php-sdk-v5/' . Client::VERSION, true);
4343

4444
$this->addSubscriber(new ExceptionListener());
45+
$this->addSubscriber(new GetServiceListener());
4546
$this->addSubscriber(new TokenListener($this->token));
4647
$this->addSubscriber(new SignatureListener($this->secretId, $this->secretKey));
4748
$this->addSubscriber(new BucketStyleListener($this->appId));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Qcloud\Cos;
4+
5+
use Aws\Common\Credentials\CredentialsInterface;
6+
use Aws\Common\Credentials\NullCredentials;
7+
use Guzzle\Common\Event;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
/**
11+
* Listener used to sign requests before they are sent over the wire.
12+
*/
13+
class GetServiceListener implements EventSubscriberInterface {
14+
// cos signature.
15+
16+
/**
17+
* Construct a new request signing plugin
18+
*/
19+
public function __construct() {
20+
}
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public static function getSubscribedEvents()
26+
{
27+
return array(
28+
'request.before_send' => array('onRequestBeforeSend', -253));
29+
}
30+
31+
/**
32+
* Signs requests before they are sent
33+
*
34+
* @param Event $event Event emitted
35+
*/
36+
public function onRequestBeforeSend(Event $event) {
37+
if($event['request']->getPath() == '/' && $event['request']->getMethod() == 'GET')
38+
{$event['request']->setUrl('http://service.cos.myqcloud.com');}
39+
/*
40+
if(!$this->credentials instanceof NullCredentials) {
41+
$this->signature->signRequest($event['request'], $this->credentials);
42+
}
43+
*/
44+
}
45+
}

src/Qcloud/Cos/Service.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ public static function getService() {
1111
'description' => 'Cos V5 API Service',
1212

1313
'operations' => array(
14+
'ListBuckets' => array(
15+
'httpMethod' => 'GET',
16+
'uri' => '/',
17+
'class' => 'Qcloud\\Cos\\Command',
18+
'responseClass' => 'ListBucketsOutput',
19+
'responseType' => 'model',
20+
'parameters' => array(
21+
'command.expects' => array(
22+
'static' => true,
23+
'default' => 'application/xml',
24+
),
25+
),
26+
),
1427
'AbortMultipartUpload' => array(
1528
'httpMethod' => 'DELETE',
1629
'uri' => '/{Bucket}{/Key*}',

src/Qcloud/Cos/TokenListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct($token) {
2727
public static function getSubscribedEvents()
2828
{
2929
return array(
30-
'request.before_send' => array('onRequestBeforeSend', -254));
30+
'request.before_send' => array('onRequestBeforeSend', -240));
3131
}
3232

3333
/**

0 commit comments

Comments
 (0)