Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

#### 方法原型
Expand Down
1 change: 1 addition & 0 deletions cos-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
25 changes: 17 additions & 8 deletions sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand All @@ -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";
}
Expand All @@ -34,7 +43,7 @@
'Bucket' => 'testbucket',
'Key' => '111',
'Body' => 'Hello World!'));
var_dump($result);
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
Expand All @@ -45,7 +54,7 @@
'Bucket' => 'testbucket',
'Key' => '111',
'Body' => 'Hello World!'));
var_dump($result);
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
Expand All @@ -55,7 +64,7 @@
$result = $cosClient->deleteObject(array(
'Bucket' => 'testbucket',
'Key' => '111'));
var_dump($result);
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
Expand All @@ -65,7 +74,7 @@
try {
$result = $cosClient->deleteBucket(array(
'Bucket' => 'testbucket'));
var_dump($result);
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
Expand All @@ -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";
}
Expand All @@ -84,7 +93,7 @@
try {
$result = $cosClient->listObjects(array(
'Bucket' => 'testbucket'));
var_dump($result);
print_r($result);
} catch (\Exception $e) {
echo "$e\n";
}
Expand Down
107 changes: 107 additions & 0 deletions sample2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

require(__DIR__ . DIRECTORY_SEPARATOR . 'cos-autoloader.php');

$cosClient = new Qcloud\Cos\Client(
array(
'region' => '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";
}


2 changes: 1 addition & 1 deletion src/Qcloud/Cos/BucketStyleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Qcloud/Cos/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
45 changes: 45 additions & 0 deletions src/Qcloud/Cos/GetServiceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Qcloud\Cos;

use Aws\Common\Credentials\CredentialsInterface;
use Aws\Common\Credentials\NullCredentials;
use Guzzle\Common\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Listener used to sign requests before they are sent over the wire.
*/
class GetServiceListener implements EventSubscriberInterface {
// cos signature.

/**
* Construct a new request signing plugin
*/
public function __construct() {
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
'request.before_send' => 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);
}
*/
}
}
13 changes: 13 additions & 0 deletions src/Qcloud/Cos/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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*}',
Expand Down
2 changes: 1 addition & 1 deletion src/Qcloud/Cos/TokenListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down