Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Qiniu/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ private function getRegion($accessKey, $bucket)
$this->regionCache[$cacheId] = $region;
} else {
$region = Zone::queryZone($accessKey, $bucket);
if (is_array($region)) {
list($region, $err) = $region;
if ($err != null) {
throw new \Exception($err->message());
}
}
$this->regionCache[$cacheId] = $region;
}
return $region;
Expand Down
18 changes: 15 additions & 3 deletions src/Qiniu/Storage/BucketManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,11 @@ public function fetch($url, $bucket, $key = null)
$path = '/fetch/' . $resource . '/to/' . $to;

$ak = $this->auth->getAccessKey();
$ioHost = $this->config->getIovipHost($ak, $bucket);
try {
$ioHost = $this->config->getIovipHost($ak, $bucket);
} catch (\Exception $err) {
return array(null, $err);
}

$url = $ioHost . $path;
return $this->post($url, null);
Expand Down Expand Up @@ -776,7 +780,11 @@ public function asynchFetch(
$data = json_encode($params);

$ak = $this->auth->getAccessKey();
$apiHost = $this->config->getApiHost($ak, $bucket);
try {
$apiHost = $this->config->getApiHost($ak, $bucket);
} catch (\Exception $err) {
return array(null, $err);
}
$url = $apiHost . $path;

return $this->postV2($url, $data);
Expand Down Expand Up @@ -826,7 +834,11 @@ public function prefetch($bucket, $key)
$path = '/prefetch/' . $resource;

$ak = $this->auth->getAccessKey();
$ioHost = $this->config->getIovipHost($ak, $bucket);
try {
$ioHost = $this->config->getIovipHost($ak, $bucket);
} catch (\Exception $err) {
return array(null, $err);
}

$url = $ioHost . $path;
return $this->post($url, null);
Expand Down
17 changes: 13 additions & 4 deletions src/Qiniu/Storage/FormUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Qiniu\Storage;

use Qiniu\Config;
use Qiniu\Http\Error;
use Qiniu\Http\Client;

Expand All @@ -14,7 +15,7 @@ final class FormUploader
* @param string $upToken 上传凭证
* @param string $key 上传文件名
* @param string $data 上传二进制流
* @param string $config 上传配置
* @param Config $config 上传配置
* @param string $params 自定义变量,规格参考
* https://developer.qiniu.com/kodo/manual/1235/vars#xvar
* @param string $mime 上传数据的mimeType
Expand Down Expand Up @@ -56,7 +57,11 @@ public static function put(
return array(null, $err);
}

$upHost = $config->getUpHost($accessKey, $bucket);
try {
$upHost = $config->getUpHost($accessKey, $bucket);
} catch (\Exception $err) {
return array(null, $err);
}

$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
if (!$response->ok()) {
Expand All @@ -71,7 +76,7 @@ public static function put(
* @param string $upToken 上传凭证
* @param string $key 上传文件名
* @param string $filePath 上传文件的路径
* @param string $config 上传配置
* @param Config $config 上传配置
* @param string $params 自定义变量,规格参考
* https://developer.qiniu.com/kodo/manual/1235/vars#xvar
* @param string $mime 上传数据的mimeType
Expand Down Expand Up @@ -112,7 +117,11 @@ public static function putFile(
return array(null, $err);
}

$upHost = $config->getUpHost($accessKey, $bucket);
try {
$upHost = $config->getUpHost($accessKey, $bucket);
} catch (\Exception $err) {
return array(null, $err);
}

$response = Client::post($upHost, $fields, $headers);
if (!$response->ok()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Qiniu/Storage/ResumeUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class ResumeUploader
* @param string $size 上传流的大小
* @param string $params 自定义变量
* @param string $mime 上传数据的mimeType
* @param string $config
* @param Config $config
* @param string $resumeRecordFile 断点续传的已上传的部分信息记录文件
* @param string $version 分片上传版本 目前支持v1/v2版本 默认v1
* @param string $partSize 分片上传v2字段 默认大小为4MB 分片大小范围为1 MB - 1 GB
Expand Down
30 changes: 30 additions & 0 deletions tests/Qiniu/Tests/BucketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ public function testPrefetch()
$this->assertNull($error);
}

public function testPrefetchFailed()
{
list($ret, $error) = $this->bucketManager->prefetch(
'fakebucket',
'php-sdk.html'
);
$this->assertNull($ret);
$this->assertNotNull($error);
}

public function testFetch()
{
list($ret, $error) = $this->bucketManager->fetch(
Expand All @@ -297,6 +307,16 @@ public function testFetch()
$this->assertNull($error);
}

public function testFetchFailed()
{
list($ret, $error) = $this->bucketManager->fetch(
'http://developer.qiniu.com/docs/v6/sdk/php-sdk.html',
'fakebucket'
);
$this->assertNull($ret);
$this->assertNotNull($error);
}

public function testAsynchFetch()
{
list($ret, $error) = $this->bucketManager->asynchFetch(
Expand Down Expand Up @@ -325,6 +345,16 @@ public function testAsynchFetch()
$this->assertNull($error);
}

public function testAsynchFetchFailed()
{
list($ret, $error) = $this->bucketManager->asynchFetch(
'http://devtools.qiniu.com/qiniu.png',
'fakebucket'
);
$this->assertNull($ret);
$this->assertNotNull($error);
}


public function testBatchCopy()
{
Expand Down
45 changes: 45 additions & 0 deletions tests/Qiniu/Tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Qiniu\Tests {
use Qiniu\Config;

class ConfigTest extends \PHPUnit_Framework_TestCase
{
protected $accessKey;
protected $bucketName;

protected function setUp()
{
global $accessKey;
$this->accessKey = $accessKey;
global $bucketName;
$this->bucketName = $bucketName;
}

public function testGetApiHost()
{
$conf = new Config();
$hasException = false;
$apiHost = '';
try {
$apiHost = $conf->getApiHost($this->accessKey, $this->bucketName);
} catch (\Exception $e) {
$hasException = true;
}
$this->assertFalse($hasException);
$this->assertEquals('http://api.qiniu.com', $apiHost);
}

public function testGetApiHostErrored()
{
$conf = new Config();
$hasException = false;
try {
$conf->getApiHost($this->accessKey, "fakebucket");
} catch (\Exception $e) {
$hasException = true;
}
$this->assertTrue($hasException);
}
}
}
17 changes: 17 additions & 0 deletions tests/Qiniu/Tests/FormUpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function testData2()
$this->assertNotNull($ret['hash']);
}

public function testDataFailed()
{
$token = $this->auth->uploadToken('fakebucket');
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', null);
$this->assertNull($ret);
$this->assertNotNull($error);
}

public function testFile()
{
$key = 'formPutFile';
Expand All @@ -56,4 +64,13 @@ public function testFile2()
$this->assertNull($error);
$this->assertNotNull($ret['hash']);
}

public function testFileFailed()
{
$key = 'fakekey';
$token = $this->auth->uploadToken('fakebucket', $key);
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', null);
$this->assertNull($ret);
$this->assertNotNull($error);
}
}