Skip to content

Commit 8e17714

Browse files
committed
multi zone support
1 parent d1bca45 commit 8e17714

File tree

6 files changed

+125
-29
lines changed

6 files changed

+125
-29
lines changed

src/Qiniu/Auth.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Qiniu;
33

44
use Qiniu;
5+
use Qiniu\Zone;
56

67
final class Auth
78
{
@@ -82,6 +83,9 @@ public function uploadToken(
8283
$args = self::copyPolicy($args, $policy, $strictPolicy);
8384
$args['scope'] = $scope;
8485
$args['deadline'] = $deadline;
86+
87+
$zone = new Zone();
88+
$args['upHosts'] = $zone->getUpHosts($this->accessKey, $bucket);
8589
$b = json_encode($args);
8690
return $this->signWithData($b);
8791
}
@@ -114,6 +118,8 @@ public function uploadToken(
114118
'persistentPipeline',
115119

116120
'deleteAfterDays',
121+
122+
'upHosts',
117123
);
118124

119125
private static $deprecatedPolicyFields = array(

src/Qiniu/Config.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
<?php
22
namespace Qiniu;
33

4+
use Qiniu\Zone;
5+
46
final class Config
57
{
68
const SDK_VER = '7.0.8';
79

810
const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改
911

10-
const IO_HOST = 'http://iovip.qbox.me'; // 七牛源站Host
1112
const RS_HOST = 'http://rs.qbox.me'; // 文件元信息管理操作Host
1213
const RSF_HOST = 'http://rsf.qbox.me'; // 列举操作Host
1314
const API_HOST = 'http://api.qiniu.com'; // 数据处理操作Host
15+
const UC_HOST = 'http://uc.qbox.me'; // Host
1416

15-
private $upHost; // 上传Host
16-
private $upHostBackup; // 上传备用Host
17+
public $zone;
1718

1819
public function __construct(Zone $z = null) // 构造函数,默认为zone0
1920
{
2021
if ($z === null) {
21-
$z = Zone::zone0();
22+
$this->zone = new Zone();
2223
}
23-
$this->upHost = $z->upHost;
24-
$this->upHostBackup = $z->upHostBackup;
25-
}
26-
27-
public function getUpHost()
28-
{
29-
return $this->upHost;
30-
}
31-
32-
public function getUpHostBackup()
33-
{
34-
return $this->upHostBackup;
3524
}
3625
}

src/Qiniu/Storage/FormUploader.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ public static function put(
4949
}
5050
}
5151

52-
$response = Client::multipartPost($config->getUpHost(), $fields, 'file', $fname, $data, $mime);
52+
$upHost = $config->zone->getUpHostByToken($upToken);
53+
$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
5354
if (!$response->ok()) {
54-
return array(null, new Error($config->getUpHost(), $response));
55+
return array(null, new Error($upHost, $response));
5556
}
5657
return array($response->json(), null);
5758
}
@@ -97,9 +98,11 @@ public static function putFile(
9798
}
9899
$fields['key'] = $key;
99100
$headers =array('Content-Type' => 'multipart/form-data');
100-
$response = client::post($config->getUpHost(), $fields, $headers);
101+
102+
$upHost = $config->zone->getBucketHostsByUpToken($upToken);
103+
$response = client::post($upHost, $fields, $headers);
101104
if (!$response->ok()) {
102-
return array(null, new Error($config->getUpHost(), $response));
105+
return array(null, new Error($upHost, $response));
103106
}
104107
return array($response->json(), null);
105108
}

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(
5454
$this->mime = $mime;
5555
$this->contexts = array();
5656
$this->config = $config;
57-
$this->host = $config->getUpHost();
57+
$this->host = $config->zone::getUpHostByToken($upToken);
5858
}
5959

6060
/**
@@ -76,7 +76,7 @@ public function upload()
7676
$ret = $response->json();
7777
}
7878
if ($response->statusCode < 0) {
79-
$this->host = $this->config->getUpHostBackup();
79+
$this->host = $this->config->zone::getBackupUpHostByToken($this->upToken);
8080
}
8181
if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
8282
$response = $this->makeBlock($data, $blockSize);

src/Qiniu/Storage/UploadManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Qiniu\Storage;
33

44
use Qiniu\Config;
5+
use Qiniu\Zone;
56
use Qiniu\Http\HttpClient;
67
use Qiniu\Storage\ResumeUploader;
78
use Qiniu\Storage\FormUploader;

src/Qiniu/Zone.php

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,121 @@
11
<?php
22
namespace Qiniu;
33

4+
use Qiniu\Http\Client;
5+
use Qiniu\Http\Error;
6+
47
final class Zone
58
{
9+
public $ioHost; // 七牛源站Host
610
public $upHost;
711
public $upHostBackup;
812

9-
public function __construct($upHost, $upHostBackup)
13+
public $hostCache; //<scheme>:<ak>:<bucket> ==> array('deadline' => 'xxx', 'upHosts' => array(), 'ioHost' => 'xxx.com')
14+
public $scheme = 'http';
15+
16+
public function __construct($scheme = null)
1017
{
11-
$this->upHost = $upHost;
12-
$this->upHostBackup = $upHostBackup;
18+
$this->hostCache = array();
19+
if ($scheme != null)
20+
{
21+
$this->scheme = $scheme;
22+
}
1323
}
1424

15-
public static function zone0()
25+
public function getUpHostByToken($uptoken)
1626
{
17-
return new self('http://up.qiniu.com', 'http://upload.qiniu.com');
27+
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
28+
$upHosts = $this->getUpHosts($ak, $bucket);
29+
return $upHosts[0];
1830
}
1931

20-
public static function zone1()
32+
public function getBackupUpHostByToken($uptoken)
33+
{
34+
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
35+
$upHosts = $this->getUpHosts($ak, $bucket);
36+
return $upHosts[1];
37+
}
38+
39+
public function getUpHosts($ak, $bucket)
40+
{
41+
$bucketHosts = $this->getBucketHosts($ak, $bucket);
42+
$upHosts = $bucketHosts['upHosts'];
43+
return $upHosts;
44+
}
45+
46+
public function getBucketHostsByUpToken($uptoken)
47+
{
48+
list($ak, $bucket) = $this->unmarshalUpToken($uptoken);
49+
return $this->getBucketHosts($ak, $bucket);
50+
}
51+
52+
private function unmarshalUpToken($uptoken)
53+
{
54+
$token = split(':', $uptoken);
55+
if (count($token) !== 3)
56+
{
57+
throw new \Exception("Invalid Uptoken", 1);
58+
}
59+
60+
$ak = $token[0];
61+
$policy = base64_urlSafeDecode($token[2]);
62+
$policy = json_decode($policy, true);
63+
64+
list($bucket, $_) = split(':', $policy['scope']);
65+
66+
return array($ak, $bucket);
67+
}
68+
69+
public function getBucketHosts($ak, $bucket)
70+
{
71+
$key = $ak . $bucket;
72+
73+
$exist = false;
74+
if (count($this->hostCache) > 0)
75+
{
76+
$exist = array_key_exists($key, $this->hostCache) && $this->hostCache[$key]['deadline'] > time();
77+
}
78+
79+
if ($exist)
80+
{
81+
return $this->hostCache[$key];
82+
}
83+
84+
list($hosts, $_) = $this->bucketHosts($ak, $bucket);
85+
86+
var_dump($hosts);
87+
$schemeHosts = $hosts[$this->scheme];
88+
$bucketHosts = array('upHosts' => $schemeHosts['up'], 'ioHost' => $schemeHosts['io'], 'deadline' => time() + $hosts['ttl']);
89+
90+
$this->hostCache[$key] = $bucketHosts;
91+
return $bucketHosts;
92+
}
93+
94+
95+
/* 请求包:
96+
* GET /v1/query?ak=<ak>&&bucket=<bucket>
97+
* 返回包:
98+
*
99+
* 200 OK {
100+
* "ttl": <ttl>, // 有效时间
101+
* "http": {
102+
* "up": [],
103+
* "io": [], // 当bucket为global时,我们不需要iohost, io缺省
104+
* },
105+
* "https": {
106+
* "up": [],
107+
* "io": [], // 当bucket为global时,我们不需要iohost, io缺省
108+
* }
109+
* }
110+
**/
111+
private function bucketHosts($ak, $bucket)
21112
{
22-
return new self('http://up-z1.qiniu.com', 'http://upload-z1.qiniu.com');
113+
$path = '/v1/query' . "?ak=$ak&bucket=$bucket";
114+
$ret = Client::Get(Config::UC_HOST . $path);
115+
if (!$ret->ok()) {
116+
return array(null, new Error($url, $ret));
117+
}
118+
$r = ($ret->body === null) ? array() : $ret->json();
119+
return array($r, null);
23120
}
24121
}

0 commit comments

Comments
 (0)