AWS SDK for Go V2 - Add user agent to the AWS client

1

Hi, As part of Migrating to the AWS SDK for Go V2, I was trying to find a replacement for the next lines of code:

sess, err := session.NewSession(cfg)
sess.Handlers.Build.PushFrontNamed(addUserAgent)

// addUserAgent is a named handler that will add information to requests made by the AWS SDK.
var addUserAgent = request.NamedHandler{
	Name: "UserAgentHandler",
	Fn:   request.MakeAddToUserAgentHandler("Name", info.Version),
}

In V2 we use config instead of session, how can I register the user agent in V2? I checked the documentation but couldn't find an answer.

Thank you, Ori

asked 2 years ago856 views
1 Answer
2

Hello Ori,

User-Agent values can be set using the following methods:

AddUserAgentKey AddUserAgentKeyValue For example to add a key/value to the user-agent to a specific service client:

cfg, err := config.LoadDefaultConfig(context.Background()) if err != nil { panic(err) }

sfnClient := sfn.NewFromConfig(cfg, func(options *sfn.Options) { options.APIOptions = append(options.APIOptions, middleware.AddUserAgentKeyValue("customKey", "customValue")) })

APIOptions can also be mutated per-operation if wanting to add a specific User-Agent key per-operation, or can also be specified when calling config.LoadDefaultConfig if the user wants to apply the User-Agent to all clients created from the resulting aws.Config type.

AWS
SUPPORT ENGINEER
answered 2 years ago
  • Thanks! I was looking for this solution everywhere.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions