如何更改 AWS 节点客户端用户代理?

How to change the AWS node client user agent?

我正在使用节点 aws-sdk 程序包,我需要在 S3 请求中发送自定义用户代理,以便在控制台日志中识别进程。

我在 Java SDK 中看到了执行此操作的方法,但我在节点包中看不到任何类似的方法。

有什么方法可以轻松做到这一点?

您可以根据 here:

在发送给构造函数的 optionshttpoptions 字段中定义代理

httpOptions (map) — A set of options to pass to the low-level HTTP request.

Currently supported options are:

  • proxy [String] — the URL to proxy requests through

  • agent [http.Agent, https.Agent] — the Agent object to perform HTTP requests with. Used for connection pooling. Defaults to the global agent (http.globalAgent) for non-SSL connections. Note that for SSL connections, a special Agent object is used in order to enable peer certificate verification. This feature is only available in the Node.js environment.

  • connectTimeout [Integer] — Sets the socket to timeout after failing to establish a connection with the server after connectTimeout milliseconds. This timeout has no effect once a socket connection has been established.

  • timeout [Integer] — Sets the socket to timeout after timeout milliseconds of inactivity on the socket. Defaults to two minutes (120000).

  • xhrAsync [Boolean] — Whether the SDK will send asynchronous HTTP requests. Used in the browser environment only. Set to false to send requests synchronously. Defaults to true (async on).

这就是你要找的吗?

浏览源代码后,我发现了一个未记录的选项来设置用户代理:customUserAgent

const options = { customUserAgent: 'my-process-name' };
const client  = new AWS.S3(options);