OSDN Git Service

Create ossClient.go (#574)
[bytom/vapor.git] / vendor / github.com / aliyun / aliyun-oss-go-sdk / oss / conf.go
1 package oss
2
3 import (
4         "bytes"
5         "fmt"
6         "log"
7         "net"
8         "os"
9         "time"
10 )
11
12 // Define the level of the output log
13 const (
14         LogOff = iota
15         Error
16         Warn
17         Info
18         Debug
19 )
20
21 // LogTag Tag for each level of log
22 var LogTag = []string{"[error]", "[warn]", "[info]", "[debug]"}
23
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
31 }
32
33 // HTTPMaxConns defines max idle connections and max idle connections per host
34 type HTTPMaxConns struct {
35         MaxIdleConns        int
36         MaxIdleConnsPerHost int
37 }
38
39 // CredentialInf is interface for get AccessKeyID,AccessKeySecret,SecurityToken
40 type Credentials interface {
41         GetAccessKeyID() string
42         GetAccessKeySecret() string
43         GetSecurityToken() string
44 }
45
46 // CredentialInfBuild is interface for get CredentialInf
47 type CredentialsProvider interface {
48         GetCredentials() Credentials
49 }
50
51 type defaultCredentials struct {
52         config *Config
53 }
54
55 func (defCre *defaultCredentials) GetAccessKeyID() string {
56         return defCre.config.AccessKeyID
57 }
58
59 func (defCre *defaultCredentials) GetAccessKeySecret() string {
60         return defCre.config.AccessKeySecret
61 }
62
63 func (defCre *defaultCredentials) GetSecurityToken() string {
64         return defCre.config.SecurityToken
65 }
66
67 type defaultCredentialsProvider struct {
68         config *Config
69 }
70
71 func (defBuild *defaultCredentialsProvider) GetCredentials() Credentials {
72         return &defaultCredentials{config: defBuild.config}
73 }
74
75 // Config defines oss configuration
76 type Config struct {
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
106 }
107
108 // LimitUploadSpeed uploadSpeed:KB/s, 0 is unlimited,default is 0
109 func (config *Config) LimitUploadSpeed(uploadSpeed int) error {
110         if uploadSpeed < 0 {
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
115                 return nil
116         }
117
118         var err error
119         config.UploadLimiter, err = GetOssLimiter(uploadSpeed)
120         if err == nil {
121                 config.UploadLimitSpeed = uploadSpeed
122         }
123         return err
124 }
125
126 // WriteLog output log function
127 func (config *Config) WriteLog(LogLevel int, format string, a ...interface{}) {
128         if config.LogLevel < LogLevel || config.Logger == nil {
129                 return
130         }
131
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())
136 }
137
138 // for get Credentials
139 func (config *Config) GetCredentials() Credentials {
140         return config.CredentialsProvider.GetCredentials()
141 }
142
143 // getDefaultOssConfig gets the default configuration.
144 func getDefaultOssConfig() *Config {
145         config := Config{}
146
147         config.Endpoint = ""
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
156
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
164
165         config.IsUseProxy = false
166         config.ProxyHost = ""
167         config.IsAuthProxy = false
168         config.ProxyUser = ""
169         config.ProxyPassword = ""
170
171         config.MD5Threshold = 16 * 1024 * 1024 // 16MB
172         config.IsEnableMD5 = false
173         config.IsEnableCRC = true
174
175         config.LogLevel = LogOff
176         config.Logger = log.New(os.Stdout, "", log.LstdFlags)
177
178         provider := &defaultCredentialsProvider{config: &config}
179         config.CredentialsProvider = provider
180
181         config.AuthVersion = AuthV1
182         config.RedirectEnabled = true
183
184         return &config
185 }