12 // Define the level of the output log
21 // LogTag Tag for each level of log
22 var LogTag = []string{"[error]", "[warn]", "[info]", "[debug]"}
24 // HTTPTimeout defines HTTP timeout.
25 type HTTPTimeout struct {
26 ConnectTimeout time.Duration
27 ReadWriteTimeout time.Duration
28 HeaderTimeout time.Duration
29 LongTimeout time.Duration
30 IdleConnTimeout time.Duration
33 // HTTPMaxConns defines max idle connections and max idle connections per host
34 type HTTPMaxConns struct {
36 MaxIdleConnsPerHost int
39 // CredentialInf is interface for get AccessKeyID,AccessKeySecret,SecurityToken
40 type Credentials interface {
41 GetAccessKeyID() string
42 GetAccessKeySecret() string
43 GetSecurityToken() string
46 // CredentialInfBuild is interface for get CredentialInf
47 type CredentialsProvider interface {
48 GetCredentials() Credentials
51 type defaultCredentials struct {
55 func (defCre *defaultCredentials) GetAccessKeyID() string {
56 return defCre.config.AccessKeyID
59 func (defCre *defaultCredentials) GetAccessKeySecret() string {
60 return defCre.config.AccessKeySecret
63 func (defCre *defaultCredentials) GetSecurityToken() string {
64 return defCre.config.SecurityToken
67 type defaultCredentialsProvider struct {
71 func (defBuild *defaultCredentialsProvider) GetCredentials() Credentials {
72 return &defaultCredentials{config: defBuild.config}
75 // Config defines oss configuration
77 Endpoint string // OSS endpoint
78 AccessKeyID string // AccessId
79 AccessKeySecret string // AccessKey
80 RetryTimes uint // Retry count by default it's 5.
81 UserAgent string // SDK name/version/system information
82 IsDebug bool // Enable debug mode. Default is false.
83 Timeout uint // Timeout in seconds. By default it's 60.
84 SecurityToken string // STS Token
85 IsCname bool // If cname is in the endpoint.
86 HTTPTimeout HTTPTimeout // HTTP timeout
87 HTTPMaxConns HTTPMaxConns // Http max connections
88 IsUseProxy bool // Flag of using proxy.
89 ProxyHost string // Flag of using proxy host.
90 IsAuthProxy bool // Flag of needing authentication.
91 ProxyUser string // Proxy user
92 ProxyPassword string // Proxy password
93 IsEnableMD5 bool // Flag of enabling MD5 for upload.
94 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.
95 IsEnableCRC bool // Flag of enabling CRC for upload.
96 LogLevel int // Log level
97 Logger *log.Logger // For write log
98 UploadLimitSpeed int // Upload limit speed:KB/s, 0 is unlimited
99 UploadLimiter *OssLimiter // Bandwidth limit reader for upload
100 CredentialsProvider CredentialsProvider // User provides interface to get AccessKeyID, AccessKeySecret, SecurityToken
101 LocalAddr net.Addr // local client host info
102 UserSetUa bool // UserAgent is set by user or not
103 AuthVersion AuthVersionType // v1 or v2 signature,default is v1
104 AdditionalHeaders []string // special http headers needed to be sign
105 RedirectEnabled bool // only effective from go1.7 onward, enable http redirect or not
108 // LimitUploadSpeed uploadSpeed:KB/s, 0 is unlimited,default is 0
109 func (config *Config) LimitUploadSpeed(uploadSpeed int) error {
111 return fmt.Errorf("invalid argument, the value of uploadSpeed is less than 0")
112 } else if uploadSpeed == 0 {
113 config.UploadLimitSpeed = 0
114 config.UploadLimiter = nil
119 config.UploadLimiter, err = GetOssLimiter(uploadSpeed)
121 config.UploadLimitSpeed = uploadSpeed
126 // WriteLog output log function
127 func (config *Config) WriteLog(LogLevel int, format string, a ...interface{}) {
128 if config.LogLevel < LogLevel || config.Logger == nil {
132 var logBuffer bytes.Buffer
133 logBuffer.WriteString(LogTag[LogLevel-1])
134 logBuffer.WriteString(fmt.Sprintf(format, a...))
135 config.Logger.Printf("%s", logBuffer.String())
138 // for get Credentials
139 func (config *Config) GetCredentials() Credentials {
140 return config.CredentialsProvider.GetCredentials()
143 // getDefaultOssConfig gets the default configuration.
144 func getDefaultOssConfig() *Config {
148 config.AccessKeyID = ""
149 config.AccessKeySecret = ""
150 config.RetryTimes = 5
151 config.IsDebug = false
152 config.UserAgent = userAgent()
153 config.Timeout = 60 // Seconds
154 config.SecurityToken = ""
155 config.IsCname = false
157 config.HTTPTimeout.ConnectTimeout = time.Second * 30 // 30s
158 config.HTTPTimeout.ReadWriteTimeout = time.Second * 60 // 60s
159 config.HTTPTimeout.HeaderTimeout = time.Second * 60 // 60s
160 config.HTTPTimeout.LongTimeout = time.Second * 300 // 300s
161 config.HTTPTimeout.IdleConnTimeout = time.Second * 50 // 50s
162 config.HTTPMaxConns.MaxIdleConns = 100
163 config.HTTPMaxConns.MaxIdleConnsPerHost = 100
165 config.IsUseProxy = false
166 config.ProxyHost = ""
167 config.IsAuthProxy = false
168 config.ProxyUser = ""
169 config.ProxyPassword = ""
171 config.MD5Threshold = 16 * 1024 * 1024 // 16MB
172 config.IsEnableMD5 = false
173 config.IsEnableCRC = true
175 config.LogLevel = LogOff
176 config.Logger = log.New(os.Stdout, "", log.LstdFlags)
178 provider := &defaultCredentialsProvider{config: &config}
179 config.CredentialsProvider = provider
181 config.AuthVersion = AuthV1
182 config.RedirectEnabled = true