Skip to content

Commit 57deed0

Browse files
authored
Merge pull request #403 from qiniu/fix/get-region-ttl-and-domains
update getRegion and configurable uc
2 parents 0b34e66 + 47bdc4a commit 57deed0

File tree

7 files changed

+159
-99
lines changed

7 files changed

+159
-99
lines changed

.github/workflows/test-ci.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
name: PHP CI with Composer
22

3-
on:
4-
push:
5-
paths-ignore:
6-
- '**.md'
3+
on: [push, pull_request]
4+
5+
concurrency: keep-only-one
76

87
jobs:
98
build:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
## Next Version
44
* 对象存储,修复无法对 key 为空字符串的对象进行操作
55
* 修复 301 重定向无法正确获取 header 信息
6+
* 对象存储,新增查询区域域名过期时间
7+
* 对象存储,更新获取区域域名的接口
8+
* 对象存储,更新查询 bucket 域名为 uc 服务
9+
* 对象存储,新增 uc 服务可配置
610

711
## 7.8.0 (2022-10-25)
812
* 移除不推荐域名,并增加区域亚太-首尔和华东-浙江2

src/Qiniu/Config.php

+92-25
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class Config
3030
public $zone;
3131
// Zone Cache
3232
private $regionCache;
33+
// UC Host
34+
private $ucHost;
3335

3436
// 构造函数
3537
public function __construct(Region $z = null)
@@ -38,6 +40,23 @@ public function __construct(Region $z = null)
3840
$this->useHTTPS = false;
3941
$this->useCdnDomains = false;
4042
$this->regionCache = array();
43+
$this->ucHost = Config::UC_HOST;
44+
}
45+
46+
public function setUcHost($ucHost)
47+
{
48+
$this->ucHost = $ucHost;
49+
}
50+
51+
public function getUcHost()
52+
{
53+
if ($this->useHTTPS === true) {
54+
$scheme = "https://";
55+
} else {
56+
$scheme = "http://";
57+
}
58+
59+
return $scheme . $this->ucHost;
4160
}
4261

4362
public function getUpHost($accessKey, $bucket)
@@ -232,46 +251,94 @@ public function getApiHostV2($accessKey, $bucket)
232251
return array($scheme . $region->apiHost, null);
233252
}
234253

254+
255+
/**
256+
* 从缓存中获取区域
257+
*
258+
* @param string $cacheId 缓存 ID
259+
* @return null|Region
260+
*/
261+
private function getRegionCache($cacheId)
262+
{
263+
if (isset($this->regionCache[$cacheId]) &&
264+
isset($this->regionCache[$cacheId]["deadline"]) &&
265+
time() < $this->regionCache[$cacheId]["deadline"]
266+
) {
267+
return $this->regionCache[$cacheId]["region"];
268+
}
269+
270+
return null;
271+
}
272+
273+
/**
274+
* 将区域设置到缓存中
275+
*
276+
* @param string $cacheId 缓存 ID
277+
* @param Region $region 缓存 ID
278+
* @return void
279+
*/
280+
private function setRegionCache($cacheId, $region)
281+
{
282+
$this->regionCache[$cacheId] = array(
283+
"region" => $region,
284+
);
285+
if (isset($region->ttl)) {
286+
$this->regionCache[$cacheId]["deadline"] = time() + $region->ttl;
287+
}
288+
}
289+
290+
/**
291+
* 从缓存中获取区域
292+
*
293+
* @param string $accessKey
294+
* @param string $bucket
295+
* @return Region
296+
*
297+
* @throws \Exception
298+
*/
235299
private function getRegion($accessKey, $bucket)
236300
{
301+
if (isset($this->zone)) {
302+
return $this->zone;
303+
}
304+
237305
$cacheId = "$accessKey:$bucket";
306+
$regionCache = $this->getRegionCache($cacheId);
307+
if ($regionCache) {
308+
return $regionCache;
309+
}
238310

239-
if (isset($this->regionCache[$cacheId])) {
240-
$region = $this->regionCache[$cacheId];
241-
} elseif (isset($this->zone)) {
242-
$region = $this->zone;
243-
$this->regionCache[$cacheId] = $region;
244-
} else {
245-
$region = Zone::queryZone($accessKey, $bucket);
246-
if (is_array($region)) {
247-
list($region, $err) = $region;
248-
if ($err != null) {
249-
throw new \Exception($err->message());
250-
}
311+
$region = Zone::queryZone($accessKey, $bucket, $this->getUcHost());
312+
if (is_array($region)) {
313+
list($region, $err) = $region;
314+
if ($err != null) {
315+
throw new \Exception($err->message());
251316
}
252-
$this->regionCache[$cacheId] = $region;
253317
}
318+
319+
$this->setRegionCache($cacheId, $region);
254320
return $region;
255321
}
256322

257323
private function getRegionV2($accessKey, $bucket)
258324
{
325+
if (isset($this->zone)) {
326+
return array($this->zone, null);
327+
}
328+
259329
$cacheId = "$accessKey:$bucket";
330+
$regionCache = $this->getRegionCache($cacheId);
331+
if (isset($regionCache)) {
332+
return array($regionCache, null);
333+
}
260334

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;
335+
$region = Zone::queryZone($accessKey, $bucket, $this->getUcHost());
336+
if (is_array($region)) {
337+
list($region, $err) = $region;
338+
return array($region, $err);
273339
}
274340

341+
$this->setRegionCache($cacheId, $region);
275342
return array($region, null);
276343
}
277344
}

src/Qiniu/Region.php

+38-51
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class Region
1919
public $apiHost;
2020
//IOVIP域名
2121
public $iovipHost;
22+
// TTL
23+
public $ttl;
2224

2325
//构造一个Region对象
2426
public function __construct(
@@ -27,7 +29,8 @@ public function __construct(
2729
$rsHost = "rs-z0.qiniuapi.com",
2830
$rsfHost = "rsf-z0.qiniuapi.com",
2931
$apiHost = "api.qiniuapi.com",
30-
$iovipHost = null
32+
$iovipHost = null,
33+
$ttl = null
3134
) {
3235

3336
$this->srcUpHosts = $srcUpHosts;
@@ -36,6 +39,7 @@ public function __construct(
3639
$this->rsfHost = $rsfHost;
3740
$this->apiHost = $apiHost;
3841
$this->iovipHost = $iovipHost;
42+
$this->ttl = $ttl;
3943
}
4044

4145
//华东机房
@@ -165,67 +169,50 @@ public static function regionSeoul()
165169
}
166170

167171
/*
168-
* GET /v2/query?ak=<ak>&&bucket=<bucket>
172+
* GET /v2/query?ak=<ak>&bucket=<bucket>
169173
**/
170-
public static function queryRegion($ak, $bucket)
174+
public static function queryRegion($ak, $bucket, $ucHost = null)
171175
{
172-
$Region = new Region();
173-
$url = Config::API_HOST . '/v2/query' . "?ak=$ak&bucket=$bucket";
176+
$region = new Region();
177+
if (!$ucHost) {
178+
$ucHost = "https://" . Config::UC_HOST;
179+
}
180+
$url = $ucHost . '/v4/query' . "?ak=$ak&bucket=$bucket";
174181
$ret = Client::Get($url);
175182
if (!$ret->ok()) {
176183
return array(null, new Error($url, $ret));
177184
}
178185
$r = ($ret->body === null) ? array() : $ret->json();
179-
//parse Region;
180-
181-
$iovipHost = $r['io']['src']['main'][0];
182-
$Region->iovipHost = $iovipHost;
183-
$accMain = $r['up']['acc']['main'][0];
184-
array_push($Region->cdnUpHosts, $accMain);
185-
if (isset($r['up']['acc']['backup'])) {
186-
foreach ($r['up']['acc']['backup'] as $key => $value) {
187-
array_push($Region->cdnUpHosts, $value);
188-
}
189-
}
190-
$srcMain = $r['up']['src']['main'][0];
191-
array_push($Region->srcUpHosts, $srcMain);
192-
if (isset($r['up']['src']['backup'])) {
193-
foreach ($r['up']['src']['backup'] as $key => $value) {
194-
array_push($Region->srcUpHosts, $value);
195-
}
186+
if (!is_array($r["hosts"]) || count($r["hosts"]) == 0) {
187+
return array(null, new Error($url, $ret));
196188
}
197189

198-
//set specific hosts
199-
if (strstr($Region->iovipHost, "z1") !== false) {
200-
$Region->rsHost = "rs-z1.qiniuapi.com";
201-
$Region->rsfHost = "rsf-z1.qiniuapi.com";
202-
$Region->apiHost = "api-z1.qiniuapi.com";
203-
} elseif (strstr($Region->iovipHost, "z2") !== false) {
204-
$Region->rsHost = "rs-z2.qiniuapi.com";
205-
$Region->rsfHost = "rsf-z2.qiniuapi.com";
206-
$Region->apiHost = "api-z2.qiniuapi.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";
211-
} elseif (strstr($Region->iovipHost, "na0") !== false) {
212-
$Region->rsHost = "rs-na0.qiniuapi.com";
213-
$Region->rsfHost = "rsf-na0.qiniuapi.com";
214-
$Region->apiHost = "api-na0.qiniuapi.com";
215-
} elseif (strstr($Region->iovipHost, "as0") !== false) {
216-
$Region->rsHost = "rs-as0.qiniuapi.com";
217-
$Region->rsfHost = "rsf-as0.qiniuapi.com";
218-
$Region->apiHost = "api-as0.qiniuapi.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";
190+
// parse region;
191+
$regionHost = $r["hosts"][0];
192+
$region->cdnUpHosts = array_merge($region->cdnUpHosts, $regionHost['up']['domains']);
193+
$region->srcUpHosts = array_merge($region->srcUpHosts, $regionHost['up']['domains']);
194+
195+
// set specific hosts
196+
$region->iovipHost = $regionHost['io']['domains'][0];
197+
if (isset($regionHost['rs']['domains']) && count($regionHost['rs']['domains']) > 0) {
198+
$region->rsHost = $regionHost['rs']['domains'][0];
223199
} else {
224-
$Region->rsHost = "rs.qiniuapi.com";
225-
$Region->rsfHost = "rsf.qiniuapi.com";
226-
$Region->apiHost = "api.qiniuapi.com";
200+
$region->rsHost = Config::RS_HOST;
227201
}
202+
if (isset($regionHost['rsf']['domains']) && count($regionHost['rsf']['domains']) > 0) {
203+
$region->rsfHost = $regionHost['rsf']['domains'][0];
204+
} else {
205+
$region->rsfHost = Config::RSF_HOST;
206+
}
207+
if (isset($regionHost['api']['domains']) && count($regionHost['api']['domains']) > 0) {
208+
$region->apiHost = $regionHost['api']['domains'][0];
209+
} else {
210+
$region->apiHost = Config::API_HOST;
211+
}
212+
213+
// set ttl
214+
$region->ttl = $regionHost['ttl'];
228215

229-
return $Region;
216+
return $region;
230217
}
231218
}

src/Qiniu/Storage/BucketManager.php

+6-15
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function buckets($shared = true)
3939
if ($shared === true) {
4040
$includeShared = "true";
4141
}
42-
return $this->getV2($this->getUcHost(). '/buckets?shared=' . $includeShared);
42+
return $this->getV2($this->config->getUcHost(). '/buckets?shared=' . $includeShared);
4343
}
4444

4545
/**
@@ -71,7 +71,7 @@ public function listbuckets(
7171
public function createBucket($name, $region = 'z0')
7272
{
7373
$path = '/mkbucketv3/' . $name . '/region/' . $region;
74-
return $this->postV2($this->getUcHost() . $path, null);
74+
return $this->postV2($this->config->getUcHost() . $path, null);
7575
}
7676

7777
/**
@@ -85,7 +85,7 @@ public function createBucket($name, $region = 'z0')
8585
public function deleteBucket($name)
8686
{
8787
$path = '/drop/' . $name;
88-
return $this->postV2($this->getUcHost() . $path, null);
88+
return $this->postV2($this->config->getUcHost() . $path, null);
8989
}
9090

9191
/**
@@ -96,7 +96,7 @@ public function deleteBucket($name)
9696
*/
9797
public function domains($bucket)
9898
{
99-
return $this->apiGet($bucket, '/v6/domain/list?tbl=' . $bucket);
99+
return $this->ucGet('/v2/domains?tbl=' . $bucket);
100100
}
101101

102102
/**
@@ -994,15 +994,6 @@ public function setObjectLifecycleWithCond(
994994
return $this->rsPost($bucket, $path);
995995
}
996996

997-
private function getUcHost()
998-
{
999-
$scheme = "http://";
1000-
if ($this->config->useHTTPS === true) {
1001-
$scheme = "https://";
1002-
}
1003-
return $scheme . Config::UC_HOST;
1004-
}
1005-
1006997
private function rsfGet($bucket, $path)
1007998
{
1008999
list($host, $err) = $this->config->getRsfHostV2($this->auth->getAccessKey(), $bucket);
@@ -1061,13 +1052,13 @@ private function apiPost($bucket, $path, $body = null)
10611052

10621053
private function ucGet($path)
10631054
{
1064-
$url = $this->getUcHost() . $path;
1055+
$url = $this->config->getUcHost() . $path;
10651056
return $this->getV2($url);
10661057
}
10671058

10681059
private function ucPost($path, $body = null)
10691060
{
1070-
$url = $this->getUcHost() . $path;
1061+
$url = $this->config->getUcHost() . $path;
10711062
return $this->postV2($url, $body);
10721063
}
10731064

src/Qiniu/Zone.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public static function qvmZonez1()
5050
return parent::qvmRegionHuabei();
5151
}
5252

53-
public static function queryZone($ak, $bucket)
53+
public static function queryZone($ak, $bucket, $ucHost = null)
5454
{
55-
return parent::queryRegion($ak, $bucket);
55+
return parent::queryRegion($ak, $bucket, $ucHost);
5656
}
5757
}

0 commit comments

Comments
 (0)