README.md
1# AWS SDK for Go
2
3[![API Reference](https://img.shields.io/badge/api-reference-blue.svg)](https://docs.aws.amazon.com/sdk-for-go/api) [![Join the chat at https://gitter.im/aws/aws-sdk-go](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/aws/aws-sdk-go?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build status](https://github.com/aws/aws-sdk-go/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/aws/aws-sdk-go/actions/workflows/go.yml) [![Apache V2 License](https://img.shields.io/badge/license-Apache%20V2-blue.svg)](https://github.com/aws/aws-sdk-go/blob/main/LICENSE.txt)
4
5aws-sdk-go is the official AWS SDK for the Go programming language.
6
7Checkout our [release notes](https://github.com/aws/aws-sdk-go/releases) for
8information about the latest bug fixes, updates, and features added to the SDK.
9
10We [announced](https://aws.amazon.com/blogs/developer/aws-sdk-for-go-version-2-general-availability/) the General Availability for the [AWS SDK for Go V2 (v2)](https://github.com/aws/aws-sdk-go-v2). The v2 SDK source is available at https://github.com/aws/aws-sdk-go-v2. Review the v2 SDK's [Developer Guide](https://aws.github.io/aws-sdk-go-v2/docs/) to get started with AWS SDK for Go V2 or review the [migration guide](https://aws.github.io/aws-sdk-go-v2/docs/migrating/) if you already use version 1.
11
12Jump To:
13* [Getting Started](#Getting-Started)
14* [Quick Examples](#Quick-Examples)
15* [Getting Help](#Getting-Help)
16* [Contributing](#Contributing)
17* [More Resources](#Resources)
18
19## Getting Started
20
21### Installing
22Use `go get` to retrieve the SDK to add it to your `GOPATH` workspace, or
23project's Go module dependencies.
24
25 go get github.com/aws/aws-sdk-go
26
27To update the SDK use `go get -u` to retrieve the latest version of the SDK.
28
29 go get -u github.com/aws/aws-sdk-go
30
31### Dependencies
32
33The SDK includes a `vendor` folder containing the runtime dependencies of the
34SDK. The metadata of the SDK's dependencies can be found in the Go module file
35`go.mod` or Dep file `Gopkg.toml`.
36
37### Go Modules
38
39If you are using Go modules, your `go get` will default to the latest tagged
40release version of the SDK. To get a specific release version of the SDK use
41`@<tag>` in your `go get` command.
42
43 go get github.com/aws/aws-sdk-go@v1.15.77
44
45To get the latest SDK repository change use `@latest`.
46
47 go get github.com/aws/aws-sdk-go@latest
48
49### Go 1.5
50
51If you are using Go 1.5 without vendoring enabled, (`GO15VENDOREXPERIMENT=1`),
52you will need to use `...` when retrieving the SDK to get its dependencies.
53
54 go get github.com/aws/aws-sdk-go/...
55
56This will still include the `vendor` folder. The `vendor` folder can be deleted
57if not used by your environment.
58
59 rm -rf $GOPATH/src/github.com/aws/aws-sdk-go/vendor
60
61
62## Quick Examples
63
64### Complete SDK Example
65
66This example shows a complete working Go file which will upload a file to S3
67and use the Context pattern to implement timeout logic that will cancel the
68request if it takes too long. This example highlights how to use sessions,
69create a service client, make a request, handle the error, and process the
70response.
71
72```go
73 package main
74
75 import (
76 "context"
77 "flag"
78 "fmt"
79 "os"
80 "time"
81
82 "github.com/aws/aws-sdk-go/aws"
83 "github.com/aws/aws-sdk-go/aws/awserr"
84 "github.com/aws/aws-sdk-go/aws/request"
85 "github.com/aws/aws-sdk-go/aws/session"
86 "github.com/aws/aws-sdk-go/service/s3"
87 )
88
89 // Uploads a file to S3 given a bucket and object key. Also takes a duration
90 // value to terminate the update if it doesn't complete within that time.
91 //
92 // The AWS Region needs to be provided in the AWS shared config or on the
93 // environment variable as `AWS_REGION`. Credentials also must be provided
94 // Will default to shared config file, but can load from environment if provided.
95 //
96 // Usage:
97 // # Upload myfile.txt to myBucket/myKey. Must complete within 10 minutes or will fail
98 // go run withContext.go -b mybucket -k myKey -d 10m < myfile.txt
99 func main() {
100 var bucket, key string
101 var timeout time.Duration
102
103 flag.StringVar(&bucket, "b", "", "Bucket name.")
104 flag.StringVar(&key, "k", "", "Object key name.")
105 flag.DurationVar(&timeout, "d", 0, "Upload timeout.")
106 flag.Parse()
107
108 // All clients require a Session. The Session provides the client with
109 // shared configuration such as region, endpoint, and credentials. A
110 // Session should be shared where possible to take advantage of
111 // configuration and credential caching. See the session package for
112 // more information.
113 sess := session.Must(session.NewSession())
114
115 // Create a new instance of the service's client with a Session.
116 // Optional aws.Config values can also be provided as variadic arguments
117 // to the New function. This option allows you to provide service
118 // specific configuration.
119 svc := s3.New(sess)
120
121 // Create a context with a timeout that will abort the upload if it takes
122 // more than the passed in timeout.
123 ctx := context.Background()
124 var cancelFn func()
125 if timeout > 0 {
126 ctx, cancelFn = context.WithTimeout(ctx, timeout)
127 }
128 // Ensure the context is canceled to prevent leaking.
129 // See context package for more information, https://golang.org/pkg/context/
130 if cancelFn != nil {
131 defer cancelFn()
132 }
133
134 // Uploads the object to S3. The Context will interrupt the request if the
135 // timeout expires.
136 _, err := svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
137 Bucket: aws.String(bucket),
138 Key: aws.String(key),
139 Body: os.Stdin,
140 })
141 if err != nil {
142 if aerr, ok := err.(awserr.Error); ok && aerr.Code() == request.CanceledErrorCode {
143 // If the SDK can determine the request or retry delay was canceled
144 // by a context the CanceledErrorCode error code will be returned.
145 fmt.Fprintf(os.Stderr, "upload canceled due to timeout, %v\n", err)
146 } else {
147 fmt.Fprintf(os.Stderr, "failed to upload object, %v\n", err)
148 }
149 os.Exit(1)
150 }
151
152 fmt.Printf("successfully uploaded file to %s/%s\n", bucket, key)
153 }
154```
155
156### Overview of SDK's Packages
157
158The SDK is composed of two main components, SDK core, and service clients.
159The SDK core packages are all available under the aws package at the root of
160the SDK. Each client for a supported AWS service is available within its own
161package under the service folder at the root of the SDK.
162
163 * aws - SDK core, provides common shared types such as Config, Logger,
164 and utilities to make working with API parameters easier.
165
166 * awserr - Provides the error interface that the SDK will use for all
167 errors that occur in the SDK's processing. This includes service API
168 response errors as well. The Error type is made up of a code and message.
169 Cast the SDK's returned error type to awserr.Error and call the Code
170 method to compare returned error to specific error codes. See the package's
171 documentation for additional values that can be extracted such as RequestID.
172
173 * credentials - Provides the types and built in credentials providers
174 the SDK will use to retrieve AWS credentials to make API requests with.
175 Nested under this folder are also additional credentials providers such as
176 stscreds for assuming IAM roles, and ec2rolecreds for EC2 Instance roles.
177
178 * endpoints - Provides the AWS Regions and Endpoints metadata for the SDK.
179 Use this to lookup AWS service endpoint information such as which services
180 are in a region, and what regions a service is in. Constants are also provided
181 for all region identifiers, e.g UsWest2RegionID for "us-west-2".
182
183 * session - Provides initial default configuration, and load
184 configuration from external sources such as environment and shared
185 credentials file.
186
187 * request - Provides the API request sending, and retry logic for the SDK.
188 This package also includes utilities for defining your own request
189 retryer, and configuring how the SDK processes the request.
190
191 * service - Clients for AWS services. All services supported by the SDK are
192 available under this folder.
193
194### How to Use the SDK's AWS Service Clients
195
196The SDK includes the Go types and utilities you can use to make requests to
197AWS service APIs. Within the service folder at the root of the SDK you'll find
198a package for each AWS service the SDK supports. All service clients follow common pattern of creation and usage.
199
200When creating a client for an AWS service you'll first need to have a Session
201value constructed. The Session provides shared configuration that can be shared
202between your service clients. When service clients are created you can pass
203in additional configuration via the aws.Config type to override configuration
204provided by in the Session to create service client instances with custom
205configuration.
206
207Once the service's client is created you can use it to make API requests the
208AWS service. These clients are safe to use concurrently.
209
210### Configuring the SDK
211
212In the AWS SDK for Go, you can configure settings for service clients, such
213as the log level and maximum number of retries. Most settings are optional;
214however, for each service client, you must specify a region and your credentials.
215The SDK uses these values to send requests to the correct AWS region and sign
216requests with the correct credentials. You can specify these values as part
217of a session or as environment variables.
218
219See the SDK's [configuration guide][config_guide] for more information.
220
221See the [session][session_pkg] package documentation for more information on how to use Session
222with the SDK.
223
224See the [Config][config_typ] type in the [aws][aws_pkg] package for more information on configuration
225options.
226
227[config_guide]: https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html
228[session_pkg]: https://docs.aws.amazon.com/sdk-for-go/api/aws/session/
229[config_typ]: https://docs.aws.amazon.com/sdk-for-go/api/aws/#Config
230[aws_pkg]: https://docs.aws.amazon.com/sdk-for-go/api/aws/
231
232### Configuring Credentials
233
234When using the SDK you'll generally need your AWS credentials to authenticate
235with AWS services. The SDK supports multiple methods of supporting these
236credentials. By default the SDK will source credentials automatically from
237its default credential chain. See the session package for more information
238on this chain, and how to configure it. The common items in the credential
239chain are the following:
240
241 * Environment Credentials - Set of environment variables that are useful
242 when sub processes are created for specific roles.
243
244 * Shared Credentials file (~/.aws/credentials) - This file stores your
245 credentials based on a profile name and is useful for local development.
246
247 * EC2 Instance Role Credentials - Use EC2 Instance Role to assign credentials
248 to application running on an EC2 instance. This removes the need to manage
249 credential files in production.
250
251Credentials can be configured in code as well by setting the Config's Credentials
252value to a custom provider or using one of the providers included with the
253SDK to bypass the default credential chain and use a custom one. This is
254helpful when you want to instruct the SDK to only use a specific set of
255credentials or providers.
256
257This example creates a credential provider for assuming an IAM role, "myRoleARN"
258and configures the S3 service client to use that role for API requests.
259
260```go
261 // Initial credentials loaded from SDK's default credential chain. Such as
262 // the environment, shared credentials (~/.aws/credentials), or EC2 Instance
263 // Role. These credentials will be used to to make the STS Assume Role API.
264 sess := session.Must(session.NewSession())
265
266 // Create the credentials from AssumeRoleProvider to assume the role
267 // referenced by the "myRoleARN" ARN.
268 creds := stscreds.NewCredentials(sess, "myRoleArn")
269
270 // Create service client value configured for credentials
271 // from assumed role.
272 svc := s3.New(sess, &aws.Config{Credentials: creds})
273```
274
275See the [credentials][credentials_pkg] package documentation for more information on credential
276providers included with the SDK, and how to customize the SDK's usage of
277credentials.
278
279The SDK has support for the shared configuration file (~/.aws/config). This
280support can be enabled by setting the environment variable, "AWS_SDK_LOAD_CONFIG=1",
281or enabling the feature in code when creating a Session via the
282Option's SharedConfigState parameter.
283
284```go
285 sess := session.Must(session.NewSessionWithOptions(session.Options{
286 SharedConfigState: session.SharedConfigEnable,
287 }))
288```
289
290[credentials_pkg]: https://docs.aws.amazon.com/sdk-for-go/api/aws/credentials
291
292### Configuring AWS Region
293
294In addition to the credentials you'll need to specify the region the SDK
295will use to make AWS API requests to. In the SDK you can specify the region
296either with an environment variable, or directly in code when a Session or
297service client is created. The last value specified in code wins if the region
298is specified multiple ways.
299
300To set the region via the environment variable set the "AWS_REGION" to the
301region you want to the SDK to use. Using this method to set the region will
302allow you to run your application in multiple regions without needing additional
303code in the application to select the region.
304
305 AWS_REGION=us-west-2
306
307The endpoints package includes constants for all regions the SDK knows. The
308values are all suffixed with RegionID. These values are helpful, because they
309reduce the need to type the region string manually.
310
311To set the region on a Session use the aws package's Config struct parameter
312Region to the AWS region you want the service clients created from the session to
313use. This is helpful when you want to create multiple service clients, and
314all of the clients make API requests to the same region.
315
316```go
317 sess := session.Must(session.NewSession(&aws.Config{
318 Region: aws.String(endpoints.UsWest2RegionID),
319 }))
320```
321
322See the [endpoints][endpoints_pkg] package for the AWS Regions and Endpoints metadata.
323
324In addition to setting the region when creating a Session you can also set
325the region on a per service client bases. This overrides the region of a
326Session. This is helpful when you want to create service clients in specific
327regions different from the Session's region.
328
329```go
330 svc := s3.New(sess, &aws.Config{
331 Region: aws.String(endpoints.UsWest2RegionID),
332 })
333```
334
335See the [Config][config_typ] type in the [aws][aws_pkg] package for more information and additional
336options such as setting the Endpoint, and other service client configuration options.
337
338[endpoints_pkg]: https://docs.aws.amazon.com/sdk-for-go/api/aws/endpoints/
339
340### Making API Requests
341
342Once the client is created you can make an API request to the service.
343Each API method takes a input parameter, and returns the service response
344and an error. The SDK provides methods for making the API call in multiple ways.
345
346In this list we'll use the S3 ListObjects API as an example for the different
347ways of making API requests.
348
349 * ListObjects - Base API operation that will make the API request to the service.
350
351 * ListObjectsRequest - API methods suffixed with Request will construct the
352 API request, but not send it. This is also helpful when you want to get a
353 presigned URL for a request, and share the presigned URL instead of your
354 application making the request directly.
355
356 * ListObjectsPages - Same as the base API operation, but uses a callback to
357 automatically handle pagination of the API's response.
358
359 * ListObjectsWithContext - Same as base API operation, but adds support for
360 the Context pattern. This is helpful for controlling the canceling of in
361 flight requests. See the Go standard library context package for more
362 information. This method also takes request package's Option functional
363 options as the variadic argument for modifying how the request will be
364 made, or extracting information from the raw HTTP response.
365
366 * ListObjectsPagesWithContext - same as ListObjectsPages, but adds support for
367 the Context pattern. Similar to ListObjectsWithContext this method also
368 takes the request package's Option function option types as the variadic
369 argument.
370
371In addition to the API operations the SDK also includes several higher level
372methods that abstract checking for and waiting for an AWS resource to be in
373a desired state. In this list we'll use WaitUntilBucketExists to demonstrate
374the different forms of waiters.
375
376 * WaitUntilBucketExists. - Method to make API request to query an AWS service for
377 a resource's state. Will return successfully when that state is accomplished.
378
379 * WaitUntilBucketExistsWithContext - Same as WaitUntilBucketExists, but adds
380 support for the Context pattern. In addition these methods take request
381 package's WaiterOptions to configure the waiter, and how underlying request
382 will be made by the SDK.
383
384The API method will document which error codes the service might return for
385the operation. These errors will also be available as const strings prefixed
386with "ErrCode" in the service client's package. If there are no errors listed
387in the API's SDK documentation you'll need to consult the AWS service's API
388documentation for the errors that could be returned.
389
390```go
391 ctx := context.Background()
392
393 result, err := svc.GetObjectWithContext(ctx, &s3.GetObjectInput{
394 Bucket: aws.String("my-bucket"),
395 Key: aws.String("my-key"),
396 })
397 if err != nil {
398 // Cast err to awserr.Error to handle specific error codes.
399 aerr, ok := err.(awserr.Error)
400 if ok && aerr.Code() == s3.ErrCodeNoSuchKey {
401 // Specific error code handling
402 }
403 return err
404 }
405
406 // Make sure to close the body when done with it for S3 GetObject APIs or
407 // will leak connections.
408 defer result.Body.Close()
409
410 fmt.Println("Object Size:", aws.Int64Value(result.ContentLength))
411```
412
413### API Request Pagination and Resource Waiters
414
415Pagination helper methods are suffixed with "Pages", and provide the
416functionality needed to round trip API page requests. Pagination methods
417take a callback function that will be called for each page of the API's response.
418
419```go
420 objects := []string{}
421 err := svc.ListObjectsPagesWithContext(ctx, &s3.ListObjectsInput{
422 Bucket: aws.String(myBucket),
423 }, func(p *s3.ListObjectsOutput, lastPage bool) bool {
424 for _, o := range p.Contents {
425 objects = append(objects, aws.StringValue(o.Key))
426 }
427 return true // continue paging
428 })
429 if err != nil {
430 panic(fmt.Sprintf("failed to list objects for bucket, %s, %v", myBucket, err))
431 }
432
433 fmt.Println("Objects in bucket:", objects)
434```
435
436Waiter helper methods provide the functionality to wait for an AWS resource
437state. These methods abstract the logic needed to check the state of an
438AWS resource, and wait until that resource is in a desired state. The waiter
439will block until the resource is in the state that is desired, an error occurs,
440or the waiter times out. If a resource times out the error code returned will
441be request.WaiterResourceNotReadyErrorCode.
442
443```go
444 err := svc.WaitUntilBucketExistsWithContext(ctx, &s3.HeadBucketInput{
445 Bucket: aws.String(myBucket),
446 })
447 if err != nil {
448 aerr, ok := err.(awserr.Error)
449 if ok && aerr.Code() == request.WaiterResourceNotReadyErrorCode {
450 fmt.Fprintf(os.Stderr, "timed out while waiting for bucket to exist")
451 }
452 panic(fmt.Errorf("failed to wait for bucket to exist, %v", err))
453 }
454 fmt.Println("Bucket", myBucket, "exists")
455```
456
457## Getting Help
458
459Please use these community resources for getting help. We use the GitHub issues
460for tracking bugs and feature requests.
461
462* Ask a question on [StackOverflow](http://stackoverflow.com/) and tag it with the [`aws-sdk-go`](http://stackoverflow.com/questions/tagged/aws-sdk-go) tag.
463* Come join the AWS SDK for Go community chat on [gitter](https://gitter.im/aws/aws-sdk-go).
464* Open a support ticket with [AWS Support](http://docs.aws.amazon.com/awssupport/latest/user/getting-started.html).
465* If you think you may have found a bug, please open an [issue](https://github.com/aws/aws-sdk-go/issues/new/choose).
466
467This SDK implements AWS service APIs. For general issues regarding the AWS services and their limitations, you may also take a look at the [Amazon Web Services Discussion Forums](https://forums.aws.amazon.com/).
468
469### Opening Issues
470
471If you encounter a bug with the AWS SDK for Go we would like to hear about it.
472Search the [existing issues](https://github.com/aws/aws-sdk-go/issues) and see
473if others are also experiencing the issue before opening a new issue. Please
474include the version of AWS SDK for Go, Go language, and OS you’re using. Please
475also include reproduction case when appropriate.
476
477The GitHub issues are intended for bug reports and feature requests. For help
478and questions with using AWS SDK for Go please make use of the resources listed
479in the [Getting Help](https://github.com/aws/aws-sdk-go#getting-help) section.
480Keeping the list of open issues lean will help us respond in a timely manner.
481
482## Contributing
483
484We work hard to provide a high-quality and useful SDK for our AWS services, and we greatly value feedback and contributions from our community. Please review our [contributing guidelines](./CONTRIBUTING.md) before submitting any [issues] or [pull requests][pr] to ensure we have all the necessary information to effectively respond to your bug report or contribution.
485
486## Maintenance and support for SDK major versions
487
488For information about maintenance and support for SDK major versions and our underlying dependencies, see the following in the AWS SDKs and Tools Shared Configuration and Credentials Reference Guide:
489
490* [AWS SDKs and Tools Maintenance Policy](https://docs.aws.amazon.com/credref/latest/refdocs/maint-policy.html)
491* [AWS SDKs and Tools Version Support Matrix](https://docs.aws.amazon.com/credref/latest/refdocs/version-support-matrix.html)
492
493## Resources
494
495[Developer guide](https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/welcome.html) - This document
496is a general introduction on how to configure and make requests with the SDK.
497If this is your first time using the SDK, this documentation and the API
498documentation will help you get started. This document focuses on the syntax
499and behavior of the SDK. The [Service Developer Guide](https://aws.amazon.com/documentation/)
500will help you get started using specific AWS services.
501
502[SDK API Reference Documentation](https://docs.aws.amazon.com/sdk-for-go/api/) - Use this
503document to look up all API operation input and output parameters for AWS
504services supported by the SDK. The API reference also includes documentation of
505the SDK, and examples how to using the SDK, service client API operations, and
506API operation require parameters.
507
508[Service Documentation](https://aws.amazon.com/documentation/) - Use this
509documentation to learn how to interface with AWS services. These guides are
510great for getting started with a service, or when looking for more
511information about a service. While this document is not required for coding,
512services may supply helpful samples to look out for.
513
514[SDK Examples](https://github.com/aws/aws-sdk-go/tree/main/example) -
515Included in the SDK's repo are several hand crafted examples using the SDK
516features and AWS services.
517
518[Forum](https://forums.aws.amazon.com/forum.jspa?forumID=293) - Ask questions, get help, and give feedback
519
520[Issues][issues] - Report issues, submit pull requests, and get involved
521 (see [Apache 2.0 License][license])
522
523
524[issues]: https://github.com/aws/aws-sdk-go/issues
525[pr]: https://github.com/aws/aws-sdk-go/pulls
526[license]: http://aws.amazon.com/apache2.0/
527