github.com/aavshr/aws-sdk-go@v1.41.3/README.md (about) 1 # AWS SDK for Go 2 3 [](https://docs.aws.amazon.com/sdk-for-go/api) [](https://gitter.im/aws/aws-sdk-go?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [](https://github.com/aavshr/aws-sdk-go/actions/workflows/go.yml) [](https://github.com/aavshr/aws-sdk-go/blob/main/LICENSE.txt) 4 5 aws-sdk-go is the official AWS SDK for the Go programming language. 6 7 Checkout our [release notes](https://github.com/aavshr/aws-sdk-go/releases) for 8 information about the latest bug fixes, updates, and features added to the SDK. 9 10 We [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/aavshr/aws-sdk-go-v2). The v2 SDK source is available at https://github.com/aavshr/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 12 Jump 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 22 Use `go get` to retrieve the SDK to add it to your `GOPATH` workspace, or 23 project's Go module dependencies. 24 25 go get github.com/aavshr/aws-sdk-go 26 27 To update the SDK use `go get -u` to retrieve the latest version of the SDK. 28 29 go get -u github.com/aavshr/aws-sdk-go 30 31 ### Dependencies 32 33 The SDK includes a `vendor` folder containing the runtime dependencies of the 34 SDK. 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 39 If you are using Go modules, your `go get` will default to the latest tagged 40 release 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/aavshr/aws-sdk-go@v1.15.77 44 45 To get the latest SDK repository change use `@latest`. 46 47 go get github.com/aavshr/aws-sdk-go@latest 48 49 ### Go 1.5 50 51 If you are using Go 1.5 without vendoring enabled, (`GO15VENDOREXPERIMENT=1`), 52 you will need to use `...` when retrieving the SDK to get its dependencies. 53 54 go get github.com/aavshr/aws-sdk-go/... 55 56 This will still include the `vendor` folder. The `vendor` folder can be deleted 57 if not used by your environment. 58 59 rm -rf $GOPATH/src/github.com/aavshr/aws-sdk-go/vendor 60 61 62 ## Quick Examples 63 64 ### Complete SDK Example 65 66 This example shows a complete working Go file which will upload a file to S3 67 and use the Context pattern to implement timeout logic that will cancel the 68 request if it takes too long. This example highlights how to use sessions, 69 create a service client, make a request, handle the error, and process the 70 response. 71 72 ```go 73 package main 74 75 import ( 76 "context" 77 "flag" 78 "fmt" 79 "os" 80 "time" 81 82 "github.com/aavshr/aws-sdk-go/aws" 83 "github.com/aavshr/aws-sdk-go/aws/awserr" 84 "github.com/aavshr/aws-sdk-go/aws/request" 85 "github.com/aavshr/aws-sdk-go/aws/session" 86 "github.com/aavshr/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 158 The SDK is composed of two main components, SDK core, and service clients. 159 The SDK core packages are all available under the aws package at the root of 160 the SDK. Each client for a supported AWS service is available within its own 161 package 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 196 The SDK includes the Go types and utilities you can use to make requests to 197 AWS service APIs. Within the service folder at the root of the SDK you'll find 198 a package for each AWS service the SDK supports. All service clients follow common pattern of creation and usage. 199 200 When creating a client for an AWS service you'll first need to have a Session 201 value constructed. The Session provides shared configuration that can be shared 202 between your service clients. When service clients are created you can pass 203 in additional configuration via the aws.Config type to override configuration 204 provided by in the Session to create service client instances with custom 205 configuration. 206 207 Once the service's client is created you can use it to make API requests the 208 AWS service. These clients are safe to use concurrently. 209 210 ### Configuring the SDK 211 212 In the AWS SDK for Go, you can configure settings for service clients, such 213 as the log level and maximum number of retries. Most settings are optional; 214 however, for each service client, you must specify a region and your credentials. 215 The SDK uses these values to send requests to the correct AWS region and sign 216 requests with the correct credentials. You can specify these values as part 217 of a session or as environment variables. 218 219 See the SDK's [configuration guide][config_guide] for more information. 220 221 See the [session][session_pkg] package documentation for more information on how to use Session 222 with the SDK. 223 224 See the [Config][config_typ] type in the [aws][aws_pkg] package for more information on configuration 225 options. 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 234 When using the SDK you'll generally need your AWS credentials to authenticate 235 with AWS services. The SDK supports multiple methods of supporting these 236 credentials. By default the SDK will source credentials automatically from 237 its default credential chain. See the session package for more information 238 on this chain, and how to configure it. The common items in the credential 239 chain 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 251 Credentials can be configured in code as well by setting the Config's Credentials 252 value to a custom provider or using one of the providers included with the 253 SDK to bypass the default credential chain and use a custom one. This is 254 helpful when you want to instruct the SDK to only use a specific set of 255 credentials or providers. 256 257 This example creates a credential provider for assuming an IAM role, "myRoleARN" 258 and 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 275 See the [credentials][credentials_pkg] package documentation for more information on credential 276 providers included with the SDK, and how to customize the SDK's usage of 277 credentials. 278 279 The SDK has support for the shared configuration file (~/.aws/config). This 280 support can be enabled by setting the environment variable, "AWS_SDK_LOAD_CONFIG=1", 281 or enabling the feature in code when creating a Session via the 282 Option'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 294 In addition to the credentials you'll need to specify the region the SDK 295 will use to make AWS API requests to. In the SDK you can specify the region 296 either with an environment variable, or directly in code when a Session or 297 service client is created. The last value specified in code wins if the region 298 is specified multiple ways. 299 300 To set the region via the environment variable set the "AWS_REGION" to the 301 region you want to the SDK to use. Using this method to set the region will 302 allow you to run your application in multiple regions without needing additional 303 code in the application to select the region. 304 305 AWS_REGION=us-west-2 306 307 The endpoints package includes constants for all regions the SDK knows. The 308 values are all suffixed with RegionID. These values are helpful, because they 309 reduce the need to type the region string manually. 310 311 To set the region on a Session use the aws package's Config struct parameter 312 Region to the AWS region you want the service clients created from the session to 313 use. This is helpful when you want to create multiple service clients, and 314 all 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 322 See the [endpoints][endpoints_pkg] package for the AWS Regions and Endpoints metadata. 323 324 In addition to setting the region when creating a Session you can also set 325 the region on a per service client bases. This overrides the region of a 326 Session. This is helpful when you want to create service clients in specific 327 regions 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 335 See the [Config][config_typ] type in the [aws][aws_pkg] package for more information and additional 336 options 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 342 Once the client is created you can make an API request to the service. 343 Each API method takes a input parameter, and returns the service response 344 and an error. The SDK provides methods for making the API call in multiple ways. 345 346 In this list we'll use the S3 ListObjects API as an example for the different 347 ways 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 371 In addition to the API operations the SDK also includes several higher level 372 methods that abstract checking for and waiting for an AWS resource to be in 373 a desired state. In this list we'll use WaitUntilBucketExists to demonstrate 374 the 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 384 The API method will document which error codes the service might return for 385 the operation. These errors will also be available as const strings prefixed 386 with "ErrCode" in the service client's package. If there are no errors listed 387 in the API's SDK documentation you'll need to consult the AWS service's API 388 documentation 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 415 Pagination helper methods are suffixed with "Pages", and provide the 416 functionality needed to round trip API page requests. Pagination methods 417 take 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 436 Waiter helper methods provide the functionality to wait for an AWS resource 437 state. These methods abstract the logic needed to check the state of an 438 AWS resource, and wait until that resource is in a desired state. The waiter 439 will block until the resource is in the state that is desired, an error occurs, 440 or the waiter times out. If a resource times out the error code returned will 441 be 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 459 Please use these community resources for getting help. We use the GitHub issues 460 for 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/aavshr/aws-sdk-go/issues/new/choose). 466 467 This 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 471 If you encounter a bug with the AWS SDK for Go we would like to hear about it. 472 Search the [existing issues](https://github.com/aavshr/aws-sdk-go/issues) and see 473 if others are also experiencing the issue before opening a new issue. Please 474 include the version of AWS SDK for Go, Go language, and OS you’re using. Please 475 also include reproduction case when appropriate. 476 477 The GitHub issues are intended for bug reports and feature requests. For help 478 and questions with using AWS SDK for Go please make use of the resources listed 479 in the [Getting Help](https://github.com/aavshr/aws-sdk-go#getting-help) section. 480 Keeping the list of open issues lean will help us respond in a timely manner. 481 482 ## Contributing 483 484 We 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 488 For 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 496 is a general introduction on how to configure and make requests with the SDK. 497 If this is your first time using the SDK, this documentation and the API 498 documentation will help you get started. This document focuses on the syntax 499 and behavior of the SDK. The [Service Developer Guide](https://aws.amazon.com/documentation/) 500 will 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 503 document to look up all API operation input and output parameters for AWS 504 services supported by the SDK. The API reference also includes documentation of 505 the SDK, and examples how to using the SDK, service client API operations, and 506 API operation require parameters. 507 508 [Service Documentation](https://aws.amazon.com/documentation/) - Use this 509 documentation to learn how to interface with AWS services. These guides are 510 great for getting started with a service, or when looking for more 511 information about a service. While this document is not required for coding, 512 services may supply helpful samples to look out for. 513 514 [SDK Examples](https://github.com/aavshr/aws-sdk-go/tree/main/example) - 515 Included in the SDK's repo are several hand crafted examples using the SDK 516 features 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/aavshr/aws-sdk-go/issues 525 [pr]: https://github.com/aavshr/aws-sdk-go/pulls 526 [license]: http://aws.amazon.com/apache2.0/