Skip to content

Commit ae23737

Browse files
lewzylulewzylu
andauthored
Dev/patch updateto5 (#184)
* update * update to 2.1.5 Co-authored-by: lewzylu <[email protected]>
1 parent 23f45c0 commit ae23737

File tree

12 files changed

+321
-43
lines changed

12 files changed

+321
-43
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
cos-php-sdk-v5 Upgrade Guide
22
====================
3+
2.1.3 to 2.1.5
4+
----------
5+
- The `download` interface supports breakpoint
6+
- Rename `getPresignetUrl` to `getPresignedUrl`
7+
38
2.1.2 to 2.1.3
49
----------
5-
- Add `download` interface, which is used for concurrent block download.
10+
- Add `download` interface, which is used for concurrent block download
611
- Add callback of `upload` and `download` progress
712
- Fix request retry
813

sample/createFolder.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
try {
16+
$result = $cosClient->putObject(array(
17+
'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
18+
'Key' => 'folder/',
19+
'Body' => "",
20+
));
21+
// 请求成功
22+
print_r($result);
23+
} catch (\Exception $e) {
24+
// 请求失败
25+
echo($e);
26+
}
27+

sample/deleteFolder.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
16+
$cos_path = "cos/folder";
17+
$nextMarker = '';
18+
$isTruncated = true;
19+
while ( $isTruncated ) {
20+
try {
21+
$result = $cosClient->listObjects(
22+
['Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
23+
'Delimiter' => '',
24+
'EncodingType' => 'url',
25+
'Marker' => $nextMarker,
26+
'Prefix' => $cos_path,
27+
'MaxKeys' => 1000]
28+
);
29+
$isTruncated = $result['IsTruncated'];
30+
$nextMarker = $result['NextMarker'];
31+
foreach ( $result['Contents'] as $content ) {
32+
$cos_file_path = $content['Key'];
33+
$local_file_path = $content['Key'];
34+
// 按照需求自定义拼接下载路径
35+
try {
36+
$cosClient->deleteObject(array(
37+
'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
38+
'Key' => $cos_file_path,
39+
));
40+
echo ( $cos_file_path . "\n" );
41+
} catch ( \Exception $e ) {
42+
echo( $e );
43+
}
44+
}
45+
} catch ( \Exception $e ) {
46+
echo( $e );
47+
}
48+
}

sample/download.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
$result = $cosClient->download(
2323
$bucket = 'examplebucket-125000000', //格式:BucketName-APPID
2424
$key = 'exampleobject',
25-
$saveAs = local_path,
26-
$options=['Progress'=>$printbar, //指定进度条
25+
$saveAs = $local_path,
26+
$options=['Progress' => $printbar, //指定进度条
2727
'PartSize' => 10 * 1024 * 1024, //分块大小
28-
'Concurrency' => 5 //并发数
29-
]
28+
'Concurrency' => 5, //并发数
29+
'ResumableDownload' => true, //是否开启断点续传,默认为false
30+
'ResumableTaskFile' => 'tmp.cosresumabletask' //断点文件信息路径,默认为<localpath>.cosresumabletask
31+
]
3032
);
3133
// 请求成功
3234
print_r($result);

sample/downloadFolder.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
require dirname( __FILE__ ) . '/../vendor/autoload.php';
4+
5+
$secretId = 'COS_SECRETID';
6+
//'云 API 密钥 SecretId';
7+
$secretKey = 'COS_SECRETKEY';
8+
//'云 API 密钥 SecretKey';
9+
$region = 'ap-beijing';
10+
//设置一个默认的存储桶地域
11+
$cosClient = new Qcloud\Cos\Client(
12+
array(
13+
'region' => $region,
14+
'schema' => 'https', //协议头部,默认为http
15+
'credentials'=> array(
16+
'secretId' => $secretId ,
17+
'secretKey' => $secretKey
18+
)
19+
)
20+
);
21+
$cos_path = 'cos/folder';
22+
$nextMarker = '';
23+
$isTruncated = true;
24+
25+
while ( $isTruncated ) {
26+
try {
27+
$result = $cosClient->listObjects(
28+
['Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
29+
'Delimiter' => '',
30+
'EncodingType' => 'url',
31+
'Marker' => $nextMarker,
32+
'Prefix' => $cos_path,
33+
'MaxKeys' => 1000]
34+
);
35+
} catch ( \Exception $e ) {
36+
echo( $e );
37+
}
38+
$isTruncated = $result['IsTruncated'];
39+
$nextMarker = $result['NextMarker'];
40+
foreach ( $result['Contents'] as $content ) {
41+
$cos_file_path = $content['Key'];
42+
$local_file_path = $content['Key'];
43+
// 按照需求自定义拼接下载路径
44+
try {
45+
$result = $cosClient->download(
46+
$bucket = 'examplebucket-125000000', //格式:BucketName-APPID
47+
$key = $cos_file_path,
48+
$saveAs = $local_file_path
49+
);
50+
echo ( $cos_file_path . "\n" );
51+
} catch ( \Exception $e ) {
52+
echo( $e );
53+
}
54+
}
55+
}

sample/getPresignetUrl.php renamed to sample/getPresignedUrl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
'secretKey' => $secretKey)));
1515
$local_path = "/data/exampleobject";
1616
try {
17-
$signedUrl = $cosClient->getPresignetUrl(
17+
$signedUrl = $cosClient->getPresignedUrl(
1818
$method='putObject',
1919
$args=['Bucket'=>'examplebucket-1250000000', //格式:BucketName-APPID
2020
'Key'=>'exampleobject',

sample/upload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
'ContentMD5' => 'string',
4343
'ServerSideEncryption' => 'string',
4444
'StorageClass' => 'string', //存储类型
45-
'Progress'=>$printbar, //指定进度条
45+
'Progress' => $printbar, //指定进度条
4646
'PartSize' => 10 * 1024 * 1024, //分块大小
4747
'Concurrency' => 5 //并发数
4848
)

sample/uploadFolder.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
require dirname( __FILE__ ) . '/../vendor/autoload.php';
4+
5+
$secretId = 'COS_SECRETID';
6+
//'云 API 密钥 SecretId';
7+
$secretKey = 'COS_SECRETKEY';
8+
//'云 API 密钥 SecretKey';
9+
$region = 'ap-beijing';
10+
//设置一个默认的存储桶地域
11+
$cosClient = new Qcloud\Cos\Client(
12+
array(
13+
'region' => $region,
14+
'schema' => 'https', //协议头部,默认为http
15+
'credentials'=> array(
16+
'secretId' => $secretId ,
17+
'secretKey' => $secretKey
18+
)
19+
)
20+
);
21+
22+
function uploadfiles( $path, $cosClient ) {
23+
foreach ( scandir( $path ) as $afile ) {
24+
if ( $afile == '.' || $afile == '..' ) continue;
25+
if ( is_dir( $path.'/'.$afile ) ) {
26+
uploadfiles( $path.'/'.$afile, $cosClient );
27+
} else {
28+
$local_file_path = $path.'/'.$afile;
29+
$cos_file_path = $local_file_path;
30+
// 按照需求自定义拼接上传路径
31+
try {
32+
$cosClient->upload(
33+
$bucket = 'examplebucket-125000000', //格式:BucketName-APPID
34+
$key = $cos_file_path,
35+
$body = fopen( $cos_file_path, 'rb' )
36+
);
37+
} catch ( \Exception $e ) {
38+
echo( $e );
39+
}
40+
}
41+
}
42+
}
43+
44+
$local_path = '/data/home/folder';
45+
uploadfiles( $local_path, $cosClient );

src/Qcloud/Cos/Client.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
* @method object GetBucketIntelligentTiering (array $arg)
7878
*/
7979
class Client extends GuzzleClient {
80-
const VERSION = '2.1.3';
80+
const VERSION = '2.1.5';
8181

8282
public $httpClient;
8383

@@ -108,7 +108,6 @@ public function __construct($cosConfig) {
108108
$this->cosConfig['retry'] = isset($cosConfig['retry']) ? $cosConfig['retry'] : 1;
109109
$this->cosConfig['userAgent'] = isset($cosConfig['userAgent']) ? $cosConfig['userAgent'] : 'cos-php-sdk-v5.'. Client::VERSION;
110110
$this->cosConfig['pathStyle'] = isset($cosConfig['pathStyle']) ? $cosConfig['pathStyle'] : false;
111-
112111

113112
$service = Service::getService();
114113
$handler = HandlerStack::create();
@@ -229,11 +228,16 @@ private function createPresignedUrl(RequestInterface $request, $expires) {
229228
}
230229

231230
public function getPresignetUrl($method, $args, $expires = "+30 minutes") {
231+
return $this->getPresignedUrl($method, $args, $expires);
232+
}
233+
234+
public function getPresignedUrl($method, $args, $expires = "+30 minutes") {
232235
$command = $this->getCommand($method, $args);
233236
$request = $this->commandToRequestTransformer($command);
234237
return $this->createPresignedUrl($request, $expires);
235238
}
236239

240+
237241
public function getObjectUrl($bucket, $key, $expires = "+30 minutes", array $args = array()) {
238242
$command = $this->getCommand('GetObject', $args + array('Bucket' => $bucket, 'Key' => $key));
239243
$request = $this->commandToRequestTransformer($command);
@@ -274,6 +278,13 @@ public function download($bucket, $key, $saveAs, $options = array()) {
274278
)
275279
);
276280
$contentLength = $rt['ContentLength'];
281+
$resumableJson = [
282+
'LastModified' => $rt['LastModified'],
283+
'ContentLength' => $rt['ContentLength'],
284+
'ETag' => $rt['ETag'],
285+
'Crc64ecma' => $rt['Crc64ecma']
286+
];
287+
$options['ResumableJson'] = $resumableJson;
277288
} catch (\Exception $e) {
278289
throw ($e);
279290
}

src/Qcloud/Cos/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function region_map( $region ) {
1515
'gz'=>'ap-guangzhou',
1616
'cd'=>'ap-chengdu',
1717
'sgp'=>'ap-singapore' );
18-
if ( array_key_exists( $region, $regionmap ) ) {
18+
if ( isset($regionmap[ $region]) ) {
1919
return $regionmap[$region];
2020
}
2121
return $region;

0 commit comments

Comments
 (0)