conf.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  89. // LimitUploadSpeed uploadSpeed:KB/s, 0 is unlimited,default is 0
  90. func (config *Config) LimitUploadSpeed(uploadSpeed int) error {
  91. if uploadSpeed < 0 {
  92. return fmt.Errorf("invalid argument, the value of uploadSpeed is less than 0")
  93. } else if uploadSpeed == 0 {
  94. config.UploadLimitSpeed = 0
  95. config.UploadLimiter = nil
  96. return nil
  97. }
  98. var err error
  99. config.UploadLimiter, err = GetOssLimiter(uploadSpeed)
  100. if err == nil {
  101. config.UploadLimitSpeed = uploadSpeed
  102. }
  103. return err
  104. }
  105. // WriteLog output log function
  106. func (config *Config) WriteLog(LogLevel int, format string, a ...interface{}) {
  107. if config.LogLevel < LogLevel || config.Logger == nil {
  108. return
  109. }
  110. var logBuffer bytes.Buffer
  111. logBuffer.WriteString(LogTag[LogLevel-1])
  112. logBuffer.WriteString(fmt.Sprintf(format, a...))
  113. config.Logger.Printf("%s", logBuffer.String())
  114. }
  115. // for get Credentials
  116. func (config *Config) GetCredentials() Credentials {
  117. return config.CredentialsProvider.GetCredentials()
  118. }
  119. // getDefaultOssConfig gets the default configuration.
  120. func getDefaultOssConfig() *Config {
  121. config := Config{}
  122. config.Endpoint = ""
  123. config.AccessKeyID = ""
  124. config.AccessKeySecret = ""
  125. config.RetryTimes = 5
  126. config.IsDebug = false
  127. config.UserAgent = userAgent()
  128. config.Timeout = 60 // Seconds
  129. config.SecurityToken = ""
  130. config.IsCname = false
  131. config.HTTPTimeout.ConnectTimeout = time.Second * 30 // 30s
  132. config.HTTPTimeout.ReadWriteTimeout = time.Second * 60 // 60s
  133. config.HTTPTimeout.HeaderTimeout = time.Second * 60 // 60s
  134. config.HTTPTimeout.LongTimeout = time.Second * 300 // 300s
  135. config.HTTPTimeout.IdleConnTimeout = time.Second * 50 // 50s
  136. config.HTTPMaxConns.MaxIdleConns = 100
  137. config.HTTPMaxConns.MaxIdleConnsPerHost = 100
  138. config.IsUseProxy = false
  139. config.ProxyHost = ""
  140. config.IsAuthProxy = false
  141. config.ProxyUser = ""
  142. config.ProxyPassword = ""
  143. config.MD5Threshold = 16 * 1024 * 1024 // 16MB
  144. config.IsEnableMD5 = false
  145. config.IsEnableCRC = true
  146. config.LogLevel = LogOff
  147. config.Logger = log.New(os.Stdout, "", log.LstdFlags)
  148. provider := &defaultCredentialsProvider{config: &config}
  149. config.CredentialsProvider = provider
  150. return &config
  151. }