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.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南