@opengis/fastify-table
Version:
core-plugins
33 lines (32 loc) • 1.12 kB
JavaScript
import { S3Client } from "@aws-sdk/client-s3";
import { createHash } from "crypto";
import config from "../.../../../../../../config.js";
const CLIENT_CACHE = {};
const getS3Client = ({ accessKeyId: accessKeyId1, secretAccessKey: secretAccessKey1, user, password, endpoint: endpoint1, host, region = "us-west-2", } = config.s3 || {}) => {
const accessKeyId = accessKeyId1 || user;
const endpoint = endpoint1 || host;
const secretAccessKey = secretAccessKey1 || password;
if (!accessKeyId || !secretAccessKey || !endpoint) {
return null;
}
const uniqueKey = createHash("md5")
.update(`${accessKeyId}${secretAccessKey}${endpoint}`)
.digest("hex");
if (CLIENT_CACHE[uniqueKey]) {
return CLIENT_CACHE[uniqueKey];
}
const client = new S3Client({
credentials: {
accessKeyId,
secretAccessKey,
},
endpoint,
forcePathStyle: true,
region,
});
CLIENT_CACHE[uniqueKey] = client;
return client;
};
const s3Client = getS3Client();
export { getS3Client, s3Client };
export default s3Client;