Skip to content

Commit 781c6ad

Browse files
authored
Feature sse header (#122)
* update service * update interface of copy
1 parent c280e99 commit 781c6ad

File tree

6 files changed

+67
-26
lines changed

6 files changed

+67
-26
lines changed

src/Qcloud/Cos/Client.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,19 @@ private function createPresignedUrl(RequestInterface $request, $expires) {
143143
public function getPresignetUrl($method, $args, $expires = null) {
144144
$command = $this->getCommand($method, $args);
145145
$request = $this->commandToRequestTransformer($command);
146-
return ($expires == null) ? $this->createPresignedUrl($request, $expires) : $request->getUri();
146+
return $this->createPresignedUrl($request, $expires);
147147
}
148148

149149
public function getObjectUrl($bucket, $key, $expires = null, array $args = array()) {
150150
$command = $this->getCommand('GetObject', $args + array('Bucket' => $bucket, 'Key' => $key));
151151
$request = $this->commandToRequestTransformer($command);
152-
return ($expires == null) ? $this->createPresignedUrl($request, $expires) : $request->getUri();
152+
return $this->createPresignedUrl($request, $expires);
153153
}
154154

155155
public function upload($bucket, $key, $body, $options = array()) {
156156
$body = Psr7\stream_for($body);
157-
$options['min_part_size'] = isset($options['min_part_size']) ? $options['min_part_size'] : MultipartUpload::MIN_PART_SIZE;
158-
if ($body->getSize() < $options['min_part_size']) {
157+
$options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::MIN_PART_SIZE;
158+
if ($body->getSize() < $options['PartSize']) {
159159
$rt = $this->putObject(array(
160160
'Bucket' => $bucket,
161161
'Key' => $key,
@@ -175,7 +175,7 @@ public function upload($bucket, $key, $body, $options = array()) {
175175

176176
public function resumeUpload($bucket, $key, $body, $uploadId, $options = array()) {
177177
$body = Psr7\stream_for($body);
178-
$options['min_part_size'] = isset($options['min_part_size']) ? $options['min_part_size'] : MultipartUpload::MIN_PART_SIZE;
178+
$options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::DEFAULT_PART_SIZE;
179179
$multipartUpload = new MultipartUpload($this, $body, array(
180180
'Bucket' => $bucket,
181181
'Key' => $key,
@@ -187,7 +187,7 @@ public function resumeUpload($bucket, $key, $body, $uploadId, $options = array()
187187

188188
public function copy($bucket, $key, $copySource, $options = array()) {
189189

190-
$options['min_part_size'] = isset($options['min_part_size']) ? $options['min_part_size'] : Copy::MIN_PART_SIZE;
190+
$options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : Copy::DEFAULT_PART_SIZE;
191191

192192
// set copysource client
193193
$sourceConfig = $this->rawCosConfig;
@@ -207,7 +207,7 @@ public function copy($bucket, $key, $copySource, $options = array()) {
207207

208208
$contentLength =$rt['ContentLength'];
209209
// sample copy
210-
if ($contentLength < $options['min_part_size']) {
210+
if ($contentLength < $options['PartSize']) {
211211
$rt = $this->copyObject(array(
212212
'Bucket' => $bucket,
213213
'Key' => $key,

src/Qcloud/Cos/Copy.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
class Copy {
99
/**
10-
* const var: part size from 5MB to 5GB, and max parts of 10000 are allowed for each upload.
10+
* const var: part size from 1MB to 5GB, and max parts of 10000 are allowed for each upload.
1111
*/
12-
const MIN_PART_SIZE = 5242880;
12+
const MIN_PART_SIZE = 1048576;
1313
const MAX_PART_SIZE = 5368709120;
14+
const DEFAULT_PART_SIZE = 52428800;
1415
const MAX_PARTS = 10000;
1516

1617
private $client;
@@ -23,16 +24,16 @@ class Copy {
2324
private $requestList = [];
2425

2526
public function __construct($client, $source, $options = array()) {
26-
$minPartSize = $options['min_part_size'];
27-
unset($options['min_part_size']);
27+
$minPartSize = $options['PartSize'];
28+
unset($options['PartSize']);
2829
$this->client = $client;
2930
$this->copySource = $source;
3031
$this->options = $options;
3132
$this->size = $source['ContentLength'];
3233
unset($source['ContentLength']);
3334
$this->partSize = $this->calculatePartSize($minPartSize);
34-
$this->concurrency = isset($options['concurrency']) ? $options['concurrency'] : 10;
35-
$this->retry = isset($options['retry']) ? $options['retry'] : 5;
35+
$this->concurrency = isset($options['Concurrency']) ? $options['Concurrency'] : 10;
36+
$this->retry = isset($options['Retry']) ? $options['Retry'] : 5;
3637
}
3738
public function copy() {
3839
$uploadId= $this->initiateMultipartUpload();
@@ -132,7 +133,6 @@ private function calculatePartSize($minPartSize)
132133
$partSize = max($minPartSize, $partSize);
133134
$partSize = min($partSize, self::MAX_PART_SIZE);
134135
$partSize = max($partSize, self::MIN_PART_SIZE);
135-
136136
return $partSize;
137137
}
138138

src/Qcloud/Cos/MultipartUpload.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
class MultipartUpload {
88
/**
9-
* const var: part size from 5MB to 5GB, and max parts of 10000 are allowed for each upload.
9+
* const var: part size from 1MB to 5GB, and max parts of 10000 are allowed for each upload.
1010
*/
11-
const MIN_PART_SIZE = 5242880;
11+
const MIN_PART_SIZE = 1048576;
1212
const MAX_PART_SIZE = 5368709120;
13+
const DEFAULT_PART_SIZE = 52428800;
1314
const MAX_PARTS = 10000;
1415

1516
private $client;
@@ -21,7 +22,8 @@ public function __construct($client, $body, $options = array()) {
2122
$this->client = $client;
2223
$this->body = $body;
2324
$this->options = $options;
24-
$this->partSize = $this->calculatePartSize($options['min_part_size']);
25+
$this->partSize = $this->calculatePartSize($options['PartSize']);
26+
unset($options['PartSize']);
2527
}
2628

2729
public function performUploading() {

src/Qcloud/Cos/Service.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,21 @@ public static function getService() {
702702
'location' => 'query',
703703
'sentAs' => 'versionId',
704704
),
705+
'SSECustomerAlgorithm' => array(
706+
'type' => 'string',
707+
'location' => 'header',
708+
'sentAs' => 'x-cos-server-side-encryption-customer-algorithm',
709+
),
710+
'SSECustomerKey' => array(
711+
'type' => 'string',
712+
'location' => 'header',
713+
'sentAs' => 'x-cos-server-side-encryption-customer-key',
714+
),
715+
'SSECustomerKeyMD5' => array(
716+
'type' => 'string',
717+
'location' => 'header',
718+
'sentAs' => 'x-cos-server-side-encryption-customer-key-MD5',
719+
),
705720
)
706721
),
707722
// 获取 COS 对象的访问权限信息(Access Control List, ACL)的方法.
@@ -1074,7 +1089,7 @@ public static function getService() {
10741089
'SSEKMSKeyId' => array(
10751090
'type' => 'string',
10761091
'location' => 'header',
1077-
'sentAs' => 'x-cos-server-side-encryption-aws-kms-key-id',
1092+
'sentAs' => 'x-cos-server-side-encryption-cos-kms-key-id',
10781093
),
10791094
'RequestPayer' => array(
10801095
'type' => 'string',
@@ -1084,6 +1099,11 @@ public static function getService() {
10841099
'ACP' => array(
10851100
'type' => 'object',
10861101
'additionalProperties' => true,
1102+
),
1103+
'PicOperations' => array(
1104+
'type' => 'string',
1105+
'location' => 'header',
1106+
'sentAs' => 'Pic-Operations',
10871107
)
10881108
)
10891109
),

src/Qcloud/Cos/Signature.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function signRequest(RequestInterface $request) {
1818
$authorization = $this->createAuthorization($request);
1919
return $request->withHeader('Authorization', $authorization);
2020
}
21-
public function createAuthorization(RequestInterface $request, $expires = "10 minutes") {
21+
public function createAuthorization(RequestInterface $request, $expires = "+30 minutes") {
2222
$signTime = (string)(time() - 60) . ';' . (string)(strtotime($expires));
2323
$httpString = strtolower($request->getMethod()) . "\n" . urldecode($request->getUri()->getPath()) .
2424
"\n\nhost=" . $request->getUri()->getHost() . "\n";
@@ -31,7 +31,7 @@ public function createAuthorization(RequestInterface $request, $expires = "10 mi
3131
"q-signature=$signature";
3232
return $authorization;
3333
}
34-
public function createPresignedUrl(RequestInterface $request, $expires = "10 minutes") {
34+
public function createPresignedUrl(RequestInterface $request, $expires = "+30 minutes") {
3535
$authorization = $this->createAuthorization($request, $expires);
3636
$uri = $request->getUri();
3737
$uri = $uri->withQuery("sign=".urlencode($authorization));

src/Qcloud/Cos/Tests/Test.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ public function testInvalidRegionBucket()
104104
}
105105
}
106106

107+
/*
108+
* get Service
109+
* 200
110+
*/
111+
public function testGetService()
112+
{
113+
try {
114+
$this->cosClient->ListBuckets();
115+
} catch (ServiceResponseException $e) {
116+
print $e;
117+
$this->assertFalse(TRUE);
118+
}
119+
}
120+
107121
/*
108122
* put bucket,bucket名称非法
109123
* InvalidBucketName
@@ -511,8 +525,6 @@ public function testHeadBucketNonexisted()
511525
$this->cosClient->HeadBucket(array(
512526
'Bucket' => $this->bucket2));
513527
} catch (ServiceResponseException $e) {
514-
// echo($e->getExceptionCode());
515-
// echo($e->getStatusCode());
516528
$this->assertTrue($e->getExceptionCode() === 'NoSuchBucket' && $e->getStatusCode() === 404);
517529
}
518530
}
@@ -987,7 +999,10 @@ public function testUploadComplexObject() {
987999
*/
9881000
public function testUploadLargeObject() {
9891001
try {
990-
$this->cosClient->upload($this->bucket, 'hello.txt', str_repeat('a', 9 * 1024 * 1024));
1002+
$this->cosClient->upload($bucket=$this->bucket,
1003+
$key='你好.txt',
1004+
$body=str_repeat('a', 3 * 1024 * 1024 + 1),
1005+
$options=['PartSize'=>1024 * 1024 + 1]);
9911006
} catch (ServiceResponseException $e) {
9921007
print $e;
9931008
$this->assertFalse(TRUE);
@@ -1137,14 +1152,18 @@ public function testCopySmallObject() {
11371152
* 复制大文件
11381153
* 200
11391154
*/
1140-
public function testCopyBigObject() {
1155+
public function testCopyLargeObject() {
11411156
try{
1142-
$this->cosClient->upload($this->bucket, '你好.txt', str_repeat('a', 9 * 1024 * 1024));
1157+
$this->cosClient->upload($bucket=$this->bucket,
1158+
$key='你好.txt',
1159+
$body=str_repeat('a', 3 * 1024 * 1024 + 1),
1160+
$options=['PartSize'=>1024 * 1024 + 1]);
11431161
$this->cosClient->copy($bucket=$this->bucket,
11441162
$key='hi.txt',
11451163
$copySource = ['Bucket'=>$this->bucket,
11461164
'Region'=>$this->region,
1147-
'Key'=>'你好.txt']);
1165+
'Key'=>'你好.txt'],
1166+
$options=['PartSize'=>1024 * 1024 + 1]);
11481167
} catch (ServiceResponseException $e) {
11491168
print $e;
11501169
$this->assertFalse(TRUE);

0 commit comments

Comments
 (0)