conf.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package oss
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "os"
  7. "time"
  8. )
  9. // Define the level of the output log
  10. const (
  11. LogOff = iota
  12. Error
  13. Warn
  14. Info
  15. Debug
  16. )
  17. // LogTag Tag for each level of log
  18. var LogTag = []string{"[error]", "[warn]", "[info]", "[debug]"}
  19. // HTTPTimeout defines HTTP timeout.
  20. type HTTPTimeout struct {
  21. ConnectTimeout time.Duration
  22. ReadWriteTimeout time.Duration
  23. HeaderTimeout time.Duration
  24. LongTimeout time.Duration
  25. IdleConnTimeout time.Duration
  26. }
  27. // HTTPMaxConns defines max idle connections and max idle connections per host
  28. type HTTPMaxConns struct {
  29. MaxIdleConns int
  30. MaxIdleConnsPerHost int
  31. }
  32. // Config defines oss configuration
  33. type Config struct {
  34. Endpoint string // OSS endpoint
  35. AccessKeyID string // AccessId
  36. AccessKeySecret string // AccessKey
  37. RetryTimes uint // Retry count by default it's 5.
  38. UserAgent string // SDK name/version/system information
  39. IsDebug bool // Enable debug mode. Default is false.
  40. Timeout uint // Timeout in seconds. By default it's 60.
  41. SecurityToken string // STS Token
  42. IsCname bool // If cname is in the endpoint.
  43. HTTPTimeout HTTPTimeout // HTTP timeout
  44. HTTPMaxConns HTTPMaxConns // Http max connections
  45. IsUseProxy bool // Flag of using proxy.
  46. ProxyHost string // Flag of using proxy host.
  47. IsAuthProxy bool // Flag of needing authentication.
  48. ProxyUser string // Proxy user
  49. ProxyPassword string // Proxy password
  50. IsEnableMD5 bool // Flag of enabling MD5 for upload.
  51. 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.
  52. IsEnableCRC bool // Flag of enabling CRC for upload.
  53. LogLevel int // Log level
  54. Logger *log.Logger // For write log
  55. UploadLimitSpeed int // Upload limit speed:KB/s, 0 is unlimited
  56. UploadLimiter *OssLimiter // Bandwidth limit reader for upload
  57. }
  58. // LimitUploadSpeed uploadSpeed:KB/s, 0 is unlimited,default is 0
  59. func (config *Config) LimitUploadSpeed(uploadSpeed int) error {
  60. if uploadSpeed < 0 {
  61. return fmt.Errorf("invalid argument, the value of uploadSpeed is less than 0")
  62. } else if uploadSpeed == 0 {
  63. config.UploadLimitSpeed = 0
  64. config.UploadLimiter = nil
  65. return nil
  66. }
  67. var err error
  68. config.UploadLimiter, err = GetOssLimiter(uploadSpeed)
  69. if err == nil {
  70. config.UploadLimitSpeed = uploadSpeed
  71. }
  72. return err
  73. }
  74. // WriteLog output log function
  75. func (config *Config) WriteLog(LogLevel int, format string, a ...interface{}) {
  76. if config.LogLevel < LogLevel || config.Logger == nil {
  77. return
  78. }
  79. var logBuffer bytes.Buffer
  80. logBuffer.WriteString(LogTag[LogLevel-1])
  81. logBuffer.WriteString(fmt.Sprintf(format, a...))
  82. config.Logger.Printf("%s", logBuffer.String())
  83. }
  84. // getDefaultOssConfig gets the default configuration.
  85. func getDefaultOssConfig() *Config {
  86. config := Config{}
  87. config.Endpoint = ""
  88. config.AccessKeyID = ""
  89. config.AccessKeySecret = ""
  90. config.RetryTimes = 5
  91. config.IsDebug = false
  92. config.UserAgent = userAgent()
  93. config.Timeout = 60 // Seconds
  94. config.SecurityToken = ""
  95. config.IsCname = false
  96. config.HTTPTimeout.ConnectTimeout = time.Second * 30 // 30s
  97. config.HTTPTimeout.ReadWriteTimeout = time.Second * 60 // 60s
  98. config.HTTPTimeout.HeaderTimeout = time.Second * 60 // 60s
  99. config.HTTPTimeout.LongTimeout = time.Second * 300 // 300s
  100. config.HTTPTimeout.IdleConnTimeout = time.Second * 50 // 50s
  101. config.HTTPMaxConns.MaxIdleConns = 100
  102. config.HTTPMaxConns.MaxIdleConnsPerHost = 100
  103. config.IsUseProxy = false
  104. config.ProxyHost = ""
  105. config.IsAuthProxy = false
  106. config.ProxyUser = ""
  107. config.ProxyPassword = ""
  108. config.MD5Threshold = 16 * 1024 * 1024 // 16MB
  109. config.IsEnableMD5 = false
  110. config.IsEnableCRC = true
  111. config.LogLevel = LogOff
  112. config.Logger = log.New(os.Stdout, "", log.LstdFlags)
  113. return &config
  114. }