Skip to content

Commit 678f0be

Browse files
authored
feat(appendObject):新增appendObject接口并修复部分bug (#201)
* doc(*):修复type error并切换COS_SECRET*->SECRET* - 将totol修改为total - 将COS_SECRETID修改为SECRETID、COS_SECRETKEY修改为SECRETKEY,防止混淆 * feat(appendObject):新增appendObject接口并修复部分bug - 新增appendObject接口,包括sample,service,test - 修复getpresignedUrl示例bug,并增加$expire的逻辑判断 * feat(getObjectUrlWithoutSign):增加无签名对象下载地址,修复bug - 增加无签名对象下载地址的sample,逻辑代码以及测试代码 - 增加全球加速相关配置参数 - 修复部分bug
1 parent b7a6a25 commit 678f0be

File tree

8 files changed

+347
-4
lines changed

8 files changed

+347
-4
lines changed

sample/appendObject.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 = "SECRETID"; //"云 API 密钥 SecretId";
6+
$secretKey = "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+
try {
17+
$result = $cosClient->appendObject(array(
18+
'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
19+
'Key' => 'exampleobject',
20+
'Position' => 0, //追加对象位置
21+
'Body' => fopen($local_path, 'rb'),//读取文件内容
22+
));
23+
/*
24+
$result = $cosClient->appendObject(array(
25+
'Bucket' => 'examplebucket-125000000', //格式:BucketName-APPID
26+
'Key' => 'exampleobject',
27+
'Position' => (integer)$result['Position'], //取出上一个追加文件的对象位置进行追加
28+
'Body' => "hello", //文件流
29+
));
30+
*/
31+
// 请求成功
32+
print_r($result);
33+
} catch (\Exception $e) {
34+
// 请求失败
35+
echo($e);
36+
}
37+

sample/getObjectWithoutSign.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 = "SECRETID"; //"云 API 密钥 SecretId";
6+
$secretKey = "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+
try {
17+
$bucket = 'examplebucket-125000000'; //存储桶,格式:BucketName-APPID
18+
$key = "exampleobject"; //对象在存储桶中的位置,即对象键
19+
$signedUrl = $cosClient -> getObjectUrlWithoutSign($bucket, $key);
20+
21+
// 请求成功
22+
echo $signedUrl;
23+
} catch (\Exception $e) {
24+
// 请求失败
25+
print_r($e);
26+
}
27+

sample/getPresignedUrl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
$args=['Bucket'=>'examplebucket-1250000000', //格式:BucketName-APPID
2020
'Key'=>'exampleobject',
2121
'Body'=>''],
22-
$expires='+30 minutes"');
22+
$expires='+30 minutes');
2323
// 请求成功
2424
echo($signedUrl);
2525
} catch (\Exception $e) {

src/Qcloud/Cos/Client.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
* @method object GetBucketImageStyle (array $arg)
8585
* @method object DeleteBucketImageStyle (array $arg)
8686
* @method object PutBucketGuetzli (array $arg)
87+
* @method object AppendObject (array $arg)
8788
* @method object GetBucketGuetzli (array $arg)
8889
* @method object DeleteBucketGuetzli (array $arg)
8990
*/
@@ -120,6 +121,7 @@ public function __construct($cosConfig) {
120121
$this->cosConfig['userAgent'] = isset($cosConfig['userAgent']) ? $cosConfig['userAgent'] : 'cos-php-sdk-v5.'. Client::VERSION;
121122
$this->cosConfig['pathStyle'] = isset($cosConfig['pathStyle']) ? $cosConfig['pathStyle'] : false;
122123
$this->cosConfig['allow_redirects'] = isset($cosConfig['allow_redirects']) ? $cosConfig['allow_redirects'] : false;
124+
$this->cosConfig['allow_accelerate'] = isset($cosConfig['allow_accelerate']) ? $cosConfig['allow_accelerate'] : false;
123125
try {
124126
$this->inputCheck();
125127
} catch (\Exception $e) {
@@ -141,8 +143,9 @@ public function __construct($cosConfig) {
141143
}
142144
$handler->push($this::handleErrors());
143145
$this->signature = new Signature($this->cosConfig['secretId'], $this->cosConfig['secretKey'], $this->cosConfig['token']);
146+
$area = $this->cosConfig['allow_accelerate'] ? 'accelerate' : $this->cosConfig['region'];
144147
$this->httpClient = new HttpClient([
145-
'base_uri' => $this->cosConfig['schema'].'://cos.' . $this->cosConfig['region'] . '.myqcloud.com/',
148+
'base_uri' => $this->cosConfig['schema'].'://cos.' . $area . '.myqcloud.com/',
146149
'timeout' => $this->cosConfig['timeout'],
147150
'handler' => $handler,
148151
'proxy' => $this->cosConfig['proxy'],

src/Qcloud/Cos/CommandToRequestTransformer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ public function bucketStyleTransformer( CommandInterface $command, RequestInterf
7272
$domain_type = '.pic.';
7373
}
7474

75-
$origin_host = $bucketname . $domain_type . $this->config['region'] . '.' . $this->config['endpoint'];
75+
$origin_host = $this->config['allow_accelerate'] ?
76+
$bucketname . $domain_type . 'accelerate' . '.' . $this->config['endpoint'] :
77+
$bucketname . $domain_type . $this->config['region'] . '.' . $this->config['endpoint'];
78+
7679
// domain
7780
if ( $this->config['domain'] != null ) {
7881
$origin_host = $this->config['domain'];

src/Qcloud/Cos/Service.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,145 @@ public static function getService() {
13081308
)
13091309
)
13101310
),
1311+
// 追加对象
1312+
'AppendObject' => array(
1313+
'httpMethod' => 'POST',
1314+
'uri' => '/{Bucket}{/Key*}?append',
1315+
'class' => 'Qcloud\\Cos\\Command',
1316+
'responseClass' => 'AppendObjectOutput',
1317+
'responseType' => 'model',
1318+
'data' => array(
1319+
'xmlRoot' => array(
1320+
'name' => 'AppendObjectRequest'
1321+
)
1322+
),
1323+
'parameters' => array(
1324+
'Position' => array(
1325+
'type' => 'integer',
1326+
'required' => true,
1327+
'location' => 'query',
1328+
'sentAs' => 'position'
1329+
),
1330+
'ACL' => array(
1331+
'type' => 'string',
1332+
'location' => 'header',
1333+
'sentAs' => 'x-cos-acl'
1334+
),
1335+
'Body' => array(
1336+
'required' => true,
1337+
'type' => array(
1338+
'any'
1339+
),
1340+
'location' => 'body'
1341+
),
1342+
'Bucket' => array(
1343+
'required' => true,
1344+
'type' => 'string',
1345+
'location' => 'uri'
1346+
),
1347+
'CacheControl' => array(
1348+
'type' => 'string',
1349+
'location' => 'header',
1350+
'sentAs' => 'Cache-Control'
1351+
),
1352+
'ContentDisposition' => array(
1353+
'type' => 'string',
1354+
'location' => 'header',
1355+
'sentAs' => 'Content-Disposition'
1356+
),
1357+
'ContentEncoding' => array(
1358+
'type' => 'string',
1359+
'location' => 'header',
1360+
'sentAs' => 'Content-Encoding'
1361+
),
1362+
'ContentLanguage' => array(
1363+
'type' => 'string',
1364+
'location' => 'header',
1365+
'sentAs' => 'Content-Language'
1366+
),
1367+
'ContentLength' => array(
1368+
'type' => 'numeric',
1369+
'minimum'=> 0,
1370+
'location' => 'header',
1371+
'sentAs' => 'Content-Length'
1372+
),
1373+
'ContentMD5' => array(
1374+
'type' => array(
1375+
'boolean'
1376+
),
1377+
'location' => 'header',
1378+
'sentAs' => 'Content-MD5'
1379+
),
1380+
'ContentType' => array(
1381+
'type' => 'string',
1382+
'location' => 'header',
1383+
'sentAs' => 'Content-Type'
1384+
),
1385+
'Key' => array(
1386+
'required' => true,
1387+
'type' => 'string',
1388+
'location' => 'uri',
1389+
'minLength' => 1,
1390+
'filters' => array(
1391+
'Qcloud\\Cos\\Client::explodeKey'
1392+
)
1393+
),
1394+
'ServerSideEncryption' => array(
1395+
'type' => 'string',
1396+
'location' => 'header',
1397+
'sentAs' => 'x-cos-server-side-encryption',
1398+
),
1399+
'StorageClass' => array(
1400+
'type' => 'string',
1401+
'location' => 'header',
1402+
'sentAs' => 'x-cos-storage-class',
1403+
),
1404+
'WebsiteRedirectLocation' => array(
1405+
'type' => 'string',
1406+
'location' => 'header',
1407+
'sentAs' => 'x-cos-website-redirect-location',
1408+
),
1409+
'SSECustomerAlgorithm' => array(
1410+
'type' => 'string',
1411+
'location' => 'header',
1412+
'sentAs' => 'x-cos-server-side-encryption-customer-algorithm',
1413+
),
1414+
'SSECustomerKey' => array(
1415+
'type' => 'string',
1416+
'location' => 'header',
1417+
'sentAs' => 'x-cos-server-side-encryption-customer-key',
1418+
),
1419+
'SSECustomerKeyMD5' => array(
1420+
'type' => 'string',
1421+
'location' => 'header',
1422+
'sentAs' => 'x-cos-server-side-encryption-customer-key-MD5',
1423+
),
1424+
'SSEKMSKeyId' => array(
1425+
'type' => 'string',
1426+
'location' => 'header',
1427+
'sentAs' => 'x-cos-server-side-encryption-cos-kms-key-id',
1428+
),
1429+
'RequestPayer' => array(
1430+
'type' => 'string',
1431+
'location' => 'header',
1432+
'sentAs' => 'x-cos-request-payer',
1433+
),
1434+
'ACP' => array(
1435+
'type' => 'object',
1436+
'additionalProperties' => true,
1437+
),
1438+
'PicOperations' => array(
1439+
'type' => 'string',
1440+
'location' => 'header',
1441+
'sentAs' => 'Pic-Operations',
1442+
),
1443+
'TrafficLimit' => array(
1444+
'type' => 'integer',
1445+
'location' => 'header',
1446+
'sentAs' => 'x-cos-traffic-limit',
1447+
)
1448+
)
1449+
),
13111450
// 设置 COS 对象的访问权限信息(Access Control List, ACL)的方法.
13121451
'PutObjectAcl' => array(
13131452
'httpMethod' => 'PUT',
@@ -4660,6 +4799,25 @@ public static function getService() {
46604799
),
46614800
),
46624801
),
4802+
'AppendObjectOutput' => array(
4803+
'type' => 'object',
4804+
'additionalProperties' => true,
4805+
'properties' => array(
4806+
'ETag' => array(
4807+
'type' => 'string',
4808+
'location' => 'header',
4809+
),
4810+
'Position' => array(
4811+
'type' => 'integer',
4812+
'location' => 'header',
4813+
'sentAs' => 'x-cos-next-append-position',
4814+
),
4815+
'RequestId' => array(
4816+
'location' => 'header',
4817+
'sentAs' => 'x-cos-request-id',
4818+
)
4819+
),
4820+
),
46634821
'PutObjectAclOutput' => array(
46644822
'type' => 'object',
46654823
'additionalProperties' => true,

src/Qcloud/Cos/Signature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function signRequest( RequestInterface $request ) {
4646
}
4747

4848
public function createAuthorization( RequestInterface $request, $expires = '+30 minutes' ) {
49-
if ( is_null( $expires ) ) {
49+
if ( is_null( $expires ) || !strtotime( $expires )) {
5050
$expires = '+30 minutes';
5151
}
5252
$signTime = ( string )( time() - 60 ) . ';' . ( string )( strtotime( $expires ) );

0 commit comments

Comments
 (0)