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..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 { @@ -32,8 +33,8 @@ protected function tearDown() { /********************************** * TestBucket **********************************/ - - /* + + /* * put bucket,bucket已经存在 * BucketAlreadyOwnedByYou * 409 @@ -47,6 +48,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 @@ -55,8 +112,9 @@ public function testCreateExistingBucket() 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); } } @@ -76,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); } } @@ -99,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); } } @@ -141,8 +199,8 @@ public function testPutBucketAclPrivate() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -160,8 +218,8 @@ public function testPutBucketAclPublicRead() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -198,8 +256,8 @@ public function testPutBucketAclReadToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -217,8 +275,8 @@ public function testPutBucketAclWriteToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -236,8 +294,8 @@ public function testPutBucketAclFullToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -255,8 +313,8 @@ public function testPutBucketAclToUsers() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -274,8 +332,8 @@ public function testPutBucketAclToSubuser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -295,8 +353,8 @@ public function testPutBucketAclReadWriteFull() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -346,8 +404,8 @@ public function testPutBucketAclByBody() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -378,8 +436,8 @@ public function testPutBucketAclByBodyToAnyone() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -420,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); } } @@ -435,8 +493,8 @@ public function testHeadBucket() $this->cosClient->HeadBucket(array( 'Bucket' => $this->bucket)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -469,8 +527,8 @@ public function testGetBucketEmpty() $this->cosClient->ListObjects(array( 'Bucket' => $this->bucket)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -526,8 +584,8 @@ public function testPutBucketCors() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -568,8 +626,8 @@ public function testGetBucketCors() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -632,8 +690,8 @@ public function testGetBucketLifecycle() 'Bucket' => $this->bucket, )); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -679,8 +737,8 @@ public function testDeleteBucketLifecycle() 'Bucket' => $this->bucket, )); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -730,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); } } @@ -742,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); } } @@ -759,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); } } @@ -802,8 +860,8 @@ public function testPutObjectEncryption() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -836,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); } } @@ -849,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); } } @@ -863,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); } } @@ -882,8 +940,8 @@ public function testPutObjectMeta() { 'lew' => str_repeat('a', 1 * 1024), ))); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -918,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); } } @@ -931,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); } } @@ -947,8 +1005,8 @@ public function testGetObject() { 'Bucket' => $this->bucket, 'Key' => '你好.txt',)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -963,8 +1021,8 @@ public function testGetObjectSpecialName() { 'Bucket' => $this->bucket, 'Key' => '你好<>!@#^%^&*&(&^!@#@!.txt',)); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -980,8 +1038,8 @@ public function testGetObjectIfMatchTrue() { 'Key' => '你好.txt', 'IfMatch' => '"b10a8db164e0754105b7a99be72e3fe5"')); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1019,8 +1077,8 @@ public function testGetObjectIfNoneMatchTrue() { 'Key' => '你好.txt', 'IfNoneMatch' => '"b10a8db164e0754105b7a99be72e3fe5"')); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1039,6 +1097,8 @@ public function testGetObjectIfNoneMatchFalse() { 'IfNoneMatch' => '""')); } catch (ServiceResponseException $e) { + print $e; + $this->assertFalse(TRUE); } } @@ -1050,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); } } @@ -1084,8 +1180,8 @@ public function testPutObjectACL() { ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1120,8 +1216,8 @@ public function testGetObjectACL() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1141,8 +1237,8 @@ public function testPutObjectAclPrivate() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1162,8 +1258,8 @@ public function testPutObjectAclPublicRead() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1204,8 +1300,8 @@ public function testPutObjectAclReadToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1225,8 +1321,8 @@ public function testPutObjectAclFullToUser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1246,8 +1342,8 @@ public function testPutObjectAclToUsers() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1267,8 +1363,8 @@ public function testPutObjectAclToSubuser() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1323,8 +1419,8 @@ public function testPutObjectAclByBody() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } } @@ -1358,8 +1454,8 @@ public function testPutObjectAclByBodyToAnyone() ) ); } catch (ServiceResponseException $e) { - $this->assertFalse(TRUE); print $e; + $this->assertFalse(TRUE); } }