Skip to content

Commit 1b3c4b7

Browse files
committed
Merge pull request #130 from rwifeng/multi_zone
Multi zone
2 parents ca1d6ac + e5bf645 commit 1b3c4b7

File tree

7 files changed

+65
-19
lines changed

7 files changed

+65
-19
lines changed

Zone.php

Whitespace-only changes.

src/Qiniu/Config.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ final class Config
55
{
66
const SDK_VER = '7.0.2';
77

8+
const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,暂不支持修改
9+
810
const IO_HOST = 'http://iovip.qbox.me'; // 七牛源站Host
911
const RS_HOST = 'http://rs.qbox.me'; // 文件元信息管理操作Host
1012
const RSF_HOST = 'http://rsf.qbox.me'; // 列举操作Host
1113
const API_HOST = 'http://api.qiniu.com'; // 数据处理操作Host
1214

13-
const UPAUTO_HOST = 'http://up.qiniu.com'; // 默认上传Host
14-
const UPDX_HOST = 'http://updx.qiniu.com'; // 电信上传Host
15-
const UPLT_HOST = 'http://uplt.qiniu.com'; // 联通上传Host
16-
const UPYD_HOST = 'http://upyd.qiniu.com'; // 移动上传Host
17-
const UPBACKUP_HOST = 'http://upload.qiniu.com'; // 备用上传Host
18-
19-
const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,暂不支持修改
15+
public static $upHost; // 上传Host
16+
public static $upHostBackup; // 上传备用Host
2017

21-
public static $defaultHost = self::UPAUTO_HOST; // 设置为默认上传Host
18+
public function __construct() // 构造函数,默认为zone0
19+
{
20+
self::setZone(Zone::zone0());
21+
}
22+
23+
public static function setZone(Zone $z)
24+
{
25+
self::$upHost = $z->upHost;
26+
self::$upHostBackup = $z->upHostBackup;
27+
}
2228
}

src/Qiniu/Storage/FormUploader.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static function put(
1111
$upToken,
1212
$key,
1313
$data,
14+
$config,
1415
$params,
1516
$mime,
1617
$checkCrc
@@ -31,9 +32,9 @@ public static function put(
3132
}
3233
}
3334

34-
$response = Client::multipartPost(Config::$defaultHost, $fields, 'file', $fname, $data, $mime);
35+
$response = Client::multipartPost($config::$upHost, $fields, 'file', $fname, $data, $mime);
3536
if (!$response->ok()) {
36-
return array(null, new Error(Config::$defaultHost, $response));
37+
return array(null, new Error($config::$upHost, $response));
3738
}
3839
return array($response->json(), null);
3940
}
@@ -42,6 +43,7 @@ public static function putFile(
4243
$upToken,
4344
$key,
4445
$filePath,
46+
$config,
4547
$params,
4648
$mime,
4749
$checkCrc
@@ -63,9 +65,9 @@ public static function putFile(
6365
}
6466
}
6567
$headers =array('Content-Type' => 'multipart/form-data');
66-
$response = client::post(Config::$defaultHost, $fields, $headers);
68+
$response = client::post($config::$upHost, $fields, $headers);
6769
if (!$response->ok()) {
68-
return array(null, new Error(Config::$defaultHost, $response));
70+
return array(null, new Error($config::$upHost, $response));
6971
}
7072
return array($response->json(), null);
7173
}

src/Qiniu/Storage/ResumeUploader.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ public function __construct(
4343
$inputStream,
4444
$size,
4545
$params,
46-
$mime
46+
$mime,
47+
$config
4748
) {
4849
$this->upToken = $upToken;
4950
$this->key = $key;
5051
$this->inputStream = $inputStream;
5152
$this->size = $size;
5253
$this->params = $params;
5354
$this->mime = $mime;
54-
$this->host = Config::$defaultHost;
55+
$this->host = $config::$upHost;
5556
$this->contexts = array();
5657
}
5758

@@ -76,7 +77,7 @@ public function upload()
7677
$ret = $response->json();
7778
}
7879
if ($response->statusCode < 0) {
79-
$this->host = Config::UPBACKUP_HOST;
80+
$this->host = $config::$upHostBackup;
8081
}
8182
if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
8283
$response = $this->makeBlock($data, $blockSize);

src/Qiniu/Storage/UploadManager.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313
*/
1414
final class UploadManager
1515
{
16-
public function __construct()
16+
public $config;
17+
18+
public function __construct($config = null)
1719
{
20+
if ($config === null) {
21+
$config = new Config();
22+
}
23+
$this->config = $config;
1824
}
1925

2026
/**
@@ -47,6 +53,7 @@ public function put(
4753
$upToken,
4854
$key,
4955
$data,
56+
$this->config,
5057
$params,
5158
$mime,
5259
$checkCrc
@@ -96,6 +103,7 @@ public function putFile(
96103
$upToken,
97104
$key,
98105
$data,
106+
$this->config,
99107
$params,
100108
$mime,
101109
$checkCrc
@@ -108,7 +116,7 @@ public function putFile(
108116
$size,
109117
$params,
110118
$mime,
111-
$checkCrc
119+
$this->config
112120
);
113121
return $up->upload();
114122
}

src/Qiniu/Zone.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
namespace Qiniu;
3+
4+
final class Zone
5+
{
6+
public $upHost;
7+
public $upHostBackup;
8+
9+
public function __construct($upHost, $upHostBackup)
10+
{
11+
$this->upHost = $upHost;
12+
$this->upHostBackup = $upHostBackup;
13+
}
14+
15+
public static function zone0()
16+
{
17+
return new self('http://up.qiniu.com', 'http://upload.qiniu.com');
18+
}
19+
20+
public static function zone1()
21+
{
22+
return new self('http://up-z1.qiniu.com', 'http://upload-z1.qiniu.com');
23+
}
24+
}

tests/Qiniu/Tests/FormUpTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33

44
use Qiniu\Storage\FormUploader;
55
use Qiniu\Storage\UploadManager;
6+
use Qiniu\Config;
67

78
class FormUpTest extends \PHPUnit_Framework_TestCase
89
{
910
protected $bucketName;
1011
protected $auth;
12+
protected $cfg;
13+
1114
protected function setUp()
1215
{
1316
global $bucketName;
1417
$this->bucketName = $bucketName;
1518

1619
global $testAuth;
1720
$this->auth = $testAuth;
21+
$this->cfg = new Config();
1822
}
23+
1924
public function testData()
2025
{
2126
$token = $this->auth->uploadToken($this->bucketName);
22-
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', null, 'text/plain', true);
27+
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->cfg, null, 'text/plain', true);
2328
$this->assertNull($error);
2429
$this->assertNotNull($ret['hash']);
2530
}
@@ -37,7 +42,7 @@ public function testFile()
3742
{
3843
$key = 'formPutFile';
3944
$token = $this->auth->uploadToken($this->bucketName, $key);
40-
list($ret, $error) = FormUploader::putFile($token, $key, __file__, null, 'text/plain', true);
45+
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->cfg, null, 'text/plain', true);
4146
$this->assertNull($error);
4247
$this->assertNotNull($ret['hash']);
4348
}

0 commit comments

Comments
 (0)