1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Qiniu; |
4
|
|
|
|
5
|
|
|
final class Config |
6
|
|
|
{ |
7
|
|
|
const SDK_VER = '7.14.0'; |
8
|
|
|
|
9
|
|
|
const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改 |
10
|
|
|
|
11
|
|
|
const RSF_HOST = 'rsf.qiniuapi.com'; |
12
|
|
|
const API_HOST = 'api.qiniuapi.com'; |
13
|
|
|
const RS_HOST = 'rs.qiniuapi.com'; // RS Host |
14
|
|
|
const UC_HOST = 'uc.qiniuapi.com'; // UC Host |
15
|
|
|
const QUERY_REGION_HOST = Config::UC_HOST; |
16
|
|
|
const RTCAPI_HOST = 'http://rtc.qiniuapi.com'; |
17
|
|
|
const ARGUS_HOST = 'ai.qiniuapi.com'; |
18
|
|
|
const CASTER_HOST = 'pili-caster.qiniuapi.com'; |
19
|
|
|
const SMS_HOST = "https://sms.qiniuapi.com"; |
20
|
|
|
const RTCAPI_VERSION = 'v3'; |
21
|
|
|
const SMS_VERSION = 'v1'; |
22
|
|
|
|
23
|
|
|
// Zone 空间对应的存储区域 |
24
|
|
|
public $region; |
25
|
|
|
//BOOL 是否使用https域名 |
26
|
|
|
public $useHTTPS; |
27
|
|
|
//BOOL 是否使用CDN加速上传域名 |
28
|
|
|
public $useCdnDomains; |
29
|
|
|
/** |
30
|
|
|
* @var Region |
31
|
69 |
|
*/ |
32
|
|
|
public $zone; |
33
|
69 |
|
// Zone Cache |
34
|
69 |
|
private $regionCache; |
35
|
69 |
|
// UC Host |
36
|
69 |
|
private $ucHost; |
37
|
69 |
|
private $queryRegionHost; |
38
|
|
|
// backup UC Hosts |
39
|
18 |
|
private $backupQueryRegionHosts; |
40
|
|
|
// backup UC Hosts max retry time |
41
|
18 |
|
public $backupUcHostsRetryTimes; |
42
|
18 |
|
|
43
|
|
|
// 构造函数 |
44
|
|
|
public function __construct(Region $z = null) |
45
|
18 |
|
{ |
46
|
|
|
$this->zone = $z; |
47
|
|
|
$this->useHTTPS = false; |
48
|
18 |
|
$this->useCdnDomains = false; |
49
|
18 |
|
$this->regionCache = array(); |
50
|
|
|
$this->ucHost = Config::UC_HOST; |
51
|
|
|
$this->queryRegionHost = Config::QUERY_REGION_HOST; |
52
|
|
|
$this->backupQueryRegionHosts = array( |
53
|
18 |
|
"kodo-config.qiniuapi.com", |
54
|
|
|
"uc.qbox.me", |
55
|
|
|
); |
56
|
3 |
|
$this->backupUcHostsRetryTimes = 2; |
57
|
|
|
} |
58
|
3 |
|
|
59
|
3 |
|
public function setUcHost($ucHost) |
60
|
|
|
{ |
61
|
|
|
$this->ucHost = $ucHost; |
62
|
3 |
|
$this->setQueryRegionHost($ucHost); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
public function getUcHost() |
66
|
3 |
|
{ |
67
|
|
|
if ($this->useHTTPS === true) { |
68
|
|
|
$scheme = "https://"; |
69
|
|
|
} else { |
70
|
3 |
|
$scheme = "http://"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $scheme . $this->ucHost; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function setQueryRegionHost($host, $backupHosts = array()) |
77
|
|
|
{ |
78
|
|
|
$this->queryRegionHost = $host; |
79
|
|
|
$this->backupQueryRegionHosts = $backupHosts; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getQueryRegionHost() |
83
|
|
|
{ |
84
|
|
|
if ($this->useHTTPS === true) { |
85
|
|
|
$scheme = "https://"; |
86
|
|
|
} else { |
87
|
|
|
$scheme = "http://"; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $scheme . $this->queryRegionHost; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function setBackupQueryRegionHosts($hosts = array()) |
94
|
|
|
{ |
95
|
|
|
$this->backupQueryRegionHosts = $hosts; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getBackupQueryRegionHosts() |
99
|
6 |
|
{ |
100
|
|
|
return $this->backupQueryRegionHosts; |
101
|
6 |
|
} |
102
|
|
|
|
103
|
6 |
|
public function getUpHost($accessKey, $bucket, $reqOpt = null) |
104
|
|
|
{ |
105
|
|
|
$region = $this->getRegion($accessKey, $bucket, $reqOpt); |
106
|
6 |
|
if ($this->useHTTPS === true) { |
107
|
|
|
$scheme = "https://"; |
108
|
|
|
} else { |
109
|
6 |
|
$scheme = "http://"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$host = $region->srcUpHosts[0]; |
113
|
|
|
if ($this->useCdnDomains === true) { |
114
|
|
|
$host = $region->cdnUpHosts[0]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $scheme . $host; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getUpHostV2($accessKey, $bucket, $reqOpt = null) |
121
|
|
|
{ |
122
|
|
|
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt); |
123
|
|
|
if ($err != null) { |
124
|
|
|
return array(null, $err); |
125
|
24 |
|
} |
126
|
|
|
|
127
|
24 |
|
if ($this->useHTTPS === true) { |
128
|
|
|
$scheme = "https://"; |
129
|
24 |
|
} else { |
130
|
6 |
|
$scheme = "http://"; |
131
|
24 |
|
} |
132
|
3 |
|
|
133
|
3 |
|
$host = $region->srcUpHosts[0]; |
134
|
3 |
|
if ($this->useCdnDomains === true) { |
135
|
21 |
|
$host = $region->cdnUpHosts[0]; |
136
|
21 |
|
} |
137
|
|
|
|
138
|
24 |
|
return array($scheme . $host, null); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getUpBackupHost($accessKey, $bucket, $reqOpt = null) |
142
|
|
|
{ |
143
|
|
|
$region = $this->getRegion($accessKey, $bucket, $reqOpt); |
144
|
|
|
if ($this->useHTTPS === true) { |
145
|
|
|
$scheme = "https://"; |
146
|
|
|
} else { |
147
|
|
|
$scheme = "http://"; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$host = $region->cdnUpHosts[0]; |
151
|
|
|
if ($this->useCdnDomains === true) { |
152
|
|
|
$host = $region->srcUpHosts[0]; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $scheme . $host; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getUpBackupHostV2($accessKey, $bucket, $reqOpt = null) |
159
|
|
|
{ |
160
|
|
|
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt); |
161
|
|
|
if ($err != null) { |
162
|
|
|
return array(null, $err); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ($this->useHTTPS === true) { |
166
|
|
|
$scheme = "https://"; |
167
|
|
|
} else { |
168
|
|
|
$scheme = "http://"; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$host = $region->cdnUpHosts[0]; |
172
|
|
|
if ($this->useCdnDomains === true) { |
173
|
|
|
$host = $region->srcUpHosts[0]; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return array($scheme . $host, null); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function getRsHost($accessKey, $bucket, $reqOpt = null) |
180
|
|
|
{ |
181
|
|
|
$region = $this->getRegion($accessKey, $bucket, $reqOpt); |
182
|
|
|
|
183
|
|
|
if ($this->useHTTPS === true) { |
184
|
|
|
$scheme = "https://"; |
185
|
|
|
} else { |
186
|
|
|
$scheme = "http://"; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $scheme . $region->rsHost; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getRsHostV2($accessKey, $bucket, $reqOpt = null) |
193
|
|
|
{ |
194
|
|
|
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt); |
195
|
|
|
if ($err != null) { |
196
|
|
|
return array(null, $err); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($this->useHTTPS === true) { |
200
|
|
|
$scheme = "https://"; |
201
|
|
|
} else { |
202
|
|
|
$scheme = "http://"; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return array($scheme . $region->rsHost, null); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getRsfHost($accessKey, $bucket, $reqOpt = null) |
209
|
|
|
{ |
210
|
|
|
$region = $this->getRegion($accessKey, $bucket, $reqOpt); |
211
|
|
|
|
212
|
|
|
if ($this->useHTTPS === true) { |
213
|
|
|
$scheme = "https://"; |
214
|
|
|
} else { |
215
|
|
|
$scheme = "http://"; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $scheme . $region->rsfHost; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getRsfHostV2($accessKey, $bucket, $reqOpt = null) |
222
|
|
|
{ |
223
|
|
|
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt); |
224
|
|
|
if ($err != null) { |
225
|
|
|
return array(null, $err); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
if ($this->useHTTPS === true) { |
229
|
|
|
$scheme = "https://"; |
230
|
|
|
} else { |
231
|
|
|
$scheme = "http://"; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return array($scheme . $region->rsfHost, null); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function getIovipHost($accessKey, $bucket, $reqOpt = null) |
238
|
|
|
{ |
239
|
|
|
$region = $this->getRegion($accessKey, $bucket, $reqOpt); |
240
|
|
|
|
241
|
|
|
if ($this->useHTTPS === true) { |
242
|
|
|
$scheme = "https://"; |
243
|
|
|
} else { |
244
|
|
|
$scheme = "http://"; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $scheme . $region->iovipHost; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function getIovipHostV2($accessKey, $bucket, $reqOpt = null) |
251
|
|
|
{ |
252
|
|
|
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt); |
253
|
|
|
if ($err != null) { |
254
|
|
|
return array(null, $err); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if ($this->useHTTPS === true) { |
258
|
|
|
$scheme = "https://"; |
259
|
|
|
} else { |
260
|
|
|
$scheme = "http://"; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return array($scheme . $region->iovipHost, null); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function getApiHost($accessKey, $bucket, $reqOpt = null) |
267
|
|
|
{ |
268
|
|
|
$region = $this->getRegion($accessKey, $bucket, $reqOpt); |
269
|
|
|
|
270
|
|
|
if ($this->useHTTPS === true) { |
271
|
|
|
$scheme = "https://"; |
272
|
|
|
} else { |
273
|
|
|
$scheme = "http://"; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
return $scheme . $region->apiHost; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function getApiHostV2($accessKey, $bucket, $reqOpt = null) |
280
|
|
|
{ |
281
|
|
|
list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt); |
282
|
|
|
if ($err != null) { |
283
|
|
|
return array(null, $err); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if ($this->useHTTPS === true) { |
287
|
|
|
$scheme = "https://"; |
288
|
|
|
} else { |
289
|
|
|
$scheme = "http://"; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return array($scheme . $region->apiHost, null); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* 从缓存中获取区域 |
298
|
|
|
* |
299
|
|
|
* @param string $cacheId 缓存 ID |
300
|
|
|
* @return null|Region |
301
|
|
|
*/ |
302
|
|
|
private function getRegionCache($cacheId) |
303
|
|
|
{ |
304
|
|
|
if (isset($this->regionCache[$cacheId]) && |
305
|
|
|
isset($this->regionCache[$cacheId]["deadline"]) && |
306
|
|
|
time() < $this->regionCache[$cacheId]["deadline"]) { |
307
|
|
|
return $this->regionCache[$cacheId]["region"]; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return null; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* 将区域设置到缓存中 |
315
|
|
|
* |
316
|
|
|
* @param string $cacheId 缓存 ID |
317
|
|
|
* @param Region $region 缓存 ID |
318
|
|
|
* @return void |
319
|
|
|
*/ |
320
|
|
|
private function setRegionCache($cacheId, $region) |
321
|
|
|
{ |
322
|
|
|
$this->regionCache[$cacheId] = array( |
323
|
|
|
"region" => $region, |
324
|
|
|
); |
325
|
|
|
if (isset($region->ttl)) { |
326
|
|
|
$this->regionCache[$cacheId]["deadline"] = time() + $region->ttl; |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* 从缓存中获取区域 |
332
|
|
|
* |
333
|
|
|
* @param string $accessKey |
334
|
|
|
* @param string $bucket |
335
|
|
|
* @return Region |
336
|
|
|
* |
337
|
|
|
* @throws \Exception |
338
|
|
|
*/ |
339
|
|
|
private function getRegion($accessKey, $bucket, $reqOpt = null) |
340
|
|
|
{ |
341
|
|
|
if (isset($this->zone)) { |
342
|
|
|
return $this->zone; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$cacheId = "$accessKey:$bucket"; |
346
|
|
|
$regionCache = $this->getRegionCache($cacheId); |
347
|
|
|
if ($regionCache) { |
348
|
|
|
return $regionCache; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
$region = Zone::queryZone( |
352
|
|
|
$accessKey, |
353
|
|
|
$bucket, |
354
|
|
|
$this->getQueryRegionHost(), |
355
|
|
|
$this->getBackupQueryRegionHosts(), |
356
|
|
|
$this->backupUcHostsRetryTimes, |
357
|
|
|
$reqOpt |
358
|
|
|
); |
359
|
|
|
if (is_array($region)) { |
|
|
|
|
360
|
|
|
list($region, $err) = $region; |
361
|
|
|
if ($err != null) { |
362
|
|
|
throw new \Exception($err->message()); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$this->setRegionCache($cacheId, $region); |
367
|
|
|
return $region; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
private function getRegionV2($accessKey, $bucket, $reqOpt = null) |
371
|
|
|
{ |
372
|
|
|
if (isset($this->zone)) { |
373
|
|
|
return array($this->zone, null); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
$cacheId = "$accessKey:$bucket"; |
377
|
|
|
$regionCache = $this->getRegionCache($cacheId); |
378
|
|
|
if (isset($regionCache)) { |
379
|
|
|
return array($regionCache, null); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$region = Zone::queryZone( |
383
|
|
|
$accessKey, |
384
|
|
|
$bucket, |
385
|
|
|
$this->getQueryRegionHost(), |
386
|
|
|
$this->getBackupQueryRegionHosts(), |
387
|
|
|
$this->backupUcHostsRetryTimes, |
388
|
|
|
$reqOpt |
389
|
|
|
); |
390
|
|
|
if (is_array($region)) { |
|
|
|
|
391
|
|
|
list($region, $err) = $region; |
392
|
|
|
return array($region, $err); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
$this->setRegionCache($cacheId, $region); |
396
|
|
|
return array($region, null); |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|