conf.go 5.4 KB

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