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