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

preguntada hace 2 años869 visualizaciones
1 Respuesta
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
INGENIERO DE SOPORTE
respondido hace 2 años
  • Thanks! I was looking for this solution everywhere.

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas