UNPKG

@opengis/fastify-table

Version:

core-plugins

42 lines (33 loc) 986 B
import { S3Client } from '@aws-sdk/client-s3'; import { createHash } from 'crypto'; function md5(string) { return createHash('md5').update(string).digest('hex'); } const CLIENT_CACHE = {}; const getS3Client = ({ accessKeyId, secretAccessKey, user, password, endpoint, host, region = 'us-west-2', }) => { // md5 have a hign chance of collisions consider switching on highload // if (!accessKeyId || !secretAccessKey || !endpoint) throw new Error('No required params'); const uniqueKey = md5(`${accessKeyId}${secretAccessKey}${endpoint}`); let client = CLIENT_CACHE[uniqueKey]; if (client) return client; client = new S3Client({ credentials: { accessKeyId: accessKeyId || user, secretAccessKey: secretAccessKey || password, }, endpoint: endpoint || host, forcePathStyle: true, region, }); CLIENT_CACHE[uniqueKey] = client; return client; }; export default { getS3Client };