diff --git a/src/Qiniu/Storage/FormUploader.php b/src/Qiniu/Storage/FormUploader.php index e3a314c1..2e1cae82 100644 --- a/src/Qiniu/Storage/FormUploader.php +++ b/src/Qiniu/Storage/FormUploader.php @@ -49,7 +49,11 @@ public static function put( } } - $upHost = $config->zone->getUpHostByToken($upToken); + list($upHost, $err) = $config->zone->getUpHostByToken($upToken); + if ($err != null) { + return array(null, $err); + } + $response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime); if (!$response->ok()) { return array(null, new Error($upHost, $response)); @@ -99,7 +103,11 @@ public static function putFile( $fields['key'] = $key; $headers =array('Content-Type' => 'multipart/form-data'); - $upHost = $config->zone->getUpHostByToken($upToken); + list($upHost, $err) = $config->zone->getUpHostByToken($upToken); + if ($err != null) { + return array(null, $err); + } + $response = client::post($upHost, $fields, $headers); if (!$response->ok()) { return array(null, new Error($upHost, $response)); diff --git a/src/Qiniu/Storage/ResumeUploader.php b/src/Qiniu/Storage/ResumeUploader.php index 34160d0e..382f21c6 100644 --- a/src/Qiniu/Storage/ResumeUploader.php +++ b/src/Qiniu/Storage/ResumeUploader.php @@ -54,7 +54,12 @@ public function __construct( $this->mime = $mime; $this->contexts = array(); $this->config = $config; - $this->host = $config->zone->getUpHostByToken($upToken); + + list($upHost, $err) = $config->zone->getUpHostByToken($upToken); + if ($err != null) { + throw new \Exception($err, 1); + } + $this->host = $upHost; } /** @@ -76,7 +81,11 @@ public function upload() $ret = $response->json(); } if ($response->statusCode < 0) { - $this->host = $this->config->zone->getBackupUpHostByToken($this->upToken); + list($bakHost, $err) = $this->config->zone->getBackupUpHostByToken($this->upToken); + if ($err != null) { + return array(null, $err); + } + $this->host = $bakHost; } if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) { $response = $this->makeBlock($data, $blockSize); diff --git a/src/Qiniu/Zone.php b/src/Qiniu/Zone.php index c9829c96..3f58430b 100644 --- a/src/Qiniu/Zone.php +++ b/src/Qiniu/Zone.php @@ -28,17 +28,17 @@ public function __construct($scheme = null) public function getUpHostByToken($uptoken) { list($ak, $bucket) = $this->unmarshalUpToken($uptoken); - list($upHosts,) = $this->getUpHosts($ak, $bucket); - return $upHosts[0]; + list($upHosts, $err) = $this->getUpHosts($ak, $bucket); + return array($upHosts[0], $err); } public function getBackupUpHostByToken($uptoken) { list($ak, $bucket) = $this->unmarshalUpToken($uptoken); - list($upHosts,) = $this->getUpHosts($ak, $bucket); + list($upHosts, $err) = $this->getUpHosts($ak, $bucket); $upHost = isset($upHosts[1]) ? $upHosts[1] : $upHosts[0]; - return $upHost; + return array($upHost, $err); } public function getIoHost($ak, $bucket) diff --git a/tests/Qiniu/Tests/ZoneTest.php b/tests/Qiniu/Tests/ZoneTest.php index d690c893..b633b9a3 100644 --- a/tests/Qiniu/Tests/ZoneTest.php +++ b/tests/Qiniu/Tests/ZoneTest.php @@ -12,6 +12,7 @@ class ZoneTest extends \PHPUnit_Framework_TestCase protected $bucketName; protected $bucketNameBC; + protected $bucketNameNA; protected function setUp() @@ -22,6 +23,9 @@ protected function setUp() global $bucketNameBC; $this->bucketNameBC = $bucketNameBC; + global $bucketNameNA; + $this->bucketNameNA = $bucketNameNA; + global $accessKey; $this->ak = $accessKey; @@ -32,23 +36,37 @@ protected function setUp() public function testUpHosts() { + // test nb http list($upHosts, $err) = $this->zone->getUpHosts($this->ak, $this->bucketName); $this->assertNull($err); $this->assertEquals('http://up.qiniu.com', $upHosts[0]); $this->assertEquals('http://upload.qiniu.com', $upHosts[1]); + // test bc http list($upHosts, $err) = $this->zone->getUpHosts($this->ak, $this->bucketNameBC); $this->assertNull($err); $this->assertEquals('http://up-z1.qiniu.com', $upHosts[0]); $this->assertEquals('http://upload-z1.qiniu.com', $upHosts[1]); + // test na http + list($upHosts, $err) = $this->zone->getUpHosts($this->ak, $this->bucketNameNA); + $this->assertNull($err); + $this->assertEquals('http://up-na0.qiniu.com', $upHosts[0]); + + // test nb https list($upHosts, $err) = $this->zoneHttps->getUpHosts($this->ak, $this->bucketName); $this->assertNull($err); $this->assertEquals('https://up.qbox.me', $upHosts[0]); + // test bc https list($upHosts, $err) = $this->zoneHttps->getUpHosts($this->ak, $this->bucketNameBC); $this->assertNull($err); $this->assertEquals('https://up-z1.qbox.me', $upHosts[0]); + + // test na https + list($upHosts, $err) = $this->zoneHttps->getUpHosts($this->ak, $this->bucketNameNA); + $this->assertNull($err); + $this->assertEquals('https://up-na0.qbox.me', $upHosts[0]); } public function testUpHostByToken() @@ -58,36 +76,52 @@ public function testUpHostByToken() 3RzIjpbImh0dHA6XC9cL3VwLXoxLnFpbml1LmNvbSIsImh0dHA6XC9cL3VwbG9hZC16MS5xaW5p dS5jb20iLCItSCB1cC16MS5xaW5pdS5jb20gaHR0cDpcL1wvMTA2LjM4LjIyNy4yNyJdfQ=='; - $upHost = $this->zone->getUpHostByToken($uptoken_bc); + list($upHost, $err) = $this->zone->getUpHostByToken($uptoken_bc); $this->assertEquals('http://up-z1.qiniu.com', $upHost); + $this->assertEquals(null, $err); - $upHostBackup = $this->zone->getBackupUpHostByToken($uptoken_bc); + list($upHostBackup, $err) = $this->zone->getBackupUpHostByToken($uptoken_bc); $this->assertEquals('http://upload-z1.qiniu.com', $upHostBackup); + $this->assertEquals(null, $err); $uptoken_bc_https = 'QWYn5TFQsLLU1pL5MFEmX3s5DmHdUThav9WyOWOm:7I47O-vFcN5TKO 6D7cobHPVkyIA=:eyJzY29wZSI6InBocHNkay1iYyIsImRlYWRsaW5lIjoxNDcwNzIyNzQ1LCJ1c Ehvc3RzIjpbImh0dHBzOlwvXC91cC16MS5xYm94Lm1lIl19'; - $upHost = $this->zoneHttps->getUpHostByToken($uptoken_bc_https); + list($upHost, $err) = $this->zoneHttps->getUpHostByToken($uptoken_bc_https); $this->assertEquals('https://up-z1.qbox.me', $upHost); + $this->assertEquals(null, $err); - $upHostBackup = $this->zoneHttps->getBackupUpHostByToken($uptoken_bc_https); + list($upHostBackup, $err) = $this->zoneHttps->getBackupUpHostByToken($uptoken_bc_https); $this->assertEquals('https://up-z1.qbox.me', $upHostBackup); + $this->assertEquals(null, $err); } public function testIoHosts() { + // test nb http $ioHost = $this->zone->getIoHost($this->ak, $this->bucketName); $this->assertEquals('http://iovip.qbox.me', $ioHost); + // test bc http $ioHost = $this->zone->getIoHost($this->ak, $this->bucketNameBC); $this->assertEquals('http://iovip-z1.qbox.me', $ioHost); + // test na http + $ioHost = $this->zone->getIoHost($this->ak, $this->bucketNameNA); + $this->assertEquals('http://iovip-na0.qbox.me', $ioHost); + + // test nb https $ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketName); $this->assertEquals('https://iovip.qbox.me', $ioHost); + // test bc https $ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketNameBC); $this->assertEquals('https://iovip-z1.qbox.me', $ioHost); + + // test na https + $ioHost = $this->zoneHttps->getIoHost($this->ak, $this->bucketNameNA); + $this->assertEquals('https://iovip-na0.qbox.me', $ioHost); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index bed9e873..57a53d13 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -10,6 +10,7 @@ $bucketName = 'phpsdk'; $key = 'php-logo.png'; $bucketNameBC = 'phpsdk-bc'; +$bucketNameNA = 'phpsdk-na'; $dummyAccessKey = 'abcdefghklmnopq'; $dummySecretKey = '1234567890';