config.go 14 KB

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