github.com/qiniu/go-sdk/v7@v7.27.0/storage/config.go (about)

     1  package storage
     2  
     3  import "github.com/qiniu/go-sdk/v7/storagev2/defaults"
     4  
     5  // Config 为文件上传,资源管理等配置
     6  type Config struct {
     7  	// 兼容保留
     8  	Zone *Region // 空间所在的存储区域
     9  
    10  	Region *Region
    11  
    12  	// 如果设置的Host本身是以http://开头的,又设置了该字段为true,那么优先使用该字段,使用https协议
    13  	// 同理如果该字段为false, 但是设置的host以https开头,那么使用http协议通信
    14  	UseHTTPS            bool   // 是否使用https域名
    15  	UseCdnDomains       bool   // 是否使用cdn加速域名
    16  	AccelerateUploading bool   // 是否启用上传加速
    17  	CentralRsHost       string // 中心机房的RsHost,用于list bucket
    18  
    19  	// 兼容保留
    20  	RsHost  string
    21  	RsfHost string
    22  	UpHost  string
    23  	ApiHost string
    24  	IoHost  string
    25  }
    26  
    27  // NewConfig 创建默认的 Config 对象
    28  func NewConfig() *Config {
    29  	var config Config
    30  	if isDisabled, err := defaults.DisableSecureProtocol(); err == nil {
    31  		config.UseHTTPS = !isDisabled
    32  	}
    33  	return &config
    34  }
    35  
    36  // reqHost 返回一个Host链接
    37  // 主要用于Config 中Host的获取,Region优先级最高, Zone次之, 最后才使用设置的Host信息
    38  // topHost是优先级最高的, host次之,如果都没有,使用默认的defaultHost
    39  func reqHost(useHttps bool, topHost, host, defaultHost string) (endp string) {
    40  	if topHost != "" {
    41  		return topHost
    42  	}
    43  	if host == "" {
    44  		host = defaultHost
    45  	}
    46  	return endpoint(useHttps, host)
    47  }
    48  
    49  // 获取RsHost
    50  // 优先使用Zone中的Host信息,如果Zone中的host信息没有配置,那么使用Config中的Host信息
    51  func (c *Config) RsReqHost() string {
    52  	rzHost := c.hostFromRegion("rs")
    53  	return reqHost(c.UseHTTPS, rzHost, c.RsHost, DefaultRsHost)
    54  }
    55  
    56  // GetRegion返回一个Region指针
    57  // 默认返回最新的Region, 如果该字段没有,那么返回兼容保留的Zone, 如果都为nil, 就返回nil
    58  func (c *Config) GetRegion() *Region {
    59  	if c.Region != nil {
    60  		return c.Region
    61  	}
    62  	if c.Zone != nil {
    63  		return c.Zone
    64  	}
    65  	return nil
    66  }
    67  
    68  func (c *Config) hostFromRegion(typ string) string {
    69  	region := c.GetRegion()
    70  	if region != nil {
    71  		switch typ {
    72  		case "rs":
    73  			return region.GetRsHost(c.UseHTTPS)
    74  		case "rsf":
    75  			return region.GetRsfHost(c.UseHTTPS)
    76  		case "api":
    77  			return region.GetApiHost(c.UseHTTPS)
    78  		case "io":
    79  			return region.GetIoHost(c.UseHTTPS)
    80  		}
    81  	}
    82  	return ""
    83  }
    84  
    85  // 获取rsfHost
    86  // 优先使用Zone中的Host信息,如果Zone中的host信息没有配置,那么使用Config中的Host信息
    87  func (c *Config) RsfReqHost() string {
    88  	rsHost := c.hostFromRegion("rsf")
    89  	return reqHost(c.UseHTTPS, rsHost, c.RsfHost, DefaultRsfHost)
    90  }
    91  
    92  // 获取apiHost
    93  // 优先使用Zone中的Host信息,如果Zone中的host信息没有配置,那么使用Config中的Host信息
    94  func (c *Config) ApiReqHost() string {
    95  	rzHost := c.hostFromRegion("api")
    96  	return reqHost(c.UseHTTPS, rzHost, c.ApiHost, DefaultAPIHost)
    97  }