Open ktalap opened 1 year ago
That is the code I use as middlewear
I try to access the file size at the: req.file = { name: file.originalname, encoding: file.encoding, type: file.mimetype, bucketFileKey: keyName, };
Multer itself
Router integration
In text form: const multerS3 = require("multer-s3"); const multer = require("multer"); const uuid = require("uuid");
import { S3Client } from "@aws-sdk/client-s3";
const s3 = new S3Client({ region: "eu", credentials: { accessKeyId: process.env.AWS_ACCESS_KEY_ID || "", secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || "", }, endpoint: process.env.AWS_END_POINT, forcePathStyle: true, });
export const upload = multer({ storage: multerS3({ s3: s3, bucket: "user-files", acl: "private", key: function (req: any, file: any, cb: any) { const uniqueFileName = uuid.v4(); const keyName = ${req.user.email}/${uniqueFileName}; req.file = { name: file.originalname, encoding: file.encoding, type: file.mimetype, bucketFileKey: keyName, };
${req.user.email}/${uniqueFileName}
cb(null, keyName); }, metadata: function (req: any, file: any, cb: any) { cb(null, { size: file.size }); },
}), });
I integrate it that way
router.post( "/upload", upload.fields([{ name: "file" }, { name: "folder" }]), postUploadFile );
Okay the point was, when we accept more than 1 form-data fields, we get the files data that way: So you gotta access it that way req.files.file[0]
That is the code I use as middlewear
I try to access the file size at the: req.file = { name: file.originalname, encoding: file.encoding, type: file.mimetype, bucketFileKey: keyName, };
Multer itself
Router integration
In text form: const multerS3 = require("multer-s3"); const multer = require("multer"); const uuid = require("uuid");
import { S3Client } from "@aws-sdk/client-s3";
const s3 = new S3Client({ region: "eu", credentials: { accessKeyId: process.env.AWS_ACCESS_KEY_ID || "", secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || "", }, endpoint: process.env.AWS_END_POINT, forcePathStyle: true, });
export const upload = multer({ storage: multerS3({ s3: s3, bucket: "user-files", acl: "private", key: function (req: any, file: any, cb: any) { const uniqueFileName = uuid.v4(); const keyName =
${req.user.email}/${uniqueFileName}
; req.file = { name: file.originalname, encoding: file.encoding, type: file.mimetype, bucketFileKey: keyName, };}), });
I integrate it that way
router.post( "/upload", upload.fields([{ name: "file" }, { name: "folder" }]), postUploadFile );