config.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. package aws
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/aws/aws-sdk-go/aws/credentials"
  6. )
  7. // UseServiceDefaultRetries instructs the config to use the service's own
  8. // default number of retries. This will be the default action if
  9. // Config.MaxRetries is nil also.
  10. const UseServiceDefaultRetries = -1
  11. // RequestRetryer is an alias for a type that implements the request.Retryer
  12. // interface.
  13. type RequestRetryer interface{}
  14. // A Config provides service configuration for service clients. By default,
  15. // all clients will use the defaults.DefaultConfig tructure.
  16. //
  17. // // Create Session with MaxRetry configuration to be shared by multiple
  18. // // service clients.
  19. // sess, err := session.NewSession(&aws.Config{
  20. // MaxRetries: aws.Int(3),
  21. // })
  22. //
  23. // // Create S3 service client with a specific Region.
  24. // svc := s3.New(sess, &aws.Config{
  25. // Region: aws.String("us-west-2"),
  26. // })
  27. type Config struct {
  28. // Enables verbose error printing of all credential chain errors.
  29. // Should be used when wanting to see all errors while attempting to
  30. // retrieve credentials.
  31. CredentialsChainVerboseErrors *bool
  32. // The credentials object to use when signing requests. Defaults to a
  33. // chain of credential providers to search for credentials in environment
  34. // variables, shared credential file, and EC2 Instance Roles.
  35. Credentials *credentials.Credentials
  36. // An optional endpoint URL (hostname only or fully qualified URI)
  37. // that overrides the default generated endpoint for a client. Set this
  38. // to `""` to use the default generated endpoint.
  39. //
  40. // @note You must still provide a `Region` value when specifying an
  41. // endpoint for a client.
  42. Endpoint *string
  43. // The region to send requests to. This parameter is required and must
  44. // be configured globally or on a per-client basis unless otherwise
  45. // noted. A full list of regions is found in the "Regions and Endpoints"
  46. // document.
  47. //
  48. // @see http://docs.aws.amazon.com/general/latest/gr/rande.html
  49. // AWS Regions and Endpoints
  50. Region *string
  51. // Set this to `true` to disable SSL when sending requests. Defaults
  52. // to `false`.
  53. DisableSSL *bool
  54. // The HTTP client to use when sending requests. Defaults to
  55. // `http.DefaultClient`.
  56. HTTPClient *http.Client
  57. // An integer value representing the logging level. The default log level
  58. // is zero (LogOff), which represents no logging. To enable logging set
  59. // to a LogLevel Value.
  60. LogLevel *LogLevelType
  61. // The logger writer interface to write logging messages to. Defaults to
  62. // standard out.
  63. Logger Logger
  64. // The maximum number of times that a request will be retried for failures.
  65. // Defaults to -1, which defers the max retry setting to the service
  66. // specific configuration.
  67. MaxRetries *int
  68. // Retryer guides how HTTP requests should be retried in case of
  69. // recoverable failures.
  70. //
  71. // When nil or the value does not implement the request.Retryer interface,
  72. // the request.DefaultRetryer will be used.
  73. //
  74. // When both Retryer and MaxRetries are non-nil, the former is used and
  75. // the latter ignored.
  76. //
  77. // To set the Retryer field in a type-safe manner and with chaining, use
  78. // the request.WithRetryer helper function:
  79. //
  80. // cfg := request.WithRetryer(aws.NewConfig(), myRetryer)
  81. //
  82. Retryer RequestRetryer
  83. // Disables semantic parameter validation, which validates input for
  84. // missing required fields and/or other semantic request input errors.
  85. DisableParamValidation *bool
  86. // Disables the computation of request and response checksums, e.g.,
  87. // CRC32 checksums in Amazon DynamoDB.
  88. DisableComputeChecksums *bool
  89. // Set this to `true` to force the request to use path-style addressing,
  90. // i.e., `http://s3.amazonaws.com/BUCKET/KEY`. By default, the S3 client
  91. // will use virtual hosted bucket addressing when possible
  92. // (`http://BUCKET.s3.amazonaws.com/KEY`).
  93. //
  94. // @note This configuration option is specific to the Amazon S3 service.
  95. // @see http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
  96. // Amazon S3: Virtual Hosting of Buckets
  97. S3ForcePathStyle *bool
  98. // Set this to `true` to disable the SDK adding the `Expect: 100-Continue`
  99. // header to PUT requests over 2MB of content. 100-Continue instructs the
  100. // HTTP client not to send the body until the service responds with a
  101. // `continue` status. This is useful to prevent sending the request body
  102. // until after the request is authenticated, and validated.
  103. //
  104. // http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
  105. //
  106. // 100-Continue is only enabled for Go 1.6 and above. See `http.Transport`'s
  107. // `ExpectContinueTimeout` for information on adjusting the continue wait
  108. // timeout. https://golang.org/pkg/net/http/#Transport
  109. //
  110. // You should use this flag to disble 100-Continue if you experience issues
  111. // with proxies or third party S3 compatible services.
  112. S3Disable100Continue *bool
  113. // Set this to `true` to enable S3 Accelerate feature. For all operations
  114. // compatible with S3 Accelerate will use the accelerate endpoint for
  115. // requests. Requests not compatible will fall back to normal S3 requests.
  116. //
  117. // The bucket must be enable for accelerate to be used with S3 client with
  118. // accelerate enabled. If the bucket is not enabled for accelerate an error
  119. // will be returned. The bucket name must be DNS compatible to also work
  120. // with accelerate.
  121. S3UseAccelerate *bool
  122. // Set this to `true` to disable the EC2Metadata client from overriding the
  123. // default http.Client's Timeout. This is helpful if you do not want the
  124. // EC2Metadata client to create a new http.Client. This options is only
  125. // meaningful if you're not already using a custom HTTP client with the
  126. // SDK. Enabled by default.
  127. //
  128. // Must be set and provided to the session.NewSession() in order to disable
  129. // the EC2Metadata overriding the timeout for default credentials chain.
  130. //
  131. // Example:
  132. // sess, err := session.NewSession(aws.NewConfig().WithEC2MetadataDiableTimeoutOverride(true))
  133. //
  134. // svc := s3.New(sess)
  135. //
  136. EC2MetadataDisableTimeoutOverride *bool
  137. // Instructs the endpiont to be generated for a service client to
  138. // be the dual stack endpoint. The dual stack endpoint will support
  139. // both IPv4 and IPv6 addressing.
  140. //
  141. // Setting this for a service which does not support dual stack will fail
  142. // to make requets. It is not recommended to set this value on the session
  143. // as it will apply to all service clients created with the session. Even
  144. // services which don't support dual stack endpoints.
  145. //
  146. // If the Endpoint config value is also provided the UseDualStack flag
  147. // will be ignored.
  148. //
  149. // Only supported with.
  150. //
  151. // sess, err := session.NewSession()
  152. //
  153. // svc := s3.New(sess, &aws.Config{
  154. // UseDualStack: aws.Bool(true),
  155. // })
  156. UseDualStack *bool
  157. // SleepDelay is an override for the func the SDK will call when sleeping
  158. // during the lifecycle of a request. Specifically this will be used for
  159. // request delays. This value should only be used for testing. To adjust
  160. // the delay of a request see the aws/client.DefaultRetryer and
  161. // aws/request.Retryer.
  162. SleepDelay func(time.Duration)
  163. // DisableRestProtocolURICleaning will not clean the URL path when making rest protocol requests.
  164. // Will default to false. This would only be used for empty directory names in s3 requests.
  165. //
  166. // Example:
  167. // sess, err := session.NewSession(&aws.Config{DisableRestProtocolURICleaning: aws.Bool(true))
  168. //
  169. // svc := s3.New(sess)
  170. // out, err := svc.GetObject(&s3.GetObjectInput {
  171. // Bucket: aws.String("bucketname"),
  172. // Key: aws.String("//foo//bar//moo"),
  173. // })
  174. DisableRestProtocolURICleaning *bool
  175. }
  176. // NewConfig returns a new Config pointer that can be chained with builder
  177. // methods to set multiple configuration values inline without using pointers.
  178. //
  179. // // Create Session with MaxRetry configuration to be shared by multiple
  180. // // service clients.
  181. // sess, err := session.NewSession(aws.NewConfig().
  182. // WithMaxRetries(3),
  183. // )
  184. //
  185. // // Create S3 service client with a specific Region.
  186. // svc := s3.New(sess, aws.NewConfig().
  187. // WithRegion("us-west-2"),
  188. // )
  189. func NewConfig() *Config {
  190. return &Config{}
  191. }
  192. // WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
  193. // a Config pointer.
  194. func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
  195. c.CredentialsChainVerboseErrors = &verboseErrs
  196. return c
  197. }
  198. // WithCredentials sets a config Credentials value returning a Config pointer
  199. // for chaining.
  200. func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
  201. c.Credentials = creds
  202. return c
  203. }
  204. // WithEndpoint sets a config Endpoint value returning a Config pointer for
  205. // chaining.
  206. func (c *Config) WithEndpoint(endpoint string) *Config {
  207. c.Endpoint = &endpoint
  208. return c
  209. }
  210. // WithRegion sets a config Region value returning a Config pointer for
  211. // chaining.
  212. func (c *Config) WithRegion(region string) *Config {
  213. c.Region = &region
  214. return c
  215. }
  216. // WithDisableSSL sets a config DisableSSL value returning a Config pointer
  217. // for chaining.
  218. func (c *Config) WithDisableSSL(disable bool) *Config {
  219. c.DisableSSL = &disable
  220. return c
  221. }
  222. // WithHTTPClient sets a config HTTPClient value returning a Config pointer
  223. // for chaining.
  224. func (c *Config) WithHTTPClient(client *http.Client) *Config {
  225. c.HTTPClient = client
  226. return c
  227. }
  228. // WithMaxRetries sets a config MaxRetries value returning a Config pointer
  229. // for chaining.
  230. func (c *Config) WithMaxRetries(max int) *Config {
  231. c.MaxRetries = &max
  232. return c
  233. }
  234. // WithDisableParamValidation sets a config DisableParamValidation value
  235. // returning a Config pointer for chaining.
  236. func (c *Config) WithDisableParamValidation(disable bool) *Config {
  237. c.DisableParamValidation = &disable
  238. return c
  239. }
  240. // WithDisableComputeChecksums sets a config DisableComputeChecksums value
  241. // returning a Config pointer for chaining.
  242. func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
  243. c.DisableComputeChecksums = &disable
  244. return c
  245. }
  246. // WithLogLevel sets a config LogLevel value returning a Config pointer for
  247. // chaining.
  248. func (c *Config) WithLogLevel(level LogLevelType) *Config {
  249. c.LogLevel = &level
  250. return c
  251. }
  252. // WithLogger sets a config Logger value returning a Config pointer for
  253. // chaining.
  254. func (c *Config) WithLogger(logger Logger) *Config {
  255. c.Logger = logger
  256. return c
  257. }
  258. // WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
  259. // pointer for chaining.
  260. func (c *Config) WithS3ForcePathStyle(force bool) *Config {
  261. c.S3ForcePathStyle = &force
  262. return c
  263. }
  264. // WithS3Disable100Continue sets a config S3Disable100Continue value returning
  265. // a Config pointer for chaining.
  266. func (c *Config) WithS3Disable100Continue(disable bool) *Config {
  267. c.S3Disable100Continue = &disable
  268. return c
  269. }
  270. // WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
  271. // pointer for chaining.
  272. func (c *Config) WithS3UseAccelerate(enable bool) *Config {
  273. c.S3UseAccelerate = &enable
  274. return c
  275. }
  276. // WithUseDualStack sets a config UseDualStack value returning a Config
  277. // pointer for chaining.
  278. func (c *Config) WithUseDualStack(enable bool) *Config {
  279. c.UseDualStack = &enable
  280. return c
  281. }
  282. // WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
  283. // returning a Config pointer for chaining.
  284. func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
  285. c.EC2MetadataDisableTimeoutOverride = &enable
  286. return c
  287. }
  288. // WithSleepDelay overrides the function used to sleep while waiting for the
  289. // next retry. Defaults to time.Sleep.
  290. func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
  291. c.SleepDelay = fn
  292. return c
  293. }
  294. // MergeIn merges the passed in configs into the existing config object.
  295. func (c *Config) MergeIn(cfgs ...*Config) {
  296. for _, other := range cfgs {
  297. mergeInConfig(c, other)
  298. }
  299. }
  300. func mergeInConfig(dst *Config, other *Config) {
  301. if other == nil {
  302. return
  303. }
  304. if other.CredentialsChainVerboseErrors != nil {
  305. dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
  306. }
  307. if other.Credentials != nil {
  308. dst.Credentials = other.Credentials
  309. }
  310. if other.Endpoint != nil {
  311. dst.Endpoint = other.Endpoint
  312. }
  313. if other.Region != nil {
  314. dst.Region = other.Region
  315. }
  316. if other.DisableSSL != nil {
  317. dst.DisableSSL = other.DisableSSL
  318. }
  319. if other.HTTPClient != nil {
  320. dst.HTTPClient = other.HTTPClient
  321. }
  322. if other.LogLevel != nil {
  323. dst.LogLevel = other.LogLevel
  324. }
  325. if other.Logger != nil {
  326. dst.Logger = other.Logger
  327. }
  328. if other.MaxRetries != nil {
  329. dst.MaxRetries = other.MaxRetries
  330. }
  331. if other.Retryer != nil {
  332. dst.Retryer = other.Retryer
  333. }
  334. if other.DisableParamValidation != nil {
  335. dst.DisableParamValidation = other.DisableParamValidation
  336. }
  337. if other.DisableComputeChecksums != nil {
  338. dst.DisableComputeChecksums = other.DisableComputeChecksums
  339. }
  340. if other.S3ForcePathStyle != nil {
  341. dst.S3ForcePathStyle = other.S3ForcePathStyle
  342. }
  343. if other.S3Disable100Continue != nil {
  344. dst.S3Disable100Continue = other.S3Disable100Continue
  345. }
  346. if other.S3UseAccelerate != nil {
  347. dst.S3UseAccelerate = other.S3UseAccelerate
  348. }
  349. if other.UseDualStack != nil {
  350. dst.UseDualStack = other.UseDualStack
  351. }
  352. if other.EC2MetadataDisableTimeoutOverride != nil {
  353. dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
  354. }
  355. if other.SleepDelay != nil {
  356. dst.SleepDelay = other.SleepDelay
  357. }
  358. if other.DisableRestProtocolURICleaning != nil {
  359. dst.DisableRestProtocolURICleaning = other.DisableRestProtocolURICleaning
  360. }
  361. }
  362. // Copy will return a shallow copy of the Config object. If any additional
  363. // configurations are provided they will be merged into the new config returned.
  364. func (c *Config) Copy(cfgs ...*Config) *Config {
  365. dst := &Config{}
  366. dst.MergeIn(c)
  367. for _, cfg := range cfgs {
  368. dst.MergeIn(cfg)
  369. }
  370. return dst
  371. }