/
region.go
442 lines (383 loc) · 11.1 KB
/
region.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package storage
import (
"context"
"fmt"
"strings"
"time"
"github.com/qiniu/go-sdk/v7/auth"
"github.com/qiniu/go-sdk/v7/client"
"github.com/qiniu/go-sdk/v7/internal/clientv2"
"github.com/qiniu/go-sdk/v7/internal/hostprovider"
"github.com/qiniu/go-sdk/v7/storagev2/apis"
"github.com/qiniu/go-sdk/v7/storagev2/http_client"
region_v2 "github.com/qiniu/go-sdk/v7/storagev2/region"
)
// 存储所在的地区,例如华东,华南,华北
// 每个存储区域可能有多个机房信息,每个机房可能有多个上传入口
type Region struct {
// 上传入口
SrcUpHosts []string `json:"src_up,omitempty"`
// 加速上传入口
CdnUpHosts []string `json:"cdn_up,omitempty"`
// 获取文件信息入口
RsHost string `json:"rs,omitempty"`
// bucket列举入口
RsfHost string `json:"rsf,omitempty"`
ApiHost string `json:"api,omitempty"`
// 存储io 入口
IovipHost string `json:"io,omitempty"`
// 源站下载入口
IoSrcHost string `json:"io_src,omitempty"`
}
type RegionID string
// GetDefaultReion 根据RegionID获取对应的Region信息
func GetRegionByID(regionID RegionID) (Region, bool) {
if r, ok := regionMap[regionID]; ok {
return r, ok
}
return Region{}, false
}
func (r *Region) String() string {
str := ""
str += fmt.Sprintf("SrcUpHosts: %v\n", r.SrcUpHosts)
str += fmt.Sprintf("CdnUpHosts: %v\n", r.CdnUpHosts)
str += fmt.Sprintf("IovipHost: %s\n", r.IovipHost)
str += fmt.Sprintf("RsHost: %s\n", r.RsHost)
str += fmt.Sprintf("RsfHost: %s\n", r.RsfHost)
str += fmt.Sprintf("ApiHost: %s\n", r.ApiHost)
return str
}
func endpoint(useHttps bool, host string) string {
host = strings.TrimSpace(host)
if host == "" {
return ""
}
if strings.HasPrefix(host, "http://") ||
strings.HasPrefix(host, "https://") {
return host
}
scheme := "http://"
if useHttps {
scheme = "https://"
}
return fmt.Sprintf("%s%s", scheme, host)
}
// 获取rsfHost
func (r *Region) GetRsfHost(useHttps bool) string {
return endpoint(useHttps, r.RsfHost)
}
// 获取io host
func (r *Region) GetIoHost(useHttps bool) string {
return endpoint(useHttps, r.IovipHost)
}
// 获取RsHost
func (r *Region) GetRsHost(useHttps bool) string {
return endpoint(useHttps, r.RsHost)
}
// 获取api host
func (r *Region) GetApiHost(useHttps bool) string {
return endpoint(useHttps, r.ApiHost)
}
func (r *Region) GetRegions(ctx context.Context) ([]*region_v2.Region, error) {
newRegion := ®ion_v2.Region{
Up: region_v2.Endpoints{Preferred: append(r.CdnUpHosts, r.SrcUpHosts...)},
}
if host := r.IovipHost; host != "" {
newRegion.Io = region_v2.Endpoints{Preferred: []string{host}}
}
if host := r.IoSrcHost; host != "" {
newRegion.IoSrc = region_v2.Endpoints{Preferred: []string{host}}
}
if host := r.RsHost; host != "" {
newRegion.Rs = region_v2.Endpoints{Preferred: []string{host}}
}
if host := r.RsfHost; host != "" {
newRegion.Rsf = region_v2.Endpoints{Preferred: []string{host}}
}
if host := r.ApiHost; host != "" {
newRegion.Api = region_v2.Endpoints{Preferred: []string{host}}
}
return []*region_v2.Region{newRegion}, nil
}
var (
// regionHuadong 表示华东机房
regionHuadong = Region{
SrcUpHosts: []string{
"up.qiniup.com",
},
CdnUpHosts: []string{
"upload.qiniup.com",
},
RsHost: "rs.qbox.me",
RsfHost: "rsf.qbox.me",
ApiHost: "api.qiniu.com",
IovipHost: "iovip.qbox.me",
}
// regionHuadongZhejiang2 表示华东-浙江2
regionHuadongZhejiang2 = Region{
SrcUpHosts: []string{
"up-cn-east-2.qiniup.com",
},
CdnUpHosts: []string{
"upload-cn-east-2.qiniup.com",
},
RsHost: "rs-cn-east-2.qiniuapi.com",
RsfHost: "rsf-cn-east-2.qiniuapi.com",
ApiHost: "api-cn-east-2.qiniuapi.com",
IovipHost: "iovip-cn-east-2.qiniuio.com",
}
// regionHuabei 表示华北机房
regionHuabei = Region{
SrcUpHosts: []string{
"up-z1.qiniup.com",
},
CdnUpHosts: []string{
"upload-z1.qiniup.com",
},
RsHost: "rs-z1.qbox.me",
RsfHost: "rsf-z1.qbox.me",
ApiHost: "api-z1.qiniuapi.com",
IovipHost: "iovip-z1.qbox.me",
}
// regionHuanan 表示华南机房
regionHuanan = Region{
SrcUpHosts: []string{
"up-z2.qiniup.com",
},
CdnUpHosts: []string{
"upload-z2.qiniup.com",
},
RsHost: "rs-z2.qbox.me",
RsfHost: "rsf-z2.qbox.me",
ApiHost: "api-z2.qiniuapi.com",
IovipHost: "iovip-z2.qbox.me",
}
// regionNorthAmerica 表示北美机房
regionNorthAmerica = Region{
SrcUpHosts: []string{
"up-na0.qiniup.com",
},
CdnUpHosts: []string{
"upload-na0.qiniup.com",
},
RsHost: "rs-na0.qbox.me",
RsfHost: "rsf-na0.qbox.me",
ApiHost: "api-na0.qiniuapi.com",
IovipHost: "iovip-na0.qbox.me",
}
// regionSingapore 表示新加坡机房
regionSingapore = Region{
SrcUpHosts: []string{
"up-as0.qiniup.com",
},
CdnUpHosts: []string{
"upload-as0.qiniup.com",
},
RsHost: "rs-as0.qbox.me",
RsfHost: "rsf-as0.qbox.me",
ApiHost: "api-as0.qiniuapi.com",
IovipHost: "iovip-as0.qbox.me",
}
)
const (
// region code
RIDHuadong = RegionID("z0")
RIDHuadongZheJiang2 = RegionID("cn-east-2")
RIDHuabei = RegionID("z1")
RIDHuanan = RegionID("z2")
RIDNorthAmerica = RegionID("na0")
RIDSingapore = RegionID("as0")
)
// regionMap 是RegionID到具体的Region的映射
var regionMap = map[RegionID]Region{
RIDHuadong: regionHuadong,
RIDHuadongZheJiang2: regionHuadongZhejiang2,
RIDHuanan: regionHuanan,
RIDHuabei: regionHuabei,
RIDSingapore: regionSingapore,
RIDNorthAmerica: regionNorthAmerica,
}
const (
defaultApiHost = "api.qiniu.com"
defaultUcHost0 = "kodo-config.qiniuapi.com"
defaultUcHost1 = "uc.qbox.me"
)
// UcHost 为查询空间相关域名的 API 服务地址
// 设置 UcHost 时,如果不指定 scheme 默认会使用 https
// Deprecated 使用 SetUcHosts 替换
var UcHost = ""
// 公有云包括 defaultApiHost,非 uc query api 使用时需要移除 defaultApiHost
// 用户配置时,不能配置 api 域名
var ucHosts = []string{defaultUcHost0, defaultUcHost1, defaultApiHost}
// SetUcHost
// Deprecated 使用 SetUcHosts 替换
func SetUcHost(host string, useHttps bool) {
if len(host) == 0 {
return
}
host = endpoint(useHttps, host)
ucHosts = []string{host}
}
// SetUcHosts 配置多个 UC 域名
func SetUcHosts(hosts ...string) {
var newHosts []string
for _, host := range hosts {
if len(host) > 0 {
newHosts = append(newHosts, host)
}
}
ucHosts = newHosts
}
func getUcHost(useHttps bool) string {
// 兼容老版本,优先使用 UcHost
host := ""
if len(UcHost) > 0 {
host = UcHost
} else if len(ucHosts) > 0 {
host = ucHosts[0]
}
return endpoint(useHttps, host)
}
// 不带 scheme
func getUcBackupHosts() []string {
var hosts []string
if len(UcHost) > 0 {
hosts = append(hosts, removeHostScheme(UcHost))
}
for _, host := range ucHosts {
if len(host) > 0 {
hosts = append(hosts, removeHostScheme(host))
}
}
hosts = removeRepeatStringItem(hosts)
return hosts
}
func getUcEndpoint(useHttps bool, hosts []string) region_v2.EndpointsProvider {
if len(hosts) == 0 {
if len(UcHost) > 0 {
hosts = append(hosts, endpoint(useHttps, UcHost))
}
for _, host := range ucHosts {
if len(host) > 0 {
hosts = append(hosts, endpoint(useHttps, host))
}
}
}
if len(hosts) > 0 {
return region_v2.Endpoints{Preferred: hosts}
} else {
return nil
}
}
// GetRegion 用来根据ak和bucket来获取空间相关的机房信息
// 延用 v2, v2 结构和 v4 结构不同且暂不可替代
// Deprecated 使用 GetRegionWithOptions 替换
func GetRegion(ak, bucket string) (*Region, error) {
return GetRegionWithOptions(ak, bucket, DefaultUCApiOptions())
}
// GetRegionWithOptions 用来根据ak和bucket来获取空间相关的机房信息
func GetRegionWithOptions(ak, bucket string, options UCApiOptions) (*Region, error) {
return getRegionByV2(ak, bucket, options)
}
// 使用 v4
//
//lint:ignore U1000 Used by test case
func getRegionGroup(ak, bucket string) (*RegionGroup, error) {
return getRegionByV4(ak, bucket, DefaultUCApiOptions())
}
func getRegionGroupWithOptions(ak, bucket string, options UCApiOptions) (*RegionGroup, error) {
return getRegionByV4(ak, bucket, options)
}
type RegionInfo struct {
ID string `json:"id"`
Description string `json:"description"`
}
func SetRegionCachePath(newPath string) {
setRegionV2CachePath(newPath)
setRegionV4CachePath(newPath)
}
// GetRegionsInfo Deprecated and use GetRegionsInfoWithOptions instead
// Deprecated
func GetRegionsInfo(mac *auth.Credentials) ([]RegionInfo, error) {
return GetRegionsInfoWithOptions(mac, DefaultUCApiOptions())
}
func GetRegionsInfoWithOptions(mac *auth.Credentials, options UCApiOptions) ([]RegionInfo, error) {
var httpClient clientv2.Client
if options.Client != nil {
httpClient = options.Client.Client
}
response, err := apis.NewStorage(&http_client.Options{
BasicHTTPClient: httpClient,
HostFreezeDuration: options.HostFreezeDuration,
HostRetryConfig: &clientv2.RetryConfig{
RetryMax: options.RetryMax,
},
}).GetRegions(
context.Background(),
&apis.GetRegionsRequest{Credentials: mac},
&apis.Options{OverwrittenBucketHosts: getUcEndpoint(options.UseHttps, options.Hosts)},
)
if err != nil {
return nil, err
}
regions := make([]RegionInfo, 0, len(response.Regions))
for _, region := range response.Regions {
regions = append(regions, RegionInfo{
ID: region.Id,
Description: region.Description,
})
}
return regions, nil
}
type ucClientConfig struct {
// 非 uc query api 需要去除默认域名 defaultApiHost
IsUcQueryApi bool
// 单域名重试次数
RetryMax int
// 请求的域名
Hosts []string
// 主备域名冻结时间(默认:600s),当一个域名请求失败(单个域名会被重试 TryTimes 次),会被冻结一段时间,使用备用域名进行重试,在冻结时间内,域名不能被使用,当一个操作中所有域名竣备冻结操作不在进行重试,返回最后一次操作的错误。
HostFreezeDuration time.Duration
Client *client.Client
}
func getUCClient(config ucClientConfig, mac *auth.Credentials) clientv2.Client {
allHosts := config.Hosts
if len(allHosts) == 0 {
allHosts = getUcBackupHosts()
}
var hosts []string = nil
if !config.IsUcQueryApi {
// 非 uc query api 去除 defaultApiHost
for _, host := range allHosts {
if host != defaultApiHost {
hosts = append(hosts, host)
}
}
} else {
hosts = allHosts
}
is := []clientv2.Interceptor{
clientv2.NewHostsRetryInterceptor(clientv2.HostsRetryConfig{
RetryConfig: clientv2.RetryConfig{
RetryMax: len(hosts),
RetryInterval: nil,
ShouldRetry: nil,
},
ShouldFreezeHost: nil,
HostFreezeDuration: config.HostFreezeDuration,
HostProvider: hostprovider.NewWithHosts(hosts),
}),
clientv2.NewSimpleRetryInterceptor(clientv2.RetryConfig{
RetryMax: config.RetryMax,
RetryInterval: nil,
ShouldRetry: nil,
}),
}
if mac != nil {
is = append(is, clientv2.NewAuthInterceptor(clientv2.AuthConfig{
Credentials: mac,
TokenType: auth.TokenQiniu,
}))
}
return clientv2.NewClient(clientv2.NewClientWithClientV1(config.Client), is...)
}