1
|
|
|
<?php |
2
|
|
|
namespace Qiniu; |
3
|
|
|
|
4
|
|
|
use Qiniu\Http\Client; |
5
|
|
|
use Qiniu\Http\Error; |
6
|
|
|
|
7
|
|
|
final class Zone |
8
|
|
|
{ |
9
|
18 |
|
public $ioHost; // 七牛源站Host |
10
|
|
|
public $upHost; |
11
|
18 |
|
public $upHostBackup; |
12
|
18 |
|
|
13
|
18 |
|
public $hostCache; //<scheme>:<ak>:<bucket> ==> array('deadline' => 'xxx', 'upHosts' => array(), 'ioHost' => 'xxx.com') |
14
|
|
|
public $scheme = 'http'; |
15
|
15 |
|
|
16
|
|
|
public function __construct($scheme = null) |
17
|
15 |
|
{ |
18
|
|
|
$this->hostCache = array(); |
19
|
|
|
if ($scheme != null) |
20
|
|
|
{ |
21
|
|
|
$this->scheme = $scheme; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getUpHostByToken($uptoken) |
26
|
|
|
{ |
27
|
|
|
list($ak, $bucket) = $this->unmarshalUpToken($uptoken); |
28
|
|
|
$upHosts = $this->getUpHosts($ak, $bucket); |
29
|
|
|
return $upHosts[0]; |
30
|
|
|
} |
31
|
|
|
|
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) |
112
|
|
|
{ |
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); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.