conf.go 5.5 KB

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