config.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. // S3DisableContentMD5Validation config option is temporarily disabled,
  133. // For S3 GetObject API calls, #1837.
  134. //
  135. // Set this to `true` to disable the S3 service client from automatically
  136. // adding the ContentMD5 to S3 Object Put and Upload API calls. This option
  137. // will also disable the SDK from performing object ContentMD5 validation
  138. // on GetObject API calls.
  139. S3DisableContentMD5Validation *bool
  140. // Set this to `true` to disable the EC2Metadata client from overriding the
  141. // default http.Client's Timeout. This is helpful if you do not want the
  142. // EC2Metadata client to create a new http.Client. This options is only
  143. // meaningful if you're not already using a custom HTTP client with the
  144. // SDK. Enabled by default.
  145. //
  146. // Must be set and provided to the session.NewSession() in order to disable
  147. // the EC2Metadata overriding the timeout for default credentials chain.
  148. //
  149. // Example:
  150. // sess := session.Must(session.NewSession(aws.NewConfig()
  151. // .WithEC2MetadataDiableTimeoutOverride(true)))
  152. //
  153. // svc := s3.New(sess)
  154. //
  155. EC2MetadataDisableTimeoutOverride *bool
  156. // Instructs the endpoint to be generated for a service client to
  157. // be the dual stack endpoint. The dual stack endpoint will support
  158. // both IPv4 and IPv6 addressing.
  159. //
  160. // Setting this for a service which does not support dual stack will fail
  161. // to make requets. It is not recommended to set this value on the session
  162. // as it will apply to all service clients created with the session. Even
  163. // services which don't support dual stack endpoints.
  164. //
  165. // If the Endpoint config value is also provided the UseDualStack flag
  166. // will be ignored.
  167. //
  168. // Only supported with.
  169. //
  170. // sess := session.Must(session.NewSession())
  171. //
  172. // svc := s3.New(sess, &aws.Config{
  173. // UseDualStack: aws.Bool(true),
  174. // })
  175. UseDualStack *bool
  176. // SleepDelay is an override for the func the SDK will call when sleeping
  177. // during the lifecycle of a request. Specifically this will be used for
  178. // request delays. This value should only be used for testing. To adjust
  179. // the delay of a request see the aws/client.DefaultRetryer and
  180. // aws/request.Retryer.
  181. //
  182. // SleepDelay will prevent any Context from being used for canceling retry
  183. // delay of an API operation. It is recommended to not use SleepDelay at all
  184. // and specify a Retryer instead.
  185. SleepDelay func(time.Duration)
  186. // DisableRestProtocolURICleaning will not clean the URL path when making rest protocol requests.
  187. // Will default to false. This would only be used for empty directory names in s3 requests.
  188. //
  189. // Example:
  190. // sess := session.Must(session.NewSession(&aws.Config{
  191. // DisableRestProtocolURICleaning: aws.Bool(true),
  192. // }))
  193. //
  194. // svc := s3.New(sess)
  195. // out, err := svc.GetObject(&s3.GetObjectInput {
  196. // Bucket: aws.String("bucketname"),
  197. // Key: aws.String("//foo//bar//moo"),
  198. // })
  199. DisableRestProtocolURICleaning *bool
  200. }
  201. // NewConfig returns a new Config pointer that can be chained with builder
  202. // methods to set multiple configuration values inline without using pointers.
  203. //
  204. // // Create Session with MaxRetry configuration to be shared by multiple
  205. // // service clients.
  206. // sess := session.Must(session.NewSession(aws.NewConfig().
  207. // WithMaxRetries(3),
  208. // ))
  209. //
  210. // // Create S3 service client with a specific Region.
  211. // svc := s3.New(sess, aws.NewConfig().
  212. // WithRegion("us-west-2"),
  213. // )
  214. func NewConfig() *Config {
  215. return &Config{}
  216. }
  217. // WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
  218. // a Config pointer.
  219. func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
  220. c.CredentialsChainVerboseErrors = &verboseErrs
  221. return c
  222. }
  223. // WithCredentials sets a config Credentials value returning a Config pointer
  224. // for chaining.
  225. func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
  226. c.Credentials = creds
  227. return c
  228. }
  229. // WithEndpoint sets a config Endpoint value returning a Config pointer for
  230. // chaining.
  231. func (c *Config) WithEndpoint(endpoint string) *Config {
  232. c.Endpoint = &endpoint
  233. return c
  234. }
  235. // WithEndpointResolver sets a config EndpointResolver value returning a
  236. // Config pointer for chaining.
  237. func (c *Config) WithEndpointResolver(resolver endpoints.Resolver) *Config {
  238. c.EndpointResolver = resolver
  239. return c
  240. }
  241. // WithRegion sets a config Region value returning a Config pointer for
  242. // chaining.
  243. func (c *Config) WithRegion(region string) *Config {
  244. c.Region = &region
  245. return c
  246. }
  247. // WithDisableSSL sets a config DisableSSL value returning a Config pointer
  248. // for chaining.
  249. func (c *Config) WithDisableSSL(disable bool) *Config {
  250. c.DisableSSL = &disable
  251. return c
  252. }
  253. // WithHTTPClient sets a config HTTPClient value returning a Config pointer
  254. // for chaining.
  255. func (c *Config) WithHTTPClient(client *http.Client) *Config {
  256. c.HTTPClient = client
  257. return c
  258. }
  259. // WithMaxRetries sets a config MaxRetries value returning a Config pointer
  260. // for chaining.
  261. func (c *Config) WithMaxRetries(max int) *Config {
  262. c.MaxRetries = &max
  263. return c
  264. }
  265. // WithDisableParamValidation sets a config DisableParamValidation value
  266. // returning a Config pointer for chaining.
  267. func (c *Config) WithDisableParamValidation(disable bool) *Config {
  268. c.DisableParamValidation = &disable
  269. return c
  270. }
  271. // WithDisableComputeChecksums sets a config DisableComputeChecksums value
  272. // returning a Config pointer for chaining.
  273. func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
  274. c.DisableComputeChecksums = &disable
  275. return c
  276. }
  277. // WithLogLevel sets a config LogLevel value returning a Config pointer for
  278. // chaining.
  279. func (c *Config) WithLogLevel(level LogLevelType) *Config {
  280. c.LogLevel = &level
  281. return c
  282. }
  283. // WithLogger sets a config Logger value returning a Config pointer for
  284. // chaining.
  285. func (c *Config) WithLogger(logger Logger) *Config {
  286. c.Logger = logger
  287. return c
  288. }
  289. // WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
  290. // pointer for chaining.
  291. func (c *Config) WithS3ForcePathStyle(force bool) *Config {
  292. c.S3ForcePathStyle = &force
  293. return c
  294. }
  295. // WithS3Disable100Continue sets a config S3Disable100Continue value returning
  296. // a Config pointer for chaining.
  297. func (c *Config) WithS3Disable100Continue(disable bool) *Config {
  298. c.S3Disable100Continue = &disable
  299. return c
  300. }
  301. // WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
  302. // pointer for chaining.
  303. func (c *Config) WithS3UseAccelerate(enable bool) *Config {
  304. c.S3UseAccelerate = &enable
  305. return c
  306. }
  307. // WithS3DisableContentMD5Validation sets a config
  308. // S3DisableContentMD5Validation value returning a Config pointer for chaining.
  309. func (c *Config) WithS3DisableContentMD5Validation(enable bool) *Config {
  310. c.S3DisableContentMD5Validation = &enable
  311. return c
  312. }
  313. // WithUseDualStack sets a config UseDualStack value returning a Config
  314. // pointer for chaining.
  315. func (c *Config) WithUseDualStack(enable bool) *Config {
  316. c.UseDualStack = &enable
  317. return c
  318. }
  319. // WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
  320. // returning a Config pointer for chaining.
  321. func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
  322. c.EC2MetadataDisableTimeoutOverride = &enable
  323. return c
  324. }
  325. // WithSleepDelay overrides the function used to sleep while waiting for the
  326. // next retry. Defaults to time.Sleep.
  327. func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
  328. c.SleepDelay = fn
  329. return c
  330. }
  331. // MergeIn merges the passed in configs into the existing config object.
  332. func (c *Config) MergeIn(cfgs ...*Config) {
  333. for _, other := range cfgs {
  334. mergeInConfig(c, other)
  335. }
  336. }
  337. func mergeInConfig(dst *Config, other *Config) {
  338. if other == nil {
  339. return
  340. }
  341. if other.CredentialsChainVerboseErrors != nil {
  342. dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
  343. }
  344. if other.Credentials != nil {
  345. dst.Credentials = other.Credentials
  346. }
  347. if other.Endpoint != nil {
  348. dst.Endpoint = other.Endpoint
  349. }
  350. if other.EndpointResolver != nil {
  351. dst.EndpointResolver = other.EndpointResolver
  352. }
  353. if other.Region != nil {
  354. dst.Region = other.Region
  355. }
  356. if other.DisableSSL != nil {
  357. dst.DisableSSL = other.DisableSSL
  358. }
  359. if other.HTTPClient != nil {
  360. dst.HTTPClient = other.HTTPClient
  361. }
  362. if other.LogLevel != nil {
  363. dst.LogLevel = other.LogLevel
  364. }
  365. if other.Logger != nil {
  366. dst.Logger = other.Logger
  367. }
  368. if other.MaxRetries != nil {
  369. dst.MaxRetries = other.MaxRetries
  370. }
  371. if other.Retryer != nil {
  372. dst.Retryer = other.Retryer
  373. }
  374. if other.DisableParamValidation != nil {
  375. dst.DisableParamValidation = other.DisableParamValidation
  376. }
  377. if other.DisableComputeChecksums != nil {
  378. dst.DisableComputeChecksums = other.DisableComputeChecksums
  379. }
  380. if other.S3ForcePathStyle != nil {
  381. dst.S3ForcePathStyle = other.S3ForcePathStyle
  382. }
  383. if other.S3Disable100Continue != nil {
  384. dst.S3Disable100Continue = other.S3Disable100Continue
  385. }
  386. if other.S3UseAccelerate != nil {
  387. dst.S3UseAccelerate = other.S3UseAccelerate
  388. }
  389. if other.S3DisableContentMD5Validation != nil {
  390. dst.S3DisableContentMD5Validation = other.S3DisableContentMD5Validation
  391. }
  392. if other.UseDualStack != nil {
  393. dst.UseDualStack = other.UseDualStack
  394. }
  395. if other.EC2MetadataDisableTimeoutOverride != nil {
  396. dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
  397. }
  398. if other.SleepDelay != nil {
  399. dst.SleepDelay = other.SleepDelay
  400. }
  401. if other.DisableRestProtocolURICleaning != nil {
  402. dst.DisableRestProtocolURICleaning = other.DisableRestProtocolURICleaning
  403. }
  404. if other.EnforceShouldRetryCheck != nil {
  405. dst.EnforceShouldRetryCheck = other.EnforceShouldRetryCheck
  406. }
  407. }
  408. // Copy will return a shallow copy of the Config object. If any additional
  409. // configurations are provided they will be merged into the new config returned.
  410. func (c *Config) Copy(cfgs ...*Config) *Config {
  411. dst := &Config{}
  412. dst.MergeIn(c)
  413. for _, cfg := range cfgs {
  414. dst.MergeIn(cfg)
  415. }
  416. return dst
  417. }