AWS SDK for Go
Developer Guide

AWS SDK for Go Request Examples

The AWS SDK for Go examples can help you write your own applications. The examples assume you have already set up and configured the SDK (that is, you have imported all required packages and set your credentials and region). For more information, see Getting Started with the AWS SDK for Go and Configuring the AWS SDK for Go.

Using context.Context with SDK Requests

In Go 1.7, the context.Context type was added to http.Request. This type provides an easy way to implement deadlines and cancellations on requests.

To use this pattern with the SDK, you call WithContext on the HTTPRequest field of the SDK's request.Request type, and provide your Context value. The following example highlights this process with a timeout on an Amazon SQSReceiveMessage API call.

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() // SQS ReceiveMessage params := &sqs.ReceiveMessageInput{ ... } req, resp := s.ReceiveMessageRequest(params) req.HTTPRequest = req.HTTPRequest.WithContext(ctx) err := req.Send()

Using API Field Setters with SDK Requests

In addition to setting API parameters by using struct fields, you can also use chainable setters on the API operation parameter fields. This enables you to use a chain of setters to set the fields of the API struct.

import ( "" "" "" "strings" ) func main() { // Initialize a session in us-west-2 that the SDK will use to load // credentials from the shared credentials file ~/.aws/credentials. sess, _ := session.NewSession(&aws.Config{ Region: aws.String("us-west-2")}, ) // Create S3 service client svc := s3.New(sess) svc.PutObject((&s3.PutObjectInput{}). SetBucket("myBucket"). SetKey("myKey"). SetBody(strings.NewReader("object body")). SetWebsiteRedirectLocation(""), ) }

You can also use this pattern with nested fields in API operation requests.

resp, err := svc.UpdateService((&ecs.UpdateServiceInput{}). SetService("myService"). SetDeploymentConfiguration((&ecs.DeploymentConfiguration{}). SetMinimumHealthyPercent(80), ), )