config.go 15 KB

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