Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS JS SDK の Proxy 対応コードを最初から入れておく方が良いかも知れない #142

Open
dynamis opened this issue Feb 2, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@dynamis
Copy link
Contributor

dynamis commented Feb 2, 2024

AWS JS SDK はローカル試験で Proxy 利用時に S3Client の初期化時パラメータとしてリクエストハンドラの追加が必要であるが、入れ忘れで詰まる方も結構いるため対応済みバージョンのコードを入れた方が有り難いかもしれない。

https://github.com/webdino/lyceum-pokemon/blob/skeleton-halfway/server/utils/s3Client.js

import { NodeHttpHandler } from "@aws-sdk/node-http-handler";
import { ProxyAgent } from "proxy-agent";
import { S3Client } from "@aws-sdk/client-s3";
 
const agent = new ProxyAgent();
const config = useRuntimeConfig();

const client = new S3Client({
  region: config.region, 
  requestHandler: new NodeHttpHandler({
    httpAgent: agent,
    httpsAgent: agent,
  }),
});

export default s3Client;

的なコード (要動作検証) だと思うのだけど、AWS JS SDK の公式サンプルが最近 s3Client.js を別途使わない (そんなの分ける意味ねぇって私が愚痴ってたらその通りになった) 形に更新されてましたので、同時に s3Client.js じゃなくて server/utils/trainer.js 内に統合しても良いかも知れない (しそのままでも良い)。

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/s3/actions/list-objects.js

このままだと本番デプロイしたときに無駄なものが挟まるのは良くないと思えば環境変数か何かで分岐するコードとしてもよいかもだが、どうも ProxyAgent 自体が proxy 設定環境変数がなければそのまま動くという振る舞いなので (import の無駄とかはあるのだけど) 直接ツッコミでも良い気はする。

@knokmki612
Copy link
Contributor

AWS JS SDK の公式サンプルが最近 s3Client.js を別途使わない (そんなの分ける意味ねぇって私が愚痴ってたらその通りになった) 形に更新されてましたので、同時に s3Client.js じゃなくて server/utils/trainer.js 内に統合しても良いかも知れない (しそのままでも良い)。

現状server/utils/trainer.js 以外で使用していませんので、統合しても何ら不都合ないように思えます

@knokmki612
Copy link
Contributor

knokmki612 commented Feb 2, 2024

どうも ProxyAgent 自体が proxy 設定環境変数がなければそのまま動く

前回以前の研修でも受講者のなかにはproxy-agent設定済みでApp Runnerへデプロイされた方もいらっしゃるかと思うので経験則的にはそうでしたね。

以下を見る限り、proxy 設定環境変数がない場合デフォルトのhttp.Agent、https.Agentインスタンスが渡され動作に影響ないようです。なので初期状態でproxy-agent導入済みで良さそうに思えます。

https://github.com/TooTallNate/proxy-agents/blob/c881a1804197b89580320b87082971c3c6a61746/packages/proxy-agent/src/index.ts#L120-L123

The default http.globalAgent that is used by http.request() has all of these values set to their respective defaults.
To configure any of them, a custom http.Agent instance must be created.

https://nodejs.org/api/http.html#new-agentoptions より

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants