conf.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package oss
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "os"
  7. "time"
  8. )
  9. const (
  10. LogOff = iota
  11. Error
  12. Warn
  13. Info
  14. Debug
  15. )
  16. var LogTag = []string{"[error]", "[warn]", "[info]", "[debug]"}
  17. // HTTPTimeout defines HTTP timeout.
  18. type HTTPTimeout struct {
  19. ConnectTimeout time.Duration
  20. ReadWriteTimeout time.Duration
  21. HeaderTimeout time.Duration
  22. LongTimeout time.Duration
  23. IdleConnTimeout time.Duration
  24. }
  25. type HTTPMaxConns struct {
  26. MaxIdleConns int
  27. MaxIdleConnsPerHost int
  28. }
  29. // Config defines oss configuration
  30. type Config struct {
  31. Endpoint string // OSS endpoint
  32. AccessKeyID string // AccessId
  33. AccessKeySecret string // AccessKey
  34. RetryTimes uint // Retry count by default it's 5.
  35. UserAgent string // SDK name/version/system information
  36. IsDebug bool // Enable debug mode. Default is false.
  37. Timeout uint // Timeout in seconds. By default it's 60.
  38. SecurityToken string // STS Token
  39. IsCname bool // If cname is in the endpoint.
  40. HTTPTimeout HTTPTimeout // HTTP timeout
  41. HTTPMaxConns HTTPMaxConns // Http max connections
  42. IsUseProxy bool // Flag of using proxy.
  43. ProxyHost string // Flag of using proxy host.
  44. IsAuthProxy bool // Flag of needing authentication.
  45. ProxyUser string // Proxy user
  46. ProxyPassword string // Proxy password
  47. IsEnableMD5 bool // Flag of enabling MD5 for upload.
  48. MD5Threshold int64 // Memory footprint threshold for each MD5 computation (16MB is the default), in byte. When the data is more than that, temp file is used.
  49. IsEnableCRC bool // Flag of enabling CRC for upload.
  50. LogLevel int // log level
  51. Logger *log.Logger // For write log
  52. }
  53. // WriteLog
  54. func (config *Config) WriteLog(LogLevel int, format string, a ...interface{}) {
  55. if config.LogLevel < LogLevel || config.Logger == nil {
  56. return
  57. }
  58. var logBuffer bytes.Buffer
  59. logBuffer.WriteString(LogTag[LogLevel-1])
  60. logBuffer.WriteString(fmt.Sprintf(format, a...))
  61. config.Logger.Printf("%s", logBuffer.String())
  62. }
  63. // getDefaultOssConfig gets the default configuration.
  64. func getDefaultOssConfig() *Config {
  65. config := Config{}
  66. config.Endpoint = ""
  67. config.AccessKeyID = ""
  68. config.AccessKeySecret = ""
  69. config.RetryTimes = 5
  70. config.IsDebug = false
  71. config.UserAgent = userAgent()
  72. config.Timeout = 60 // Seconds
  73. config.SecurityToken = ""
  74. config.IsCname = false
  75. config.HTTPTimeout.ConnectTimeout = time.Second * 30 // 30s
  76. config.HTTPTimeout.ReadWriteTimeout = time.Second * 60 // 60s
  77. config.HTTPTimeout.HeaderTimeout = time.Second * 60 // 60s
  78. config.HTTPTimeout.LongTimeout = time.Second * 300 // 300s
  79. config.HTTPTimeout.IdleConnTimeout = time.Second * 50 // 50s
  80. config.HTTPMaxConns.MaxIdleConns = 100
  81. config.HTTPMaxConns.MaxIdleConnsPerHost = 100
  82. config.IsUseProxy = false
  83. config.ProxyHost = ""
  84. config.IsAuthProxy = false
  85. config.ProxyUser = ""
  86. config.ProxyPassword = ""
  87. config.MD5Threshold = 16 * 1024 * 1024 // 16MB
  88. config.IsEnableMD5 = false
  89. config.IsEnableCRC = true
  90. config.LogLevel = LogOff
  91. config.Logger = log.New(os.Stdout, "", log.LstdFlags)
  92. return &config
  93. }