🌳
pt0/bktF/serverF/getS3ClientF.mts
1import { S3Client } from '@aws-sdk/client-s3'
2import { NodeHttpHandler } from '@smithy/node-http-handler'
4import { defaultS3Region } from './cfgF.mts'
6export const getS3Client = ({bucket_name, socketTimeoutMs, keepAlive}: {bucket_name: string, socketTimeoutMs?: number, keepAlive?: boolean}) => {
7 const cfg = getS3ReqCfg({bucket_name}) as Record<string, string>
9 let {region, endpoint, s3ForcePathStyle, accessKeyId, secretAccessKey} = cfg
10 region ||= defaultS3Region
12 const handlerOpts: Record<string, any> = {}
13 if (socketTimeoutMs !== undefined) handlerOpts.socketTimeout = socketTimeoutMs
14 if (keepAlive !== undefined) handlerOpts.httpsAgent = {keepAlive}
16 const s3 = new S3Client({
17 requestChecksumCalculation: 'WHEN_REQUIRED',
18 forcePathStyle: !!s3ForcePathStyle,
19 region, endpoint,
20 credentials: {
21 accessKeyId, secretAccessKey,
22 },
23 ...(Object.keys(handlerOpts).length ? {requestHandler: new NodeHttpHandler(handlerOpts)} : {}),
24 })
26 return s3