config.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 structure.
  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 for AWS
  59. // 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. //
  106. // See http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
  107. // for Amazon S3: Virtual Hosting of Buckets
  108. S3ForcePathStyle *bool
  109. // Set this to `true` to disable the SDK adding the `Expect: 100-Continue`
  110. // header to PUT requests over 2MB of content. 100-Continue instructs the
  111. // HTTP client not to send the body until the service responds with a
  112. // `continue` status. This is useful to prevent sending the request body
  113. // until after the request is authenticated, and validated.
  114. //
  115. // http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
  116. //
  117. // 100-Continue is only enabled for Go 1.6 and above. See `http.Transport`'s
  118. // `ExpectContinueTimeout` for information on adjusting the continue wait
  119. // timeout. https://golang.org/pkg/net/http/#Transport
  120. //
  121. // You should use this flag to disble 100-Continue if you experience issues
  122. // with proxies or third party S3 compatible services.
  123. S3Disable100Continue *bool
  124. // Set this to `true` to enable S3 Accelerate feature. For all operations
  125. // compatible with S3 Accelerate will use the accelerate endpoint for
  126. // requests. Requests not compatible will fall back to normal S3 requests.
  127. //
  128. // The bucket must be enable for accelerate to be used with S3 client with
  129. // accelerate enabled. If the bucket is not enabled for accelerate an error
  130. // will be returned. The bucket name must be DNS compatible to also work
  131. // with accelerate.
  132. S3UseAccelerate *bool
  133. // S3DisableContentMD5Validation config option is temporarily disabled,
  134. // For S3 GetObject API calls, #1837.
  135. //
  136. // Set this to `true` to disable the S3 service client from automatically
  137. // adding the ContentMD5 to S3 Object Put and Upload API calls. This option
  138. // will also disable the SDK from performing object ContentMD5 validation
  139. // on GetObject API calls.
  140. S3DisableContentMD5Validation *bool
  141. // Set this to `true` to disable the EC2Metadata client from overriding the
  142. // default http.Client's Timeout. This is helpful if you do not want the
  143. // EC2Metadata client to create a new http.Client. This options is only
  144. // meaningful if you're not already using a custom HTTP client with the
  145. // SDK. Enabled by default.
  146. //
  147. // Must be set and provided to the session.NewSession() in order to disable
  148. // the EC2Metadata overriding the timeout for default credentials chain.
  149. //
  150. // Example:
  151. // sess := session.Must(session.NewSession(aws.NewConfig()
  152. // .WithEC2MetadataDiableTimeoutOverride(true)))
  153. //
  154. // svc := s3.New(sess)
  155. //
  156. EC2MetadataDisableTimeoutOverride *bool
  157. // Instructs the endpoint to be generated for a service client to
  158. // be the dual stack endpoint. The dual stack endpoint will support
  159. // both IPv4 and IPv6 addressing.
  160. //
  161. // Setting this for a service which does not support dual stack will fail
  162. // to make requets. It is not recommended to set this value on the session
  163. // as it will apply to all service clients created with the session. Even
  164. // services which don't support dual stack endpoints.
  165. //
  166. // If the Endpoint config value is also provided the UseDualStack flag
  167. // will be ignored.
  168. //
  169. // Only supported with.
  170. //
  171. // sess := session.Must(session.NewSession())
  172. //
  173. // svc := s3.New(sess, &aws.Config{
  174. // UseDualStack: aws.Bool(true),
  175. // })
  176. UseDualStack *bool
  177. // SleepDelay is an override for the func the SDK will call when sleeping
  178. // during the lifecycle of a request. Specifically this will be used for
  179. // request delays. This value should only be used for testing. To adjust
  180. // the delay of a request see the aws/client.DefaultRetryer and
  181. // aws/request.Retryer.
  182. //
  183. // SleepDelay will prevent any Context from being used for canceling retry
  184. // delay of an API operation. It is recommended to not use SleepDelay at all
  185. // and specify a Retryer instead.
  186. SleepDelay func(time.Duration)
  187. // DisableRestProtocolURICleaning will not clean the URL path when making rest protocol requests.
  188. // Will default to false. This would only be used for empty directory names in s3 requests.
  189. //
  190. // Example:
  191. // sess := session.Must(session.NewSession(&aws.Config{
  192. // DisableRestProtocolURICleaning: aws.Bool(true),
  193. // }))
  194. //
  195. // svc := s3.New(sess)
  196. // out, err := svc.GetObject(&s3.GetObjectInput {
  197. // Bucket: aws.String("bucketname"),
  198. // Key: aws.String("//foo//bar//moo"),
  199. // })
  200. DisableRestProtocolURICleaning *bool
  201. // EnableEndpointDiscovery will allow for endpoint discovery on operations that
  202. // have the definition in its model. By default, endpoint discovery is off.
  203. //
  204. // Example:
  205. // sess := session.Must(session.NewSession(&aws.Config{
  206. // EnableEndpointDiscovery: aws.Bool(true),
  207. // }))
  208. //
  209. // svc := s3.New(sess)
  210. // out, err := svc.GetObject(&s3.GetObjectInput {
  211. // Bucket: aws.String("bucketname"),
  212. // Key: aws.String("/foo/bar/moo"),
  213. // })
  214. EnableEndpointDiscovery *bool
  215. // DisableEndpointHostPrefix will disable the SDK's behavior of prefixing
  216. // request endpoint hosts with modeled information.
  217. //
  218. // Disabling this feature is useful when you want to use local endpoints
  219. // for testing that do not support the modeled host prefix pattern.
  220. DisableEndpointHostPrefix *bool
  221. }
  222. // NewConfig returns a new Config pointer that can be chained with builder
  223. // methods to set multiple configuration values inline without using pointers.
  224. //
  225. // // Create Session with MaxRetry configuration to be shared by multiple
  226. // // service clients.
  227. // sess := session.Must(session.NewSession(aws.NewConfig().
  228. // WithMaxRetries(3),
  229. // ))
  230. //
  231. // // Create S3 service client with a specific Region.
  232. // svc := s3.New(sess, aws.NewConfig().
  233. // WithRegion("us-west-2"),
  234. // )
  235. func NewConfig() *Config {
  236. return &Config{}
  237. }
  238. // WithCredentialsChainVerboseErrors sets a config verbose errors boolean and returning
  239. // a Config pointer.
  240. func (c *Config) WithCredentialsChainVerboseErrors(verboseErrs bool) *Config {
  241. c.CredentialsChainVerboseErrors = &verboseErrs
  242. return c
  243. }
  244. // WithCredentials sets a config Credentials value returning a Config pointer
  245. // for chaining.
  246. func (c *Config) WithCredentials(creds *credentials.Credentials) *Config {
  247. c.Credentials = creds
  248. return c
  249. }
  250. // WithEndpoint sets a config Endpoint value returning a Config pointer for
  251. // chaining.
  252. func (c *Config) WithEndpoint(endpoint string) *Config {
  253. c.Endpoint = &endpoint
  254. return c
  255. }
  256. // WithEndpointResolver sets a config EndpointResolver value returning a
  257. // Config pointer for chaining.
  258. func (c *Config) WithEndpointResolver(resolver endpoints.Resolver) *Config {
  259. c.EndpointResolver = resolver
  260. return c
  261. }
  262. // WithRegion sets a config Region value returning a Config pointer for
  263. // chaining.
  264. func (c *Config) WithRegion(region string) *Config {
  265. c.Region = &region
  266. return c
  267. }
  268. // WithDisableSSL sets a config DisableSSL value returning a Config pointer
  269. // for chaining.
  270. func (c *Config) WithDisableSSL(disable bool) *Config {
  271. c.DisableSSL = &disable
  272. return c
  273. }
  274. // WithHTTPClient sets a config HTTPClient value returning a Config pointer
  275. // for chaining.
  276. func (c *Config) WithHTTPClient(client *http.Client) *Config {
  277. c.HTTPClient = client
  278. return c
  279. }
  280. // WithMaxRetries sets a config MaxRetries value returning a Config pointer
  281. // for chaining.
  282. func (c *Config) WithMaxRetries(max int) *Config {
  283. c.MaxRetries = &max
  284. return c
  285. }
  286. // WithDisableParamValidation sets a config DisableParamValidation value
  287. // returning a Config pointer for chaining.
  288. func (c *Config) WithDisableParamValidation(disable bool) *Config {
  289. c.DisableParamValidation = &disable
  290. return c
  291. }
  292. // WithDisableComputeChecksums sets a config DisableComputeChecksums value
  293. // returning a Config pointer for chaining.
  294. func (c *Config) WithDisableComputeChecksums(disable bool) *Config {
  295. c.DisableComputeChecksums = &disable
  296. return c
  297. }
  298. // WithLogLevel sets a config LogLevel value returning a Config pointer for
  299. // chaining.
  300. func (c *Config) WithLogLevel(level LogLevelType) *Config {
  301. c.LogLevel = &level
  302. return c
  303. }
  304. // WithLogger sets a config Logger value returning a Config pointer for
  305. // chaining.
  306. func (c *Config) WithLogger(logger Logger) *Config {
  307. c.Logger = logger
  308. return c
  309. }
  310. // WithS3ForcePathStyle sets a config S3ForcePathStyle value returning a Config
  311. // pointer for chaining.
  312. func (c *Config) WithS3ForcePathStyle(force bool) *Config {
  313. c.S3ForcePathStyle = &force
  314. return c
  315. }
  316. // WithS3Disable100Continue sets a config S3Disable100Continue value returning
  317. // a Config pointer for chaining.
  318. func (c *Config) WithS3Disable100Continue(disable bool) *Config {
  319. c.S3Disable100Continue = &disable
  320. return c
  321. }
  322. // WithS3UseAccelerate sets a config S3UseAccelerate value returning a Config
  323. // pointer for chaining.
  324. func (c *Config) WithS3UseAccelerate(enable bool) *Config {
  325. c.S3UseAccelerate = &enable
  326. return c
  327. }
  328. // WithS3DisableContentMD5Validation sets a config
  329. // S3DisableContentMD5Validation value returning a Config pointer for chaining.
  330. func (c *Config) WithS3DisableContentMD5Validation(enable bool) *Config {
  331. c.S3DisableContentMD5Validation = &enable
  332. return c
  333. }
  334. // WithUseDualStack sets a config UseDualStack value returning a Config
  335. // pointer for chaining.
  336. func (c *Config) WithUseDualStack(enable bool) *Config {
  337. c.UseDualStack = &enable
  338. return c
  339. }
  340. // WithEC2MetadataDisableTimeoutOverride sets a config EC2MetadataDisableTimeoutOverride value
  341. // returning a Config pointer for chaining.
  342. func (c *Config) WithEC2MetadataDisableTimeoutOverride(enable bool) *Config {
  343. c.EC2MetadataDisableTimeoutOverride = &enable
  344. return c
  345. }
  346. // WithSleepDelay overrides the function used to sleep while waiting for the
  347. // next retry. Defaults to time.Sleep.
  348. func (c *Config) WithSleepDelay(fn func(time.Duration)) *Config {
  349. c.SleepDelay = fn
  350. return c
  351. }
  352. // WithEndpointDiscovery will set whether or not to use endpoint discovery.
  353. func (c *Config) WithEndpointDiscovery(t bool) *Config {
  354. c.EnableEndpointDiscovery = &t
  355. return c
  356. }
  357. // WithDisableEndpointHostPrefix will set whether or not to use modeled host prefix
  358. // when making requests.
  359. func (c *Config) WithDisableEndpointHostPrefix(t bool) *Config {
  360. c.DisableEndpointHostPrefix = &t
  361. return c
  362. }
  363. // MergeIn merges the passed in configs into the existing config object.
  364. func (c *Config) MergeIn(cfgs ...*Config) {
  365. for _, other := range cfgs {
  366. mergeInConfig(c, other)
  367. }
  368. }
  369. func mergeInConfig(dst *Config, other *Config) {
  370. if other == nil {
  371. return
  372. }
  373. if other.CredentialsChainVerboseErrors != nil {
  374. dst.CredentialsChainVerboseErrors = other.CredentialsChainVerboseErrors
  375. }
  376. if other.Credentials != nil {
  377. dst.Credentials = other.Credentials
  378. }
  379. if other.Endpoint != nil {
  380. dst.Endpoint = other.Endpoint
  381. }
  382. if other.EndpointResolver != nil {
  383. dst.EndpointResolver = other.EndpointResolver
  384. }
  385. if other.Region != nil {
  386. dst.Region = other.Region
  387. }
  388. if other.DisableSSL != nil {
  389. dst.DisableSSL = other.DisableSSL
  390. }
  391. if other.HTTPClient != nil {
  392. dst.HTTPClient = other.HTTPClient
  393. }
  394. if other.LogLevel != nil {
  395. dst.LogLevel = other.LogLevel
  396. }
  397. if other.Logger != nil {
  398. dst.Logger = other.Logger
  399. }
  400. if other.MaxRetries != nil {
  401. dst.MaxRetries = other.MaxRetries
  402. }
  403. if other.Retryer != nil {
  404. dst.Retryer = other.Retryer
  405. }
  406. if other.DisableParamValidation != nil {
  407. dst.DisableParamValidation = other.DisableParamValidation
  408. }
  409. if other.DisableComputeChecksums != nil {
  410. dst.DisableComputeChecksums = other.DisableComputeChecksums
  411. }
  412. if other.S3ForcePathStyle != nil {
  413. dst.S3ForcePathStyle = other.S3ForcePathStyle
  414. }
  415. if other.S3Disable100Continue != nil {
  416. dst.S3Disable100Continue = other.S3Disable100Continue
  417. }
  418. if other.S3UseAccelerate != nil {
  419. dst.S3UseAccelerate = other.S3UseAccelerate
  420. }
  421. if other.S3DisableContentMD5Validation != nil {
  422. dst.S3DisableContentMD5Validation = other.S3DisableContentMD5Validation
  423. }
  424. if other.UseDualStack != nil {
  425. dst.UseDualStack = other.UseDualStack
  426. }
  427. if other.EC2MetadataDisableTimeoutOverride != nil {
  428. dst.EC2MetadataDisableTimeoutOverride = other.EC2MetadataDisableTimeoutOverride
  429. }
  430. if other.SleepDelay != nil {
  431. dst.SleepDelay = other.SleepDelay
  432. }
  433. if other.DisableRestProtocolURICleaning != nil {
  434. dst.DisableRestProtocolURICleaning = other.DisableRestProtocolURICleaning
  435. }
  436. if other.EnforceShouldRetryCheck != nil {
  437. dst.EnforceShouldRetryCheck = other.EnforceShouldRetryCheck
  438. }
  439. if other.EnableEndpointDiscovery != nil {
  440. dst.EnableEndpointDiscovery = other.EnableEndpointDiscovery
  441. }
  442. if other.DisableEndpointHostPrefix != nil {
  443. dst.DisableEndpointHostPrefix = other.DisableEndpointHostPrefix
  444. }
  445. }
  446. // Copy will return a shallow copy of the Config object. If any additional
  447. // configurations are provided they will be merged into the new config returned.
  448. func (c *Config) Copy(cfgs ...*Config) *Config {
  449. dst := &Config{}
  450. dst.MergeIn(c)
  451. for _, cfg := range cfgs {
  452. dst.MergeIn(cfg)
  453. }
  454. return dst
  455. }