diff --git a/src/Qiniu/Config.php b/src/Qiniu/Config.php index 5bf413f7..6222134a 100644 --- a/src/Qiniu/Config.php +++ b/src/Qiniu/Config.php @@ -53,6 +53,27 @@ public function getUpHost($accessKey, $bucket) return $scheme . $host; } + public function getUpHostV2($accessKey, $bucket) + { + list($region, $err) = $this->getRegionV2($accessKey, $bucket); + if ($err != null) { + return array(null, $err); + } + + if ($this->useHTTPS === true) { + $scheme = "https://"; + } else { + $scheme = "http://"; + } + + $host = $region->srcUpHosts[0]; + if ($this->useCdnDomains === true) { + $host = $region->cdnUpHosts[0]; + } + + return array($scheme . $host, null); + } + public function getUpBackupHost($accessKey, $bucket) { $region = $this->getRegion($accessKey, $bucket); @@ -70,6 +91,27 @@ public function getUpBackupHost($accessKey, $bucket) return $scheme . $host; } + public function getUpBackupHostV2($accessKey, $bucket) + { + list($region, $err) = $this->getRegionV2($accessKey, $bucket); + if ($err != null) { + return array(null, $err); + } + + if ($this->useHTTPS === true) { + $scheme = "https://"; + } else { + $scheme = "http://"; + } + + $host = $region->cdnUpHosts[0]; + if ($this->useCdnDomains === true) { + $host = $region->srcUpHosts[0]; + } + + return array($scheme . $host, null); + } + public function getRsHost($accessKey, $bucket) { $region = $this->getRegion($accessKey, $bucket); @@ -83,6 +125,22 @@ public function getRsHost($accessKey, $bucket) return $scheme . $region->rsHost; } + public function getRsHostV2($accessKey, $bucket) + { + list($region, $err) = $this->getRegionV2($accessKey, $bucket); + if ($err != null) { + return array(null, $err); + } + + if ($this->useHTTPS === true) { + $scheme = "https://"; + } else { + $scheme = "http://"; + } + + return array($scheme . $region->rsHost, null); + } + public function getRsfHost($accessKey, $bucket) { $region = $this->getRegion($accessKey, $bucket); @@ -96,6 +154,22 @@ public function getRsfHost($accessKey, $bucket) return $scheme . $region->rsfHost; } + public function getRsfHostV2($accessKey, $bucket) + { + list($region, $err) = $this->getRegionV2($accessKey, $bucket); + if ($err != null) { + return array(null, $err); + } + + if ($this->useHTTPS === true) { + $scheme = "https://"; + } else { + $scheme = "http://"; + } + + return array($scheme . $region->rsfHost, null); + } + public function getIovipHost($accessKey, $bucket) { $region = $this->getRegion($accessKey, $bucket); @@ -109,6 +183,22 @@ public function getIovipHost($accessKey, $bucket) return $scheme . $region->iovipHost; } + public function getIovipHostV2($accessKey, $bucket) + { + list($region, $err) = $this->getRegionV2($accessKey, $bucket); + if ($err != null) { + return array(null, $err); + } + + if ($this->useHTTPS === true) { + $scheme = "https://"; + } else { + $scheme = "http://"; + } + + return array($scheme . $region->iovipHost, null); + } + public function getApiHost($accessKey, $bucket) { $region = $this->getRegion($accessKey, $bucket); @@ -122,6 +212,22 @@ public function getApiHost($accessKey, $bucket) return $scheme . $region->apiHost; } + public function getApiHostV2($accessKey, $bucket) + { + list($region, $err) = $this->getRegionV2($accessKey, $bucket); + if ($err != null) { + return array(null, $err); + } + + if ($this->useHTTPS === true) { + $scheme = "https://"; + } else { + $scheme = "http://"; + } + + return array($scheme . $region->apiHost, null); + } + private function getRegion($accessKey, $bucket) { $cacheId = "$accessKey:$bucket"; @@ -143,4 +249,25 @@ private function getRegion($accessKey, $bucket) } return $region; } + + private function getRegionV2($accessKey, $bucket) + { + $cacheId = "$accessKey:$bucket"; + + if (isset($this->regionCache[$cacheId])) { + $region = $this->regionCache[$cacheId]; + } elseif (isset($this->zone)) { + $region = $this->zone; + $this->regionCache[$cacheId] = $region; + } else { + $region = Zone::queryZone($accessKey, $bucket); + if (is_array($region)) { + list($region, $err) = $region; + return array($region, $err); + } + $this->regionCache[$cacheId] = $region; + } + + return array($region, null); + } } diff --git a/src/Qiniu/Region.php b/src/Qiniu/Region.php index eae21d19..9eb9ebda 100644 --- a/src/Qiniu/Region.php +++ b/src/Qiniu/Region.php @@ -107,6 +107,19 @@ public static function regionHuanan() return $regionHuanan; } + //华东2 机房 + public static function regionHuadong2() + { + return new Region( + array('up-cn-east-2.qiniup.com'), + array('upload-cn-east-2.qiniup.com'), + "rs-cn-east-2.qiniuapi.com", + "rsf-cn-east-2.qiniuapi.com", + "api-cn-east-2.qiniuapi.com", + "iovip-cn-east-2.qiniuio.com" + ); + } + //北美机房 public static function regionNorthAmerica() { @@ -137,6 +150,20 @@ public static function regionSingapore() return $regionSingapore; } + //首尔 + public static function regionSeoul() + { + //首尔 + return new Region( + array('up-ap-northeast-1.qiniup.com'), + array('upload-ap-northeast-1.qiniup.com'), + "rs-ap-northeast-1.qiniuapi.com", + "rsf-ap-northeast-1.qiniuapi.com", + "api-ap-northeast-1.qiniuapi.com", + "iovip-ap-northeast-1.qiniuio.com" + ); + } + /* * GET /v2/query?ak=&&bucket= **/ @@ -177,6 +204,10 @@ public static function queryRegion($ak, $bucket) $Region->rsHost = "rs-z2.qbox.me"; $Region->rsfHost = "rsf-z2.qbox.me"; $Region->apiHost = "api-z2.qiniu.com"; + } elseif (strstr($Region->iovipHost, "cn-east-2") !== false) { + $Region->rsHost = "rs-cn-east-2.qiniuapi.com"; + $Region->rsfHost = "rsf-cn-east-2.qiniuapi.com"; + $Region->apiHost = "api-cn-east-2.qiniuapi.com"; } elseif (strstr($Region->iovipHost, "na0") !== false) { $Region->rsHost = "rs-na0.qbox.me"; $Region->rsfHost = "rsf-na0.qbox.me"; @@ -185,6 +216,10 @@ public static function queryRegion($ak, $bucket) $Region->rsHost = "rs-as0.qbox.me"; $Region->rsfHost = "rsf-as0.qbox.me"; $Region->apiHost = "api-as0.qiniu.com"; + } elseif (strstr($Region->iovipHost, "ap-northeast-1") !== false) { + $Region->rsHost = "rs-ap-northeast-1.qiniuapi.com"; + $Region->rsfHost = "rsf-ap-northeast-1.qiniuapi.com"; + $Region->apiHost = "api-ap-northeast-1.qiniuapi.com"; } else { $Region->rsHost = "rs.qbox.me"; $Region->rsfHost = "rsf.qbox.me"; diff --git a/src/Qiniu/Storage/BucketManager.php b/src/Qiniu/Storage/BucketManager.php index 93fa267d..6187d450 100644 --- a/src/Qiniu/Storage/BucketManager.php +++ b/src/Qiniu/Storage/BucketManager.php @@ -39,7 +39,7 @@ public function buckets($shared = true) if ($shared === true) { $includeShared = "true"; } - return $this->rsGet('/buckets?shared=' . $includeShared); + return $this->getV2($this->getUcHost(). '/buckets?shared=' . $includeShared); } /** @@ -71,7 +71,7 @@ public function listbuckets( public function createBucket($name, $region = 'z0') { $path = '/mkbucketv3/' . $name . '/region/' . $region; - return $this->rsPost($path, null); + return $this->postV2($this->getUcHost() . $path, null); } /** @@ -85,7 +85,7 @@ public function createBucket($name, $region = 'z0') public function deleteBucket($name) { $path = '/drop/' . $name; - return $this->rsPost($path, null); + return $this->postV2($this->getUcHost() . $path, null); } /** @@ -96,7 +96,7 @@ public function deleteBucket($name) */ public function domains($bucket) { - return $this->apiGet('/v6/domain/list?tbl=' . $bucket); + return $this->apiGet($bucket, '/v6/domain/list?tbl=' . $bucket); } /** @@ -149,8 +149,7 @@ public function listFiles( \Qiniu\setWithoutEmpty($query, 'marker', $marker); \Qiniu\setWithoutEmpty($query, 'limit', $limit); \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter); - $url = $this->getRsfHost() . '/list?' . http_build_query($query); - return $this->getV2($url); + return $this->rsfGet($bucket, '/list?' . http_build_query($query)); } /** @@ -181,7 +180,14 @@ public function listFilesv2( \Qiniu\setWithoutEmpty($query, 'delimiter', $delimiter); \Qiniu\setWithoutEmpty($query, 'skipconfirm', $skipconfirm); $path = '/v2/list?' . http_build_query($query); - $url = $this->getRsfHost() . $path; + + list($host, $err) = $this->config->getRsfHostV2($this->auth->getAccessKey(), $bucket); + + if ($err != null) { + return array(null, $err); + } + + $url = $host . $path; $headers = $this->auth->authorizationV2($url, 'POST', null, 'application/x-www-form-urlencoded'); $ret = Client::post($url, null, $headers); if (!$ret->ok()) { @@ -572,7 +578,7 @@ public function putBucketMaxAge($bucket, $maxAge) public function putBucketQuota($bucket, $size, $count) { $path = '/setbucketquota/' . $bucket . '/size/' . $size . '/count/' . $count; - return $this->apiPost($path, null); + return $this->apiPost($bucket, $path); } /** @@ -584,7 +590,7 @@ public function putBucketQuota($bucket, $size, $count) public function getBucketQuota($bucket) { $path = '/getbucketquota/' . $bucket; - return $this->apiPost($path, null); + return $this->apiPost($bucket, $path); } /** @@ -599,7 +605,7 @@ public function getBucketQuota($bucket) public function stat($bucket, $key) { $path = '/stat/' . \Qiniu\entry($bucket, $key); - return $this->rsGet($path); + return $this->rsGet($bucket, $path); } /** @@ -614,7 +620,7 @@ public function stat($bucket, $key) public function delete($bucket, $key) { $path = '/delete/' . \Qiniu\entry($bucket, $key); - return $this->rsPost($path); + return $this->rsPost($bucket, $path); } /** @@ -650,7 +656,7 @@ public function copy($from_bucket, $from_key, $to_bucket, $to_key, $force = fals if ($force === true) { $path .= '/force/true'; } - return $this->rsPost($path); + return $this->rsPost($from_bucket, $path); } /** @@ -672,7 +678,7 @@ public function move($from_bucket, $from_key, $to_bucket, $to_key, $force = fals if ($force) { $path .= '/force/true'; } - return $this->rsPost($path); + return $this->rsPost($from_bucket, $path); } /** @@ -690,7 +696,7 @@ public function changeMime($bucket, $key, $mime) $resource = \Qiniu\entry($bucket, $key); $encode_mime = \Qiniu\base64_urlSafeEncode($mime); $path = '/chgm/' . $resource . '/mime/' . $encode_mime; - return $this->rsPost($path); + return $this->rsPost($bucket, $path); } @@ -708,7 +714,7 @@ public function changeType($bucket, $key, $fileType) { $resource = \Qiniu\entry($bucket, $key); $path = '/chtype/' . $resource . '/type/' . $fileType; - return $this->rsPost($path); + return $this->rsPost($bucket, $path); } /** @@ -725,7 +731,7 @@ public function restoreAr($bucket, $key, $freezeAfterDays) { $resource = \Qiniu\entry($bucket, $key); $path = '/restoreAr/' . $resource . '/freezeAfterDays/' . $freezeAfterDays; - return $this->rsPost($path); + return $this->rsPost($bucket, $path); } /** @@ -742,7 +748,7 @@ public function changeStatus($bucket, $key, $status) { $resource = \Qiniu\entry($bucket, $key); $path = '/chstatus/' . $resource . '/status/' . $status; - return $this->rsPost($path); + return $this->rsPost($bucket, $path); } /** @@ -763,9 +769,10 @@ public function fetch($url, $bucket, $key = null) $path = '/fetch/' . $resource . '/to/' . $to; $ak = $this->auth->getAccessKey(); - try { - $ioHost = $this->config->getIovipHost($ak, $bucket); - } catch (\Exception $err) { + + + list($ioHost, $err) = $this->config->getIovipHostV2($ak, $bucket); + if ($err != null) { return array(null, $err); } @@ -820,15 +827,7 @@ public function asynchFetch( \Qiniu\setWithoutEmpty($params, 'ignore_same_key', $ignore_same_key); $data = json_encode($params); - $ak = $this->auth->getAccessKey(); - try { - $apiHost = $this->config->getApiHost($ak, $bucket); - } catch (\Exception $err) { - return array(null, $err); - } - $url = $apiHost . $path; - - return $this->postV2($url, $data); + return $this->apiPost($bucket, $path, $data); } @@ -874,9 +873,9 @@ public function prefetch($bucket, $key) $path = '/prefetch/' . $resource; $ak = $this->auth->getAccessKey(); - try { - $ioHost = $this->config->getIovipHost($ak, $bucket); - } catch (\Exception $err) { + list($ioHost, $err) = $this->config->getIovipHostV2($ak, $bucket); + + if ($err != null) { return array(null, $err); } @@ -902,8 +901,12 @@ public function prefetch($bucket, $key) */ public function batch($operations) { + $scheme = "http://"; + if ($this->config->useHTTPS === true) { + $scheme = "https://"; + } $params = 'op=' . implode('&op=', $operations); - return $this->rsPost('/batch', $params); + return $this->postV2($scheme . Config::RS_HOST . '/batch', $params); } /** @@ -920,61 +923,72 @@ public function deleteAfterDays($bucket, $key, $days) { $entry = \Qiniu\entry($bucket, $key); $path = "/deleteAfterDays/$entry/$days"; - return $this->rsPost($path); + return $this->rsPost($bucket, $path); } - private function getRsfHost() + private function getUcHost() { $scheme = "http://"; if ($this->config->useHTTPS === true) { $scheme = "https://"; } - return $scheme . Config::RSF_HOST; + return $scheme . Config::UC_HOST; } - private function getRsHost() + private function rsfGet($bucket, $path) { - $scheme = "http://"; - if ($this->config->useHTTPS === true) { - $scheme = "https://"; + list($host, $err) = $this->config->getRsfHostV2($this->auth->getAccessKey(), $bucket); + + if ($err != null) { + return array(null, $err); } - return $scheme . Config::RS_HOST; + + return $this->getV2($host . $path); } - private function getApiHost() + private function rsGet($bucket, $path) { - $scheme = "http://"; - if ($this->config->useHTTPS === true) { - $scheme = "https://"; + list($host, $err) = $this->config->getRsHostV2($this->auth->getAccessKey(), $bucket); + + if ($err != null) { + return array(null, $err); } - return $scheme . Config::API_HOST; + + return $this->getV2($host . $path); } - private function getUcHost() + private function rsPost($bucket, $path, $body = null) { - $scheme = "http://"; - if ($this->config->useHTTPS === true) { - $scheme = "https://"; + list($host, $err) = $this->config->getRsHostV2($this->auth->getAccessKey(), $bucket); + + if ($err != null) { + return array(null, $err); } - return $scheme . Config::UC_HOST; - } - private function rsPost($path, $body = null) - { - $url = $this->getRsHost() . $path; - return $this->postV2($url, $body); + return $this->postV2($host . $path, $body); } - private function apiPost($path, $body = null) + private function apiGet($bucket, $path) { - $url = $this->getApiHost() . $path; - return $this->postV2($url, $body); + list($host, $err) = $this->config->getApiHostV2($this->auth->getAccessKey(), $bucket); + + if ($err != null) { + return array(null, $err); + } + + return $this->getV2($host . $path); } - private function ucPost($path, $body = null) + private function apiPost($bucket, $path, $body = null) { - $url = $this->getUcHost() . $path; - return $this->postV2($url, $body); + + list($host, $err) = $this->config->getApiHostV2($this->auth->getAccessKey(), $bucket); + + if ($err != null) { + return array(null, $err); + } + + return $this->postV2($host . $path, $body); } private function ucGet($path) @@ -983,16 +997,10 @@ private function ucGet($path) return $this->getV2($url); } - private function apiGet($path) - { - $url = $this->getApiHost() . $path; - return $this->getV2($url); - } - - private function rsGet($path) + private function ucPost($path, $body = null) { - $url = $this->getRsHost() . $path; - return $this->getV2($url); + $url = $this->getUcHost() . $path; + return $this->postV2($url, $body); } private function getV2($url) diff --git a/src/Qiniu/Storage/FormUploader.php b/src/Qiniu/Storage/FormUploader.php index d907e16f..4750cecb 100644 --- a/src/Qiniu/Storage/FormUploader.php +++ b/src/Qiniu/Storage/FormUploader.php @@ -62,9 +62,8 @@ public static function put( return array(null, $err); } - try { - $upHost = $config->getUpHost($accessKey, $bucket); - } catch (\Exception $err) { + list($upHost, $err) = $config->getUpHostV2($accessKey, $bucket); + if ($err != null) { return array(null, $err); } @@ -135,9 +134,8 @@ public static function putFile( return array(null, $err); } - try { - $upHost = $config->getUpHost($accessKey, $bucket); - } catch (\Exception $err) { + list($upHost, $err) = $config->getUpHostV2($accessKey, $bucket); + if ($err != null) { return array(null, $err); } diff --git a/src/Qiniu/Storage/ResumeUploader.php b/src/Qiniu/Storage/ResumeUploader.php index a983d38d..097c836a 100644 --- a/src/Qiniu/Storage/ResumeUploader.php +++ b/src/Qiniu/Storage/ResumeUploader.php @@ -98,7 +98,7 @@ public function __construct( return array(null, $err); } - $upHost = $config->getUpHost($accessKey, $bucket); + list($upHost, $err) = $config->getUpHostV2($accessKey, $bucket); if ($err != null) { throw new \Exception($err->message(), 1); } @@ -210,7 +210,10 @@ public function upload($fname) if ($err != null) { return array(null, $err); } - $upHostBackup = $this->config->getUpBackupHost($accessKey, $bucket); + list($upHostBackup, $err) = $this->config->getUpBackupHostV2($accessKey, $bucket); + if ($err != null) { + return array(null, $err); + } $this->host = $upHostBackup; } diff --git a/src/Qiniu/Zone.php b/src/Qiniu/Zone.php index 7af0e3f5..1d7cda2f 100644 --- a/src/Qiniu/Zone.php +++ b/src/Qiniu/Zone.php @@ -20,6 +20,11 @@ public static function zonez2() return parent::regionHuanan(); } + public static function zoneCnEast2() + { + return parent::regionHuadong2(); + } + public static function zoneAs0() { return parent::regionSingapore(); @@ -30,6 +35,11 @@ public static function zoneNa0() return parent::regionNorthAmerica(); } + public static function zoneApNortheast1() + { + return parent::regionSeoul(); + } + public static function qvmZonez0() { return parent::qvmRegionHuadong(); diff --git a/tests/Qiniu/Tests/ConfigTest.php b/tests/Qiniu/Tests/ConfigTest.php index 9f7a7ac8..9d19a0bd 100644 --- a/tests/Qiniu/Tests/ConfigTest.php +++ b/tests/Qiniu/Tests/ConfigTest.php @@ -41,5 +41,22 @@ public function testGetApiHostErrored() } $this->assertTrue($hasException); } + + public function testGetApiHostV2() + { + $conf = new Config(); + list($apiHost, $err) = $conf->getApiHostV2($this->accessKey, $this->bucketName); + $this->assertNull($err); + $this->assertEquals('http://api.qiniu.com', $apiHost); + } + + public function testGetApiHostV2Errored() + { + $conf = new Config(); + list($apiHost, $err) = $conf->getApiHostV2($this->accessKey, "fakebucket"); + $this->assertNotNull($err->code()); + $this->assertEquals(631, $err->code()); + $this->assertNull($apiHost); + } } } diff --git a/tests/Qiniu/Tests/ResumeUpTest.php b/tests/Qiniu/Tests/ResumeUpTest.php index 989786a4..ac4bcaca 100755 --- a/tests/Qiniu/Tests/ResumeUpTest.php +++ b/tests/Qiniu/Tests/ResumeUpTest.php @@ -68,8 +68,7 @@ public function test4ML() public function test4ML2() { $key = 'resumePutFile4ML_'.rand(); - $zone = new Zone(array('upload.fake.qiniu.com'), array('upload.qiniup.com')); - $cfg = new Config($zone); + $cfg = new Config(); $upManager = new UploadManager($cfg); $token = $this->auth->uploadToken($this->bucketName, $key); $tempFile = qiniuTempFile(4 * 1024 * 1024 + 10); @@ -180,8 +179,7 @@ public function testResumeUploadWithParams() public function testResumeUploadV2() { - $zone = new Zone(array('up.qiniup.com')); - $cfg = new Config($zone); + $cfg = new Config(); $upManager = new UploadManager($cfg); $testFileSize = array( config::BLOCK_SIZE / 2, @@ -260,8 +258,7 @@ public function testResumeUploadV2WithParams() // but not match the test style of this project public function testResumeUploadWithInvalidVersion() { - $zone = new Zone(array('up.qiniup.com')); - $cfg = new Config($zone); + $cfg = new Config(); $upManager = new UploadManager($cfg); $testFileSize = config::BLOCK_SIZE * 2; $partSize = 5 * 1024 * 1024; diff --git a/tests/Qiniu/Tests/ZoneTest.php b/tests/Qiniu/Tests/ZoneTest.php index cc5cce50..56190ac0 100755 --- a/tests/Qiniu/Tests/ZoneTest.php +++ b/tests/Qiniu/Tests/ZoneTest.php @@ -99,6 +99,12 @@ public function testZonez2() $this->assertContains('upload-z2.qiniup.com', $zone->cdnUpHosts); } + public function testZoneCnEast2() + { + $zone = Zone::zoneCnEast2(); + $this->assertContains('upload-cn-east-2.qiniup.com', $zone->cdnUpHosts); + } + public function testZoneNa0() { $zone = Zone::zoneNa0(); @@ -111,6 +117,12 @@ public function testZoneAs0() $this->assertContains('upload-as0.qiniup.com', $zone->cdnUpHosts); } + public function testZoneApNortheast1() + { + $zone = Zone::zoneApNortheast1(); + $this->assertContains('upload-ap-northeast-1.qiniup.com', $zone->cdnUpHosts); + } + public function testQvmZonez0() { $zone = Zone::qvmZonez0();