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

gefragt vor 2 Jahren869 Aufrufe
1 Antwort
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-TECHNIKER
beantwortet vor 2 Jahren
  • Thanks! I was looking for this solution everywhere.

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen