@opengis/fastify-table
Version:
core-plugins
35 lines (34 loc) • 1.44 kB
JavaScript
import { Upload } from "@aws-sdk/lib-storage";
import s3Client from "../client.js";
import config from "../../../../../../config.js";
import getValidData from "../../utils/getValidData.js";
import getDataSize from "../../utils/getDataSize.js";
import dataTypes from "../../utils/handlers/dataTypes.js";
import getMimeType from "../../mime/index.js";
import getS3FilePath from "./utils/getS3FilePath.js";
const uploadFile = (s3Settings) => async (fp, data) => {
const filepath = getS3FilePath(fp, s3Settings);
const validData = await getValidData({ data, types: [dataTypes.stream] });
const size = await getDataSize({ data });
const type = getMimeType(filepath);
try {
const bucketParams = {
Bucket: s3Settings?.containerName || config.s3?.containerName || "work",
Key: filepath,
ContentLength: size,
ContentType: type,
Body: validData,
};
const parallelUpload = new Upload({
client: s3Client,
params: bucketParams,
});
const res = await parallelUpload.done();
// const res = await s3Client.send(new PutObjectCommand(bucketParams));
return res; // For unit tests.
}
catch (err) {
throw new Error(`Upload error s3: ${err}. Message: ${err.message}. Keys: ${Object.keys(err)}. Filepath: ${filepath}. Data type: ${typeof validData}.`);
}
};
export default uploadFile;