config.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package aws
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/ks3sdklib/aws-sdk-go/aws/retry"
  6. "io"
  7. "net/http"
  8. "os"
  9. "time"
  10. "github.com/ks3sdklib/aws-sdk-go/aws/credentials"
  11. )
  12. // DefaultChainCredentials is a Credentials which will find the first available
  13. // credentials Value from the list of Providers.
  14. //
  15. // This should be used in the default case. Once the type of credentials are
  16. // known switching to the specific Credentials will be more efficient.
  17. var DefaultChainCredentials = credentials.NewChainCredentials(
  18. []credentials.Provider{
  19. &credentials.EnvProvider{},
  20. &credentials.SharedCredentialsProvider{Filename: "", Profile: ""},
  21. &credentials.EC2RoleProvider{ExpiryWindow: 5 * time.Minute},
  22. })
  23. // DefaultMaxRetries is the default number of retries for a service.
  24. const DefaultMaxRetries = 3
  25. // DefaultConfig is the default all service configuration will be based off of.
  26. var DefaultConfig = &Config{
  27. Credentials: DefaultChainCredentials,
  28. Endpoint: "",
  29. Ks3BillEndpoint: "ks3bill.api.ksyun.com",
  30. Region: "",
  31. DisableSSL: false,
  32. ManualSend: false,
  33. HTTPClient: http.DefaultClient,
  34. LogHTTPBody: false,
  35. LogLevel: Off,
  36. Logger: os.Stdout,
  37. MaxRetries: DefaultMaxRetries,
  38. RetryRule: retry.DefaultExponentialRetryRule,
  39. ShouldRetry: retry.ShouldRetry,
  40. DisableParamValidation: false,
  41. DisableComputeChecksums: false,
  42. S3ForcePathStyle: false,
  43. DomainMode: false,
  44. SignerVersion: "V2",
  45. CrcCheckEnabled: false,
  46. DisableRestProtocolURICleaning: true,
  47. DisableDnsCache: false,
  48. }
  49. // A Config provides service configuration
  50. type Config struct {
  51. Credentials *credentials.Credentials
  52. Endpoint string
  53. Ks3BillEndpoint string
  54. Region string
  55. DisableSSL bool
  56. ManualSend bool
  57. HTTPClient *http.Client
  58. LogHTTPBody bool
  59. LogLevel uint
  60. Logger io.Writer
  61. MaxRetries int // 重试次数
  62. RetryRule retry.RetryRule // 重试规则
  63. ShouldRetry func(error) bool // 是否需要重试
  64. DisableParamValidation bool
  65. DisableComputeChecksums bool
  66. S3ForcePathStyle bool
  67. DomainMode bool
  68. SignerVersion string
  69. CrcCheckEnabled bool // 允许crc64校验,默认为false
  70. DisableRestProtocolURICleaning bool // 禁用path clean,默认为true
  71. DisableDnsCache bool // 禁用DNS缓存,默认为false
  72. }
  73. // Copy will return a shallow copy of the Config object.
  74. func (c Config) Copy() Config {
  75. dst := Config{}
  76. dst.Credentials = c.Credentials
  77. dst.Endpoint = c.Endpoint
  78. dst.Ks3BillEndpoint = c.Ks3BillEndpoint
  79. dst.Region = c.Region
  80. dst.DisableSSL = c.DisableSSL
  81. dst.ManualSend = c.ManualSend
  82. dst.HTTPClient = c.HTTPClient
  83. dst.LogHTTPBody = c.LogHTTPBody
  84. dst.LogLevel = c.LogLevel
  85. dst.Logger = c.Logger
  86. dst.MaxRetries = c.MaxRetries
  87. dst.RetryRule = c.RetryRule
  88. dst.ShouldRetry = c.ShouldRetry
  89. dst.DisableParamValidation = c.DisableParamValidation
  90. dst.DisableComputeChecksums = c.DisableComputeChecksums
  91. dst.S3ForcePathStyle = c.S3ForcePathStyle
  92. dst.DomainMode = c.DomainMode
  93. dst.SignerVersion = c.SignerVersion
  94. dst.CrcCheckEnabled = c.CrcCheckEnabled
  95. dst.DisableRestProtocolURICleaning = c.DisableRestProtocolURICleaning
  96. dst.DisableDnsCache = c.DisableDnsCache
  97. return dst
  98. }
  99. // Merge merges the newcfg attribute values into this Config. Each attribute
  100. // will be merged into this config if the newcfg attribute's value is non-zero.
  101. // Due to this, newcfg attributes with zero values cannot be merged in. For
  102. // example bool attributes cannot be cleared using Merge, and must be explicitly
  103. // set on the Config structure.
  104. func (c Config) Merge(newcfg *Config) *Config {
  105. if newcfg == nil {
  106. return &c
  107. }
  108. cfg := Config{}
  109. if newcfg.Credentials != nil {
  110. cfg.Credentials = newcfg.Credentials
  111. } else {
  112. cfg.Credentials = c.Credentials
  113. }
  114. if newcfg.Endpoint != "" {
  115. cfg.Endpoint = newcfg.Endpoint
  116. } else {
  117. cfg.Endpoint = c.Endpoint
  118. }
  119. if newcfg.Ks3BillEndpoint != "" {
  120. cfg.Ks3BillEndpoint = newcfg.Ks3BillEndpoint
  121. } else {
  122. cfg.Ks3BillEndpoint = c.Ks3BillEndpoint
  123. }
  124. if newcfg.Region != "" {
  125. cfg.Region = newcfg.Region
  126. } else {
  127. cfg.Region = c.Region
  128. }
  129. if newcfg.DisableSSL {
  130. cfg.DisableSSL = newcfg.DisableSSL
  131. } else {
  132. cfg.DisableSSL = c.DisableSSL
  133. }
  134. if newcfg.ManualSend {
  135. cfg.ManualSend = newcfg.ManualSend
  136. } else {
  137. cfg.ManualSend = c.ManualSend
  138. }
  139. if newcfg.DisableDnsCache {
  140. cfg.DisableDnsCache = newcfg.DisableDnsCache
  141. } else {
  142. cfg.DisableDnsCache = c.DisableDnsCache
  143. }
  144. if newcfg.HTTPClient != nil {
  145. cfg.HTTPClient = newcfg.HTTPClient
  146. } else {
  147. cfg.HTTPClient = c.HTTPClient
  148. if !cfg.DisableDnsCache {
  149. cfg.HTTPClient.Transport = DnsCacheTransport
  150. }
  151. }
  152. defaultHTTPRedirect(cfg.HTTPClient)
  153. if newcfg.LogHTTPBody {
  154. cfg.LogHTTPBody = newcfg.LogHTTPBody
  155. } else {
  156. cfg.LogHTTPBody = c.LogHTTPBody
  157. }
  158. if newcfg.LogLevel != 0 {
  159. cfg.LogLevel = newcfg.LogLevel
  160. } else {
  161. cfg.LogLevel = c.LogLevel
  162. }
  163. if newcfg.Logger != nil {
  164. cfg.Logger = newcfg.Logger
  165. } else {
  166. cfg.Logger = c.Logger
  167. }
  168. if newcfg.MaxRetries != 0 {
  169. cfg.MaxRetries = newcfg.MaxRetries
  170. } else {
  171. cfg.MaxRetries = c.MaxRetries
  172. }
  173. if newcfg.RetryRule != nil {
  174. cfg.RetryRule = newcfg.RetryRule
  175. } else {
  176. cfg.RetryRule = c.RetryRule
  177. }
  178. if newcfg.ShouldRetry != nil {
  179. cfg.ShouldRetry = newcfg.ShouldRetry
  180. } else {
  181. cfg.ShouldRetry = c.ShouldRetry
  182. }
  183. if newcfg.DisableParamValidation {
  184. cfg.DisableParamValidation = newcfg.DisableParamValidation
  185. } else {
  186. cfg.DisableParamValidation = c.DisableParamValidation
  187. }
  188. if newcfg.DisableComputeChecksums {
  189. cfg.DisableComputeChecksums = newcfg.DisableComputeChecksums
  190. } else {
  191. cfg.DisableComputeChecksums = c.DisableComputeChecksums
  192. }
  193. if newcfg.S3ForcePathStyle {
  194. cfg.S3ForcePathStyle = newcfg.S3ForcePathStyle
  195. } else {
  196. cfg.S3ForcePathStyle = c.S3ForcePathStyle
  197. }
  198. if newcfg.DomainMode {
  199. cfg.DomainMode = newcfg.DomainMode
  200. } else {
  201. cfg.DomainMode = c.DomainMode
  202. }
  203. if newcfg.SignerVersion != "" {
  204. cfg.SignerVersion = newcfg.SignerVersion
  205. } else {
  206. cfg.SignerVersion = c.SignerVersion
  207. }
  208. if newcfg.CrcCheckEnabled {
  209. cfg.CrcCheckEnabled = newcfg.CrcCheckEnabled
  210. } else {
  211. cfg.CrcCheckEnabled = c.CrcCheckEnabled
  212. }
  213. if newcfg.DisableRestProtocolURICleaning {
  214. cfg.DisableRestProtocolURICleaning = newcfg.DisableRestProtocolURICleaning
  215. } else {
  216. cfg.DisableRestProtocolURICleaning = c.DisableRestProtocolURICleaning
  217. }
  218. return &cfg
  219. }
  220. // Define the level of the output log
  221. const (
  222. Off = iota
  223. Error
  224. Warn
  225. Info
  226. Debug
  227. )
  228. // LogTag Tag for each level of log
  229. var LogTag = []string{"[error]", "[warn]", "[info]", "[debug]"}
  230. func (c Config) writeLog(level uint, format string, a ...interface{}) {
  231. if c.LogLevel < level {
  232. return
  233. }
  234. var logBuffer bytes.Buffer
  235. logBuffer.WriteString(time.Now().Format("2006/01/02 15:04:05"))
  236. logBuffer.WriteString(" ")
  237. logBuffer.WriteString(LogTag[level-1])
  238. logBuffer.WriteString(fmt.Sprintf(format, a...))
  239. if logBuffer.Bytes()[logBuffer.Len()-1] != '\n' {
  240. logBuffer.WriteString("\n")
  241. }
  242. fmt.Fprintf(c.Logger, "%s", logBuffer.String())
  243. }
  244. func (c Config) LogError(format string, a ...interface{}) {
  245. if c.LogLevel < Error {
  246. return
  247. }
  248. c.writeLog(Error, format, a...)
  249. }
  250. func (c Config) LogWarn(format string, a ...interface{}) {
  251. if c.LogLevel < Warn {
  252. return
  253. }
  254. c.writeLog(Warn, format, a...)
  255. }
  256. func (c Config) LogInfo(format string, a ...interface{}) {
  257. if c.LogLevel < Info {
  258. return
  259. }
  260. c.writeLog(Info, format, a...)
  261. }
  262. func (c Config) LogDebug(format string, a ...interface{}) {
  263. if c.LogLevel < Debug {
  264. return
  265. }
  266. c.writeLog(Debug, format, a...)
  267. }