Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@ cos-php-sdk-v5 Upgrade Guide
cos-php-sdk-v5 now uses [GuzzleHttp] for HTTP message.
Due to fact, it depending on PHP >= 5.6.

- Use the `Qcloud\Cos\Client\createPresignedUrl()` method instead of the `Qcloud\Cos\Command\createPresignedUrl()`
- Use the `Qcloud\Cos\Client\getPresignetUrl()` method instead of the `Qcloud\Cos\Command\createPresignedUrl()`

v2:
```php
$signedUrl = $cosClient->getPresignetUrl($method='putObject',
$args=['Bucket'=>'examplebucket-1250000000', 'Key'=>'exampleobject', 'Body'=>''],
$expires='+30 minutes');
```

v1:
```php
$command = $cosClient->getCommand('putObject', array(
'Bucket' => "examplebucket-1250000000",
'Key' => "exampleobject",
'Body' => '',
));
$signedUrl = $command->createPresignedUrl('+30 minutes');
```

- `$copSource` parameters of the `Qcloud\Cos\Client\Copy` interface are no longer compatible with older versions.

v2:
Expand Down
14 changes: 7 additions & 7 deletions src/Qcloud/Cos/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions src/Qcloud/Cos/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 5 additions & 3 deletions src/Qcloud/Cos/MultipartUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
22 changes: 21 additions & 1 deletion src/Qcloud/Cos/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)的方法.
Expand Down Expand Up @@ -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',
Expand All @@ -1084,6 +1099,11 @@ public static function getService() {
'ACP' => array(
'type' => 'object',
'additionalProperties' => true,
),
'PicOperations' => array(
'type' => 'string',
'location' => 'header',
'sentAs' => 'Pic-Operations',
)
)
),
Expand Down
4 changes: 2 additions & 2 deletions src/Qcloud/Cos/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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));
Expand Down
31 changes: 25 additions & 6 deletions src/Qcloud/Cos/Tests/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down