From 7c51014ddeca6715b9f64e4916b7a7176c9516c2 Mon Sep 17 00:00:00 2001 From: lewzylu Date: Fri, 20 Sep 2019 17:55:21 +0800 Subject: [PATCH 1/2] update regiontest --- src/Qcloud/Cos/Client.php | 2 +- src/Qcloud/Cos/Common.php | 11 +++---- src/Qcloud/Cos/Tests/Test.php | 60 +++++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/src/Qcloud/Cos/Client.php b/src/Qcloud/Cos/Client.php index fadd0cd9..9c65dfd1 100644 --- a/src/Qcloud/Cos/Client.php +++ b/src/Qcloud/Cos/Client.php @@ -34,7 +34,7 @@ public function __construct($cosConfig) { $this->rawCosConfig = $cosConfig; $this->cosConfig['schema'] = isset($cosConfig['schema']) ? $cosConfig['schema'] : 'http'; $this->cosConfig['endpoint'] = isset($cosConfig['endpoint']) ? $cosConfig['endpoint'] : null; - $this->cosConfig['region'] = isset($regionmap[$cosConfig['region']]) ? region_map($cosConfig['region']) : $cosConfig['region']; + $this->cosConfig['region'] = region_map($cosConfig['region']); $this->cosConfig['appId'] = isset($cosConfig['credentials']['appId']) ? $cosConfig['credentials']['appId'] : null; $this->cosConfig['secretId'] = isset($cosConfig['credentials']['secretId']) ? $cosConfig['credentials']['secretId'] : ""; $this->cosConfig['secretKey'] = isset($cosConfig['credentials']['secretKey']) ? $cosConfig['credentials']['secretKey'] : ""; diff --git a/src/Qcloud/Cos/Common.php b/src/Qcloud/Cos/Common.php index 03bab953..c188002e 100644 --- a/src/Qcloud/Cos/Common.php +++ b/src/Qcloud/Cos/Common.php @@ -2,7 +2,8 @@ namespace Qcloud\Cos; -$regionmap = array('cn-east'=>'ap-shanghai', +function region_map($region) { + $regionmap = array('cn-east'=>'ap-shanghai', 'cn-south'=>'ap-guangzhou', 'cn-north'=>'ap-beijing-1', 'cn-south-2'=>'ap-guangzhou-2', @@ -13,11 +14,9 @@ 'sh'=>'ap-shanghai', 'gz'=>'ap-guangzhou', 'cd'=>'ap-chengdu', - 'sgp'=>'ap-singapore',); - -function region_map($region) { - if (array_key_exists($region, regionmap)) { - return regionmap[$region]; + 'sgp'=>'ap-singapore'); + if (array_key_exists($region, $regionmap)) { + return $regionmap[$region]; } return $region; } diff --git a/src/Qcloud/Cos/Tests/Test.php b/src/Qcloud/Cos/Tests/Test.php index 55eb461b..550db17c 100644 --- a/src/Qcloud/Cos/Tests/Test.php +++ b/src/Qcloud/Cos/Tests/Test.php @@ -32,8 +32,8 @@ protected function tearDown() { /********************************** * TestBucket **********************************/ - - /* + + /* * put bucket,bucket已经存在 * BucketAlreadyOwnedByYou * 409 @@ -47,6 +47,62 @@ public function testCreateExistingBucket() } } + /* + * put bucket, 创建所有region的bucket + * 409 + */ + public function testValidRegionBucket() + { + $regionlist = array('cn-east','ap-shanghai', + 'cn-south','ap-guangzhou', + 'cn-north','ap-beijing-1', + 'cn-south-2','ap-guangzhou-2', + 'cn-southwest','ap-chengdu', + 'sg','ap-singapore', + 'tj','ap-beijing-1', + 'bj','ap-beijing', + 'sh','ap-shanghai', + 'gz','ap-guangzhou', + 'cd','ap-chengdu', + 'sgp','ap-singapore'); + foreach ($regionlist as$region) { + try { + + $this->cosClient = new Client(array('region' => $region, + 'credentials' => array( + 'appId' => getenv('COS_APPID'), + 'secretId' => getenv('COS_KEY'), + 'secretKey' => getenv('COS_SECRET')))); + $this->cosClient->createBucket(['Bucket' => $this->bucket]); + } catch (ServiceResponseException $e) { + $this->assertEquals([$e->getStatusCode()], [409]); + } + } + } + + /* + * put bucket, 不合法的region名 + * 409 + */ + public function testInvalidRegionBucket() + { + $regionlist = array('cn-east-2','ap-shanghai-3'); + foreach ($regionlist as$region) { + try { + $this->cosClient = new Client(array('region' => $region, + 'credentials' => array( + 'appId' => getenv('COS_APPID'), + 'secretId' => getenv('COS_KEY'), + 'secretKey' => getenv('COS_SECRET')))); + $this->cosClient->createBucket(['Bucket' => $this->bucket]); + } catch (ServiceResponseException $e) { + $this->assertFalse(TRUE); + } catch (\GuzzleHttp\Exception\ConnectException $e) { + $this->assertTrue(TRUE); + } + } + } + /* * put bucket,bucket名称非法 * InvalidBucketName From b6de78608e189f8c7b8bcbc32e378d7d58a33ac2 Mon Sep 17 00:00:00 2001 From: lewzylu Date: Sun, 22 Sep 2019 22:02:50 +0800 Subject: [PATCH 2/2] Add copy test --- src/Qcloud/Cos/Tests/Test.php | 136 ++++++++++++++++++++++------------ 1 file changed, 88 insertions(+), 48 deletions(-) diff --git a/src/Qcloud/Cos/Tests/Test.php b/src/Qcloud/Cos/Tests/Test.php index 550db17c..eea87b6c 100644 --- a/src/Qcloud/Cos/Tests/Test.php +++ b/src/Qcloud/Cos/Tests/Test.php @@ -9,15 +9,16 @@ class COSTest extends \PHPUnit_Framework_TestCase const SYNC_TIME = 5; private $cosClient; private $bucket; + private $region; private $message; private $ass; protected function setUp() { $this->bucket = getenv('COS_BUCKET'); + $this->region = getenv('COS_REGION'); $this->bucket2 = "tmp".$this->bucket; - $this->cosClient = new Client(array('region' => getenv('COS_REGION'), + $this->cosClient = new Client(array('region' => $this->region, 'credentials' => array( - 'appId' => getenv('COS_APPID'), 'secretId' => getenv('COS_KEY'), 'secretKey' => getenv('COS_SECRET')))); try { @@ -111,8 +112,9 @@ public function testInvalidRegionBucket() public function testCreateInvalidBucket() { try { - $this->cosClient->createBucket(array('Bucket' => 'qwe_213')); + $this->cosClient->createBucket(array('Bucket' => 'qwe_123' . $this->bucket)); } catch (ServiceResponseException $e) { + print $e; $this->assertTrue($e->getExceptionCode() === 'InvalidBucketName' && $e->getStatusCode() === 400); } } @@ -132,8 +134,8 @@ public function testCreatePrivateBucket() sleep(COSTest::SYNC_TIME); TestHelper::nuke($this->bucket2); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -155,8 +157,8 @@ public function testCreatePublicReadBucket() sleep(COSTest::SYNC_TIME); TestHelper::nuke($this->bucket2); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -197,8 +199,8 @@ public function testPutBucketAclPrivate() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -216,8 +218,8 @@ public function testPutBucketAclPublicRead() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -254,8 +256,8 @@ public function testPutBucketAclReadToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -273,8 +275,8 @@ public function testPutBucketAclWriteToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -292,8 +294,8 @@ public function testPutBucketAclFullToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -311,8 +313,8 @@ public function testPutBucketAclToUsers() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -330,8 +332,8 @@ public function testPutBucketAclToSubuser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -351,8 +353,8 @@ public function testPutBucketAclReadWriteFull() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -402,8 +404,8 @@ public function testPutBucketAclByBody() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -434,8 +436,8 @@ public function testPutBucketAclByBodyToAnyone() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -476,8 +478,8 @@ public function testPutBucketAclCover() 'Bucket' => $this->bucket, 'GrantWrite' => 'id="qcs::cam::uin/2779643970:uin/2779643970"')); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -491,8 +493,8 @@ public function testHeadBucket() $this->cosClient->HeadBucket(array( 'Bucket' => $this->bucket)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -525,8 +527,8 @@ public function testGetBucketEmpty() $this->cosClient->ListObjects(array( 'Bucket' => $this->bucket)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -582,8 +584,8 @@ public function testPutBucketCors() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -624,8 +626,8 @@ public function testGetBucketCors() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -688,8 +690,8 @@ public function testGetBucketLifecycle() 'Bucket' => $this->bucket, )); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -735,8 +737,8 @@ public function testDeleteBucketLifecycle() 'Bucket' => $this->bucket, )); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -786,8 +788,8 @@ public function testPutBucket2() $this->cosClient->createBucket(array('Bucket' => '12345-'.$this->bucket)); $this->cosClient->deleteBucket(array('Bucket' => '12345-'.$this->bucket)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -798,11 +800,11 @@ public function testPutBucket2() public function testPutBucket3() { try { - $this->cosClient->createBucket(array('Bucket' => $this->bucket.'-12333-4445')); - $this->cosClient->deleteBucket(array('Bucket' => $this->bucket.'-12333-4445')); + $this->cosClient->createBucket(array('Bucket' => '12-333-4445' . $this->bucket)); + $this->cosClient->deleteBucket(array('Bucket' => '12-333-4445' . $this->bucket)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -815,8 +817,8 @@ public function testGetBucketLocation() try { $this->cosClient->getBucketLocation(array('Bucket' => $this->bucket)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -858,8 +860,8 @@ public function testPutObjectEncryption() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -892,8 +894,8 @@ public function testUploadSmallObject() { try { $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World'); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -905,8 +907,8 @@ public function testPutObjectEmpty() { try { $this->cosClient->upload($this->bucket, '你好.txt', '123'); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -919,8 +921,8 @@ public function testPutObjectExisted() { $this->cosClient->upload($this->bucket, '你好.txt', '1234124'); $this->cosClient->upload($this->bucket, '你好.txt', '请二位qwe'); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -938,8 +940,8 @@ public function testPutObjectMeta() { 'lew' => str_repeat('a', 1 * 1024), ))); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -974,8 +976,8 @@ public function testUploadComplexObject() { try { $this->cosClient->upload($this->bucket, '→↓←→↖↗↙↘! \"#$%&\'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 'Hello World'); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -987,8 +989,8 @@ public function testUploadLargeObject() { try { $this->cosClient->upload($this->bucket, 'hello.txt', str_repeat('a', 9 * 1024 * 1024)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1003,8 +1005,8 @@ public function testGetObject() { 'Bucket' => $this->bucket, 'Key' => '你好.txt',)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1019,8 +1021,8 @@ public function testGetObjectSpecialName() { 'Bucket' => $this->bucket, 'Key' => '你好<>!@#^%^&*&(&^!@#@!.txt',)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1036,8 +1038,8 @@ public function testGetObjectIfMatchTrue() { 'Key' => '你好.txt', 'IfMatch' => '"b10a8db164e0754105b7a99be72e3fe5"')); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1075,8 +1077,8 @@ public function testGetObjectIfNoneMatchTrue() { 'Key' => '你好.txt', 'IfNoneMatch' => '"b10a8db164e0754105b7a99be72e3fe5"')); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1095,6 +1097,8 @@ public function testGetObjectIfNoneMatchFalse() { 'IfNoneMatch' => '""')); } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); } } @@ -1106,8 +1110,44 @@ public function testGetObjectUrl() { try{ $this->cosClient->getObjectUrl($this->bucket, 'hello.txt', '+10 minutes'); } catch (ServiceResponseException $e) { + print $e; $this->assertFalse(TRUE); + } + } + + /* + * 复制小文件 + * 200 + */ + public function testCopySmallObject() { + try{ + $this->cosClient->upload($this->bucket, '你好.txt', 'Hello World'); + $this->cosClient->copy($bucket=$this->bucket, + $key='hi.txt', + $copySource = ['Bucket'=>$this->bucket, + 'Region'=>$this->region, + 'Key'=>'你好.txt']); + } catch (ServiceResponseException $e) { print $e; + $this->assertFalse(TRUE); + } + } + + /* + * 复制大文件 + * 200 + */ + public function testCopyBigObject() { + try{ + $this->cosClient->upload($this->bucket, '你好.txt', str_repeat('a', 9 * 1024 * 1024)); + $this->cosClient->copy($bucket=$this->bucket, + $key='hi.txt', + $copySource = ['Bucket'=>$this->bucket, + 'Region'=>$this->region, + 'Key'=>'你好.txt']); + } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); } } @@ -1140,8 +1180,8 @@ public function testPutObjectACL() { ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1176,8 +1216,8 @@ public function testGetObjectACL() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1197,8 +1237,8 @@ public function testPutObjectAclPrivate() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1218,8 +1258,8 @@ public function testPutObjectAclPublicRead() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1260,8 +1300,8 @@ public function testPutObjectAclReadToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1281,8 +1321,8 @@ public function testPutObjectAclFullToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1302,8 +1342,8 @@ public function testPutObjectAclToUsers() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1323,8 +1363,8 @@ public function testPutObjectAclToSubuser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1379,8 +1419,8 @@ public function testPutObjectAclByBody() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1414,8 +1454,8 @@ public function testPutObjectAclByBodyToAnyone() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } }