Skip to content

Commit

Permalink
update to 2.1.3 (#179)
Browse files Browse the repository at this point in the history
* update

* update sample

* update to 2.1.3

Co-authored-by: lewzylu <[email protected]>
  • Loading branch information
lewzylu and lewzylu authored Mar 27, 2021
1 parent 0152bc3 commit ecd70eb
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
cos-php-sdk-v5 Upgrade Guide
====================
2.1.2 to 2.1.3
----------
- Add `download` interface, which is used for concurrent block download.
- Add callback of `upload` and `download` progress
- Fix request retry

2.1.1 to 2.1.2
----------
- The interface supports custom parameters
Expand Down
29 changes: 29 additions & 0 deletions sample/cosClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
$token = "COS_TMPTOKEN"; //"云 API 临时密钥 Token"
$region = "ap-beijing"; //设置一个默认的存储桶地域
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region, //园区
'schema' => 'https', //协议头部,默认为http
'timeout' => 10, //超时时间
'connect_timeout' => 10, //连接超时时间
'ip' => '', //ip
'port' => '', //端口
'endpoint' => '', //endpoint
'domain' => '', //自定义域名
'proxy' => '', //代理服务器
'retry' => 10, //重试次数
'userAgent' => '', //UA
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey,
'token' => $token,
'anonymous' => true, //匿名模式
)
)
);
37 changes: 37 additions & 0 deletions sample/download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

require dirname(__FILE__) . '/../vendor/autoload.php';

$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
$region = "ap-beijing"; //设置一个默认的存储桶地域
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/data/exampleobject";

$printbar = function($totolSize, $downloadedSize) {
printf("downloaded [%d/%d]\n", $downloadedSize, $totolSize);
};

try {
$result = $cosClient->download(
$bucket = 'examplebucket-125000000', //格式:BucketName-APPID
$key = 'exampleobject',
$saveAs = local_path,
$options=['Progress'=>$printbar, //指定进度条
'PartSize' => 10 * 1024 * 1024, //分块大小
'Concurrency' => 5 //并发数
]
);
// 请求成功
print_r($result);
} catch (\Exception $e) {
// 请求失败
echo($e);
}

10 changes: 9 additions & 1 deletion sample/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
'secretId' => $secretId ,
'secretKey' => $secretKey)));
$local_path = "/data/exampleobject";

$printbar = function($totolSize, $uploadedSize) {
printf("uploaded [%d/%d]\n", $uploadedSize, $totolSize);
};

try {
$result = $cosClient->upload(
$bucket = 'examplebucket-125000000', //格式:BucketName-APPID
Expand All @@ -36,7 +41,10 @@
),
'ContentMD5' => 'string',
'ServerSideEncryption' => 'string',
'StorageClass' => 'string'
'StorageClass' => 'string', //存储类型
'Progress'=>$printbar, //指定进度条
'PartSize' => 10 * 1024 * 1024, //分块大小
'Concurrency' => 5 //并发数
)
*/
);
Expand Down
61 changes: 45 additions & 16 deletions src/Qcloud/Cos/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Command\Guzzle\Deserializer;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Command\CommandInterface;
use GuzzleHttp\Command\Exception\CommandException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7;

Expand Down Expand Up @@ -75,7 +77,7 @@
* @method object SelectObjectContent (array $arg)
*/
class Client extends GuzzleClient {
const VERSION = '2.1.2';
const VERSION = '2.1.3';

public $httpClient;

Expand Down Expand Up @@ -110,6 +112,7 @@ public function __construct($cosConfig) {

$service = Service::getService();
$handler = HandlerStack::create();
$handler->push(Middleware::retry($this->retryDecide(), $this->retryDelay()));
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
return $request->withHeader('User-Agent', $this->cosConfig['userAgent']);
}));
Expand All @@ -135,6 +138,38 @@ public function __construct($cosConfig) {
'commandToRequestTransformer'], [$this, 'responseToResultTransformer'],
null);
}
public function retryDecide() {
return function (
$retries,
RequestInterface $request,
ResponseInterface $response = null,
\Exception $exception = null
) {
if ($retries >= $this->cosConfig['retry']) {
return false;
}
if ($response != null && $response->getStatusCode() >= 400 ) {
return true;
}
if ($exception instanceof \Qcloud\Cos\Exception\ServiceResponseException) {
if ($exception->getStatusCode() >= 400) {
return true;
}
}

if ($exception instanceof ConnectException) {
return true;
}

return false;
};
}

public function retryDelay() {
return function ($numberOfRetries) {
return 1000 * $numberOfRetries;
};
}
public function commandToRequestTransformer(CommandInterface $command)
{
$this->action = $command->GetName();
Expand Down Expand Up @@ -168,21 +203,15 @@ public function __destruct() {
}

public function __call($method, array $args) {
for ($i = 0; $i <= $this->cosConfig['retry']; $i++) {
try {
$rt = parent::__call(ucfirst($method), $args);
return $rt;
} catch (\Exception $e) {
if ($i != $this->cosConfig['retry']) {
sleep(1 << ($i));
continue;
}
$previous = $e->getPrevious();
if ($previous !== null) {
throw $previous;
} else {
throw $e;
}
try {
$rt = parent::__call(ucfirst($method), $args);
return $rt;
} catch (\Exception $e) {
$previous = $e->getPrevious();
if ($previous !== null) {
throw $previous;
} else {
throw $e;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Qcloud/Cos/MultipartUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function uploadParts($uploadId) {

'rejected' => function ($reason, $index) {
printf("part [%d] upload failed, reason: %s\n", $index, $reason);
throw($reason);
}
]);
$promise = $pool->promise();
Expand Down

0 comments on commit ecd70eb

Please sign in to comment.