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