From 2050b51027b21454df548ba1b89f0564f0f9c2be Mon Sep 17 00:00:00 2001 From: lewzylu Date: Mon, 23 Sep 2019 21:13:11 +0800 Subject: [PATCH 1/2] update service --- src/Qcloud/Cos/Service.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Qcloud/Cos/Service.php b/src/Qcloud/Cos/Service.php index 0f0453b6..c5ecd633 100644 --- a/src/Qcloud/Cos/Service.php +++ b/src/Qcloud/Cos/Service.php @@ -702,6 +702,21 @@ public static function getService() { 'location' => 'query', 'sentAs' => 'versionId', ), + 'SSECustomerAlgorithm' => array( + 'type' => 'string', + 'location' => 'header', + 'sentAs' => 'x-cos-server-side-encryption-customer-algorithm', + ), + 'SSECustomerKey' => array( + 'type' => 'string', + 'location' => 'header', + 'sentAs' => 'x-cos-server-side-encryption-customer-key', + ), + 'SSECustomerKeyMD5' => array( + 'type' => 'string', + 'location' => 'header', + 'sentAs' => 'x-cos-server-side-encryption-customer-key-MD5', + ), ) ), // 获取 COS 对象的访问权限信息(Access Control List, ACL)的方法. @@ -1074,7 +1089,7 @@ public static function getService() { 'SSEKMSKeyId' => array( 'type' => 'string', 'location' => 'header', - 'sentAs' => 'x-cos-server-side-encryption-aws-kms-key-id', + 'sentAs' => 'x-cos-server-side-encryption-cos-kms-key-id', ), 'RequestPayer' => array( 'type' => 'string', @@ -1084,6 +1099,11 @@ public static function getService() { 'ACP' => array( 'type' => 'object', 'additionalProperties' => true, + ), + 'PicOperations' => array( + 'type' => 'string', + 'location' => 'header', + 'sentAs' => 'Pic-Operations', ) ) ), From 4dda298c648f4be262a9f640a5ae6bf38985f1af Mon Sep 17 00:00:00 2001 From: lewzylu Date: Tue, 24 Sep 2019 12:45:28 +0800 Subject: [PATCH 2/2] update interface of copy --- src/Qcloud/Cos/Client.php | 14 +++++++------- src/Qcloud/Cos/Copy.php | 14 +++++++------- src/Qcloud/Cos/MultipartUpload.php | 8 +++++--- src/Qcloud/Cos/Signature.php | 4 ++-- src/Qcloud/Cos/Tests/Test.php | 31 ++++++++++++++++++++++++------ 5 files changed, 46 insertions(+), 25 deletions(-) diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index 9c65dfd1..797f29d1 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -143,19 +143,19 @@ private function createPresignedUrl(RequestInterface $request, $expires) { public function getPresignetUrl($method, $args, $expires = null) { $command = $this->getCommand($method, $args); $request = $this->commandToRequestTransformer($command); - return ($expires == null) ? $this->createPresignedUrl($request, $expires) : $request->getUri(); + return $this->createPresignedUrl($request, $expires); } public function getObjectUrl($bucket, $key, $expires = null, array $args = array()) { $command = $this->getCommand('GetObject', $args + array('Bucket' => $bucket, 'Key' => $key)); $request = $this->commandToRequestTransformer($command); - return ($expires == null) ? $this->createPresignedUrl($request, $expires) : $request->getUri(); + return $this->createPresignedUrl($request, $expires); } public function upload($bucket, $key, $body, $options = array()) { $body = Psr7\stream_for($body); - $options['min_part_size'] = isset($options['min_part_size']) ? $options['min_part_size'] : MultipartUpload::MIN_PART_SIZE; - if ($body->getSize() < $options['min_part_size']) { + $options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::MIN_PART_SIZE; + if ($body->getSize() < $options['PartSize']) { $rt = $this->putObject(array( 'Bucket' => $bucket, 'Key' => $key, @@ -175,7 +175,7 @@ public function upload($bucket, $key, $body, $options = array()) { public function resumeUpload($bucket, $key, $body, $uploadId, $options = array()) { $body = Psr7\stream_for($body); - $options['min_part_size'] = isset($options['min_part_size']) ? $options['min_part_size'] : MultipartUpload::MIN_PART_SIZE; + $options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : MultipartUpload::DEFAULT_PART_SIZE; $multipartUpload = new MultipartUpload($this, $body, array( 'Bucket' => $bucket, 'Key' => $key, @@ -187,7 +187,7 @@ public function resumeUpload($bucket, $key, $body, $uploadId, $options = array() public function copy($bucket, $key, $copySource, $options = array()) { - $options['min_part_size'] = isset($options['min_part_size']) ? $options['min_part_size'] : Copy::MIN_PART_SIZE; + $options['PartSize'] = isset($options['PartSize']) ? $options['PartSize'] : Copy::DEFAULT_PART_SIZE; // set copysource client $sourceConfig = $this->rawCosConfig; @@ -207,7 +207,7 @@ public function copy($bucket, $key, $copySource, $options = array()) { $contentLength =$rt['ContentLength']; // sample copy - if ($contentLength < $options['min_part_size']) { + if ($contentLength < $options['PartSize']) { $rt = $this->copyObject(array( 'Bucket' => $bucket, 'Key' => $key, diff --git a/src/Qcloud/Cos/Copy.php b/src/Qcloud/Cos/Copy.php index 13033d4d..98ed075d 100644 --- a/src/Qcloud/Cos/Copy.php +++ b/src/Qcloud/Cos/Copy.php @@ -7,10 +7,11 @@ class Copy { /** - * const var: part size from 5MB to 5GB, and max parts of 10000 are allowed for each upload. + * const var: part size from 1MB to 5GB, and max parts of 10000 are allowed for each upload. */ - const MIN_PART_SIZE = 5242880; + const MIN_PART_SIZE = 1048576; const MAX_PART_SIZE = 5368709120; + const DEFAULT_PART_SIZE = 52428800; const MAX_PARTS = 10000; private $client; @@ -23,16 +24,16 @@ class Copy { private $requestList = []; public function __construct($client, $source, $options = array()) { - $minPartSize = $options['min_part_size']; - unset($options['min_part_size']); + $minPartSize = $options['PartSize']; + unset($options['PartSize']); $this->client = $client; $this->copySource = $source; $this->options = $options; $this->size = $source['ContentLength']; unset($source['ContentLength']); $this->partSize = $this->calculatePartSize($minPartSize); - $this->concurrency = isset($options['concurrency']) ? $options['concurrency'] : 10; - $this->retry = isset($options['retry']) ? $options['retry'] : 5; + $this->concurrency = isset($options['Concurrency']) ? $options['Concurrency'] : 10; + $this->retry = isset($options['Retry']) ? $options['Retry'] : 5; } public function copy() { $uploadId= $this->initiateMultipartUpload(); @@ -132,7 +133,6 @@ private function calculatePartSize($minPartSize) $partSize = max($minPartSize, $partSize); $partSize = min($partSize, self::MAX_PART_SIZE); $partSize = max($partSize, self::MIN_PART_SIZE); - return $partSize; } diff --git a/src/Qcloud/Cos/MultipartUpload.php b/src/Qcloud/Cos/MultipartUpload.php index d4b7a10b..b27972fa 100644 --- a/src/Qcloud/Cos/MultipartUpload.php +++ b/src/Qcloud/Cos/MultipartUpload.php @@ -6,10 +6,11 @@ class MultipartUpload { /** - * const var: part size from 5MB to 5GB, and max parts of 10000 are allowed for each upload. + * const var: part size from 1MB to 5GB, and max parts of 10000 are allowed for each upload. */ - const MIN_PART_SIZE = 5242880; + const MIN_PART_SIZE = 1048576; const MAX_PART_SIZE = 5368709120; + const DEFAULT_PART_SIZE = 52428800; const MAX_PARTS = 10000; private $client; @@ -21,7 +22,8 @@ public function __construct($client, $body, $options = array()) { $this->client = $client; $this->body = $body; $this->options = $options; - $this->partSize = $this->calculatePartSize($options['min_part_size']); + $this->partSize = $this->calculatePartSize($options['PartSize']); + unset($options['PartSize']); } public function performUploading() { diff --git a/src/Qcloud/Cos/Signature.php b/src/Qcloud/Cos/Signature.php index 69ee8134..3b70dd83 100644 --- a/src/Qcloud/Cos/Signature.php +++ b/src/Qcloud/Cos/Signature.php @@ -18,7 +18,7 @@ public function signRequest(RequestInterface $request) { $authorization = $this->createAuthorization($request); return $request->withHeader('Authorization', $authorization); } - public function createAuthorization(RequestInterface $request, $expires = "10 minutes") { + public function createAuthorization(RequestInterface $request, $expires = "+30 minutes") { $signTime = (string)(time() - 60) . ';' . (string)(strtotime($expires)); $httpString = strtolower($request->getMethod()) . "\n" . urldecode($request->getUri()->getPath()) . "\n\nhost=" . $request->getUri()->getHost() . "\n"; @@ -31,7 +31,7 @@ public function createAuthorization(RequestInterface $request, $expires = "10 mi "q-signature=$signature"; return $authorization; } - public function createPresignedUrl(RequestInterface $request, $expires = "10 minutes") { + public function createPresignedUrl(RequestInterface $request, $expires = "+30 minutes") { $authorization = $this->createAuthorization($request, $expires); $uri = $request->getUri(); $uri = $uri->withQuery("sign=".urlencode($authorization)); diff --git a/src/Qcloud/Cos/Tests/Test.php b/src/Qcloud/Cos/Tests/Test.php index eea87b6c..a954e669 100644 --- a/src/Qcloud/Cos/Tests/Test.php +++ b/src/Qcloud/Cos/Tests/Test.php @@ -104,6 +104,20 @@ public function testInvalidRegionBucket() } } + /* + * get Service + * 200 + */ + public function testGetService() + { + try { + $this->cosClient->ListBuckets(); + } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); + } + } + /* * put bucket,bucket名称非法 * InvalidBucketName @@ -511,8 +525,6 @@ public function testHeadBucketNonexisted() $this->cosClient->HeadBucket(array( 'Bucket' => $this->bucket2)); } catch (ServiceResponseException $e) { -// echo($e->getExceptionCode()); -// echo($e->getStatusCode()); $this->assertTrue($e->getExceptionCode() === 'NoSuchBucket' && $e->getStatusCode() === 404); } } @@ -987,7 +999,10 @@ public function testUploadComplexObject() { */ public function testUploadLargeObject() { try { - $this->cosClient->upload($this->bucket, 'hello.txt', str_repeat('a', 9 * 1024 * 1024)); + $this->cosClient->upload($bucket=$this->bucket, + $key='你好.txt', + $body=str_repeat('a', 3 * 1024 * 1024 + 1), + $options=['PartSize'=>1024 * 1024 + 1]); } catch (ServiceResponseException $e) { print $e; $this->assertFalse(TRUE); @@ -1137,14 +1152,18 @@ public function testCopySmallObject() { * 复制大文件 * 200 */ - public function testCopyBigObject() { + public function testCopyLargeObject() { try{ - $this->cosClient->upload($this->bucket, '你好.txt', str_repeat('a', 9 * 1024 * 1024)); + $this->cosClient->upload($bucket=$this->bucket, + $key='你好.txt', + $body=str_repeat('a', 3 * 1024 * 1024 + 1), + $options=['PartSize'=>1024 * 1024 + 1]); $this->cosClient->copy($bucket=$this->bucket, $key='hi.txt', $copySource = ['Bucket'=>$this->bucket, 'Region'=>$this->region, - 'Key'=>'你好.txt']); + 'Key'=>'你好.txt'], + $options=['PartSize'=>1024 * 1024 + 1]); } catch (ServiceResponseException $e) { print $e; $this->assertFalse(TRUE);