I have an application that sends zip files to aws S3, but I need to unzip that directory first, and then send all the files and send them to my bucket. I've been trying for a few hours and I have no success, here is my code:
import multer from "multer";
import path from "path";
import crypto from "crypto";
import aws from "aws-sdk";
import multerS3 from "multer-s3-transform";
const MAX_SIZE_TWO_MEGABYTES = 2 * 1024 * 1024 * 1024;
const storageTypes = {
local: multer.diskStorage({
destination: (request, file, callback) => {
callback(
null,
path.resolve(__dirname, "..", "..", "tmp", "uploads")
);
},
filename: (request, file, callback) => {
crypto.randomBytes(16, (error, hash) => {
if (error) callback(error);
file.key = `${hash.toString("hex")}-${file.originalname}`;
callback(null, file.key);
});
},
}),
s3: multerS3({
s3: new aws.S3(),
bucket: process.env.BUCKET_NAME,
/**
* Down here I think the magic should happen
*/
// transforms: [
// {
// key: (request, file, callback) => {
// console.log("TransformsKey");
// callback(null, true);
// },
// transform: (request, file, callback) => {
// console.log("Transform");
// callback(null, true);
// },
// },
// ],
contentType: multerS3.AUTO_CONTENT_TYPE,
acl: "public-read",
key: (request, file, callback) => {
crypto.randomBytes(16, (error, hash) => {
if (error) callback(error);
const fileName = `${hash.toString("hex")}-${file.originalname}`;
callback(null, fileName);
});
},
}),
};
And here I export my settings
module.exports = {
dest: path.resolve(__dirname, "..", "..", "tmp", "uploads"),
storage: storageTypes[process.env.STORAGE_TYPE],
limits: {
fileSize: MAX_SIZE_TWO_MEGABYTES,
},
fileFilter: (request, file, callback) => {
const allowedMimes = [
"image/jpeg",
"image/pjpeg",
"image/png",
"image/gif",
"application/zip",
"application/rar",
];
if (allowedMimes.includes(file.mimetype)) {
callback(null, true);
} else {
callback(new erroror("Invalid file type."));
}
},
};
Ps.: Today i use multer like a Middleware
I'm trying some libs and other examples, but I'm not getting anyway.
I hope help.