AWS Developer Tools Blog
V2 AWS SDK for Go adds Context to API operations
As of January 19th, 2021, the AWS SDK for Go, version 2 (v2) is generally available.
The v2 AWS SDK for Go developer preview made a breaking change in the release of v0.8.0. The v0.8.0 release added a new parameter, context.Context, to the SDK’s Send
and Paginate Next
methods.
Context
was added as a required parameter to the Send
and Paginate Next
methods to enable you to use the v2 SDK for Go in your application with cancellation and request tracing.
Using the Context pattern helps reduce the chance of code paths mistakenly dropping the Context
, causing the cancellation and tracing chain to be lost. When the Context
is lost, it can be difficult to track down the missing cancellation and tracing metrics within an application.
Migrating to v0.8.0
After you update your application to depend on v0.8.0 of the v2 SDK, you’ll encounter compile errors. This is because of the Context
parameter that was added to the Send
and Paginate Next
methods.
If your application is already using the Context
pattern, you can now pass the Context
into Send
and Paginate Next
methods directly, instead of calling SetContext
on the request returned by the client’s operation request method.
If you don’t need a Context
within your application, you can use context.Background()
or context.TODO()
instead of specifying a Context
, such as a timeout, deadline, cancel, or httptrace.ClientTrace.
Example code: before v0.8.0
The following code is an example of an application using the Amazon S3 service’s PutObject API operation with the v2 SDK before v0.8.0. The example code is
using the req.SetContext
method to specify the Context
for the PutObject
operation.
func uploadObject(ctx context.Context, bucket, key string, obj io.ReadSeeker) error
req := svc.PutObjectRequest(&s3.PutObjectInput{
Bucket: &bucket,
Key: &key,
Body: obj,
})
req.SetContext(ctx)
_, err := req.Send()
return err
}
Example code: updated to v0.8.0
To migrate the previous example code to use v0.8.0 of the v2 SDK, we need to remove the req.SetContext
method call, and pass the Context
directly to
the Send
method instead. This change will make the example code compatible with v0.8.0 of the v2 SDK.
func uploadObject(ctx context.Context, bucket, key string, obj io.ReadSeeker) error
req := svc.PutObjectRequest(&s3.PutObjectInput{
Bucket: &bucket,
Key: &key,
Body: obj,
})
_, err := req.Send(ctx)
return err
}
What’s next for the v2 SDK for Go developer preview?
We’re working to improve usability and reduce pain points with the v2 SDK. Two specific areas we’re looking at are the SDK’s request lifecycle and error handling.
Improving the SDK’s request lifecycle will help reduce your application’s CPU and memory performance when using the SDK. It also makes it easier for you to extend and modify the SDK’s core functionality.
For the SDK’s error handling, we’re investigating alternative approaches, such as typed errors for API operation exceptions. By using typed errors, your application can assert directly against the error type. This would reduce the need to do string comparisons for SDK API operation response errors.
See our issues on Github to share your feedback, questions, and feature requests, and to stay current with the v2 AWS SDK for Go developer preview as it moves to GA.