Skip to content

Commit 4dc187e

Browse files
authored
Merge pull request #386 from qiniu/features/add-zone-and-fix-query-host
add zone and fix query host
2 parents 0e9296a + 73dc85a commit 4dc187e

File tree

9 files changed

+292
-85
lines changed

9 files changed

+292
-85
lines changed

src/Qiniu/Config.php

+127
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ public function getUpHost($accessKey, $bucket)
5757
return $scheme . $host;
5858
}
5959

60+
public function getUpHostV2($accessKey, $bucket)
61+
{
62+
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
63+
if ($err != null) {
64+
return array(null, $err);
65+
}
66+
67+
if ($this->useHTTPS === true) {
68+
$scheme = "https://";
69+
} else {
70+
$scheme = "http://";
71+
}
72+
73+
$host = $region->srcUpHosts[0];
74+
if ($this->useCdnDomains === true) {
75+
$host = $region->cdnUpHosts[0];
76+
}
77+
78+
return array($scheme . $host, null);
79+
}
80+
6081
public function getUpBackupHost($accessKey, $bucket)
6182
{
6283
$region = $this->getRegion($accessKey, $bucket);
@@ -74,6 +95,27 @@ public function getUpBackupHost($accessKey, $bucket)
7495
return $scheme . $host;
7596
}
7697

98+
public function getUpBackupHostV2($accessKey, $bucket)
99+
{
100+
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
101+
if ($err != null) {
102+
return array(null, $err);
103+
}
104+
105+
if ($this->useHTTPS === true) {
106+
$scheme = "https://";
107+
} else {
108+
$scheme = "http://";
109+
}
110+
111+
$host = $region->cdnUpHosts[0];
112+
if ($this->useCdnDomains === true) {
113+
$host = $region->srcUpHosts[0];
114+
}
115+
116+
return array($scheme . $host, null);
117+
}
118+
77119
public function getRsHost($accessKey, $bucket)
78120
{
79121
$region = $this->getRegion($accessKey, $bucket);
@@ -87,6 +129,22 @@ public function getRsHost($accessKey, $bucket)
87129
return $scheme . $region->rsHost;
88130
}
89131

132+
public function getRsHostV2($accessKey, $bucket)
133+
{
134+
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
135+
if ($err != null) {
136+
return array(null, $err);
137+
}
138+
139+
if ($this->useHTTPS === true) {
140+
$scheme = "https://";
141+
} else {
142+
$scheme = "http://";
143+
}
144+
145+
return array($scheme . $region->rsHost, null);
146+
}
147+
90148
public function getRsfHost($accessKey, $bucket)
91149
{
92150
$region = $this->getRegion($accessKey, $bucket);
@@ -100,6 +158,22 @@ public function getRsfHost($accessKey, $bucket)
100158
return $scheme . $region->rsfHost;
101159
}
102160

161+
public function getRsfHostV2($accessKey, $bucket)
162+
{
163+
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
164+
if ($err != null) {
165+
return array(null, $err);
166+
}
167+
168+
if ($this->useHTTPS === true) {
169+
$scheme = "https://";
170+
} else {
171+
$scheme = "http://";
172+
}
173+
174+
return array($scheme . $region->rsfHost, null);
175+
}
176+
103177
public function getIovipHost($accessKey, $bucket)
104178
{
105179
$region = $this->getRegion($accessKey, $bucket);
@@ -113,6 +187,22 @@ public function getIovipHost($accessKey, $bucket)
113187
return $scheme . $region->iovipHost;
114188
}
115189

190+
public function getIovipHostV2($accessKey, $bucket)
191+
{
192+
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
193+
if ($err != null) {
194+
return array(null, $err);
195+
}
196+
197+
if ($this->useHTTPS === true) {
198+
$scheme = "https://";
199+
} else {
200+
$scheme = "http://";
201+
}
202+
203+
return array($scheme . $region->iovipHost, null);
204+
}
205+
116206
public function getApiHost($accessKey, $bucket)
117207
{
118208
$region = $this->getRegion($accessKey, $bucket);
@@ -126,6 +216,22 @@ public function getApiHost($accessKey, $bucket)
126216
return $scheme . $region->apiHost;
127217
}
128218

219+
public function getApiHostV2($accessKey, $bucket)
220+
{
221+
list($region, $err) = $this->getRegionV2($accessKey, $bucket);
222+
if ($err != null) {
223+
return array(null, $err);
224+
}
225+
226+
if ($this->useHTTPS === true) {
227+
$scheme = "https://";
228+
} else {
229+
$scheme = "http://";
230+
}
231+
232+
return array($scheme . $region->apiHost, null);
233+
}
234+
129235
private function getRegion($accessKey, $bucket)
130236
{
131237
$cacheId = "$accessKey:$bucket";
@@ -147,4 +253,25 @@ private function getRegion($accessKey, $bucket)
147253
}
148254
return $region;
149255
}
256+
257+
private function getRegionV2($accessKey, $bucket)
258+
{
259+
$cacheId = "$accessKey:$bucket";
260+
261+
if (isset($this->regionCache[$cacheId])) {
262+
$region = $this->regionCache[$cacheId];
263+
} elseif (isset($this->zone)) {
264+
$region = $this->zone;
265+
$this->regionCache[$cacheId] = $region;
266+
} else {
267+
$region = Zone::queryZone($accessKey, $bucket);
268+
if (is_array($region)) {
269+
list($region, $err) = $region;
270+
return array($region, $err);
271+
}
272+
$this->regionCache[$cacheId] = $region;
273+
}
274+
275+
return array($region, null);
276+
}
150277
}

src/Qiniu/Region.php

+35
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ public static function regionHuanan()
107107
return $regionHuanan;
108108
}
109109

110+
//华东2 机房
111+
public static function regionHuadong2()
112+
{
113+
return new Region(
114+
array('up-cn-east-2.qiniup.com'),
115+
array('upload-cn-east-2.qiniup.com'),
116+
"rs-cn-east-2.qiniuapi.com",
117+
"rsf-cn-east-2.qiniuapi.com",
118+
"api-cn-east-2.qiniuapi.com",
119+
"iovip-cn-east-2.qiniuio.com"
120+
);
121+
}
122+
110123
//北美机房
111124
public static function regionNorthAmerica()
112125
{
@@ -137,6 +150,20 @@ public static function regionSingapore()
137150
return $regionSingapore;
138151
}
139152

153+
//首尔
154+
public static function regionSeoul()
155+
{
156+
//首尔
157+
return new Region(
158+
array('up-ap-northeast-1.qiniup.com'),
159+
array('upload-ap-northeast-1.qiniup.com'),
160+
"rs-ap-northeast-1.qiniuapi.com",
161+
"rsf-ap-northeast-1.qiniuapi.com",
162+
"api-ap-northeast-1.qiniuapi.com",
163+
"iovip-ap-northeast-1.qiniuio.com"
164+
);
165+
}
166+
140167
/*
141168
* GET /v2/query?ak=<ak>&&bucket=<bucket>
142169
**/
@@ -177,6 +204,10 @@ public static function queryRegion($ak, $bucket)
177204
$Region->rsHost = "rs-z2.qbox.me";
178205
$Region->rsfHost = "rsf-z2.qbox.me";
179206
$Region->apiHost = "api-z2.qiniu.com";
207+
} elseif (strstr($Region->iovipHost, "cn-east-2") !== false) {
208+
$Region->rsHost = "rs-cn-east-2.qiniuapi.com";
209+
$Region->rsfHost = "rsf-cn-east-2.qiniuapi.com";
210+
$Region->apiHost = "api-cn-east-2.qiniuapi.com";
180211
} elseif (strstr($Region->iovipHost, "na0") !== false) {
181212
$Region->rsHost = "rs-na0.qbox.me";
182213
$Region->rsfHost = "rsf-na0.qbox.me";
@@ -185,6 +216,10 @@ public static function queryRegion($ak, $bucket)
185216
$Region->rsHost = "rs-as0.qbox.me";
186217
$Region->rsfHost = "rsf-as0.qbox.me";
187218
$Region->apiHost = "api-as0.qiniu.com";
219+
} elseif (strstr($Region->iovipHost, "ap-northeast-1") !== false) {
220+
$Region->rsHost = "rs-ap-northeast-1.qiniuapi.com";
221+
$Region->rsfHost = "rsf-ap-northeast-1.qiniuapi.com";
222+
$Region->apiHost = "api-ap-northeast-1.qiniuapi.com";
188223
} else {
189224
$Region->rsHost = "rs.qbox.me";
190225
$Region->rsfHost = "rsf.qbox.me";

0 commit comments

Comments
 (0)