Skip to content

Commit ecd70eb

Browse files
lewzylulewzylu
andauthored
update to 2.1.3 (#179)
* update * update sample * update to 2.1.3 Co-authored-by: lewzylu <lewzylu@tencent.com>
1 parent 0152bc3 commit ecd70eb

File tree

6 files changed

+127
-17
lines changed

6 files changed

+127
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
cos-php-sdk-v5 Upgrade Guide
22
====================
3+
2.1.2 to 2.1.3
4+
----------
5+
- Add `download` interface, which is used for concurrent block download.
6+
- Add callback of `upload` and `download` progress
7+
- Fix request retry
8+
39
2.1.1 to 2.1.2
410
----------
511
- The interface supports custom parameters

sample/cosClient.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
require dirname(__FILE__) . '/../vendor/autoload.php';
4+
5+
$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
6+
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
7+
$token = "COS_TMPTOKEN"; //"云 API 临时密钥 Token"
8+
$region = "ap-beijing"; //设置一个默认的存储桶地域
9+
$cosClient = new Qcloud\Cos\Client(
10+
array(
11+
'region' => $region, //园区
12+
'schema' => 'https', //协议头部,默认为http
13+
'timeout' => 10, //超时时间
14+
'connect_timeout' => 10, //连接超时时间
15+
'ip' => '', //ip
16+
'port' => '', //端口
17+
'endpoint' => '', //endpoint
18+
'domain' => '', //自定义域名
19+
'proxy' => '', //代理服务器
20+
'retry' => 10, //重试次数
21+
'userAgent' => '', //UA
22+
'credentials'=> array(
23+
'secretId' => $secretId ,
24+
'secretKey' => $secretKey,
25+
'token' => $token,
26+
'anonymous' => true, //匿名模式
27+
)
28+
)
29+
);

sample/download.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
require dirname(__FILE__) . '/../vendor/autoload.php';
4+
5+
$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
6+
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
7+
$region = "ap-beijing"; //设置一个默认的存储桶地域
8+
$cosClient = new Qcloud\Cos\Client(
9+
array(
10+
'region' => $region,
11+
'schema' => 'https', //协议头部,默认为http
12+
'credentials'=> array(
13+
'secretId' => $secretId ,
14+
'secretKey' => $secretKey)));
15+
$local_path = "/data/exampleobject";
16+
17+
$printbar = function($totolSize, $downloadedSize) {
18+
printf("downloaded [%d/%d]\n", $downloadedSize, $totolSize);
19+
};
20+
21+
try {
22+
$result = $cosClient->download(
23+
$bucket = 'examplebucket-125000000', //格式:BucketName-APPID
24+
$key = 'exampleobject',
25+
$saveAs = local_path,
26+
$options=['Progress'=>$printbar, //指定进度条
27+
'PartSize' => 10 * 1024 * 1024, //分块大小
28+
'Concurrency' => 5 //并发数
29+
]
30+
);
31+
// 请求成功
32+
print_r($result);
33+
} catch (\Exception $e) {
34+
// 请求失败
35+
echo($e);
36+
}
37+

sample/upload.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
'secretId' => $secretId ,
1414
'secretKey' => $secretKey)));
1515
$local_path = "/data/exampleobject";
16+
17+
$printbar = function($totolSize, $uploadedSize) {
18+
printf("uploaded [%d/%d]\n", $uploadedSize, $totolSize);
19+
};
20+
1621
try {
1722
$result = $cosClient->upload(
1823
$bucket = 'examplebucket-125000000', //格式:BucketName-APPID
@@ -36,7 +41,10 @@
3641
),
3742
'ContentMD5' => 'string',
3843
'ServerSideEncryption' => 'string',
39-
'StorageClass' => 'string'
44+
'StorageClass' => 'string', //存储类型
45+
'Progress'=>$printbar, //指定进度条
46+
'PartSize' => 10 * 1024 * 1024, //分块大小
47+
'Concurrency' => 5 //并发数
4048
)
4149
*/
4250
);

src/Qcloud/Cos/Client.php

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use GuzzleHttp\Command\Guzzle\Description;
1313
use GuzzleHttp\Command\Guzzle\GuzzleClient;
1414
use GuzzleHttp\Command\Guzzle\Deserializer;
15+
use GuzzleHttp\Exception\RequestException;
1516
use GuzzleHttp\Command\CommandInterface;
1617
use GuzzleHttp\Command\Exception\CommandException;
18+
use GuzzleHttp\Exception\ConnectException;
1719
use GuzzleHttp\Middleware;
1820
use GuzzleHttp\Psr7;
1921

@@ -75,7 +77,7 @@
7577
* @method object SelectObjectContent (array $arg)
7678
*/
7779
class Client extends GuzzleClient {
78-
const VERSION = '2.1.2';
80+
const VERSION = '2.1.3';
7981

8082
public $httpClient;
8183

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

111113
$service = Service::getService();
112114
$handler = HandlerStack::create();
115+
$handler->push(Middleware::retry($this->retryDecide(), $this->retryDelay()));
113116
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
114117
return $request->withHeader('User-Agent', $this->cosConfig['userAgent']);
115118
}));
@@ -135,6 +138,38 @@ public function __construct($cosConfig) {
135138
'commandToRequestTransformer'], [$this, 'responseToResultTransformer'],
136139
null);
137140
}
141+
public function retryDecide() {
142+
return function (
143+
$retries,
144+
RequestInterface $request,
145+
ResponseInterface $response = null,
146+
\Exception $exception = null
147+
) {
148+
if ($retries >= $this->cosConfig['retry']) {
149+
return false;
150+
}
151+
if ($response != null && $response->getStatusCode() >= 400 ) {
152+
return true;
153+
}
154+
if ($exception instanceof \Qcloud\Cos\Exception\ServiceResponseException) {
155+
if ($exception->getStatusCode() >= 400) {
156+
return true;
157+
}
158+
}
159+
160+
if ($exception instanceof ConnectException) {
161+
return true;
162+
}
163+
164+
return false;
165+
};
166+
}
167+
168+
public function retryDelay() {
169+
return function ($numberOfRetries) {
170+
return 1000 * $numberOfRetries;
171+
};
172+
}
138173
public function commandToRequestTransformer(CommandInterface $command)
139174
{
140175
$this->action = $command->GetName();
@@ -168,21 +203,15 @@ public function __destruct() {
168203
}
169204

170205
public function __call($method, array $args) {
171-
for ($i = 0; $i <= $this->cosConfig['retry']; $i++) {
172-
try {
173-
$rt = parent::__call(ucfirst($method), $args);
174-
return $rt;
175-
} catch (\Exception $e) {
176-
if ($i != $this->cosConfig['retry']) {
177-
sleep(1 << ($i));
178-
continue;
179-
}
180-
$previous = $e->getPrevious();
181-
if ($previous !== null) {
182-
throw $previous;
183-
} else {
184-
throw $e;
185-
}
206+
try {
207+
$rt = parent::__call(ucfirst($method), $args);
208+
return $rt;
209+
} catch (\Exception $e) {
210+
$previous = $e->getPrevious();
211+
if ($previous !== null) {
212+
throw $previous;
213+
} else {
214+
throw $e;
186215
}
187216
}
188217
}

src/Qcloud/Cos/MultipartUpload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public function uploadParts($uploadId) {
112112

113113
'rejected' => function ($reason, $index) {
114114
printf("part [%d] upload failed, reason: %s\n", $index, $reason);
115+
throw($reason);
115116
}
116117
]);
117118
$promise = $pool->promise();

0 commit comments

Comments
 (0)