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

已提问 2 年前869 查看次数
1 回答
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
支持工程师
已回答 2 年前
  • Thanks! I was looking for this solution everywhere.

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则