anacronw / multer-s3

multer storage engine for amazon s3
MIT License
662 stars 193 forks source link

Problems with getting req.file.size in express #202

Open ktalap opened 1 year ago

ktalap commented 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 image image

Router integration image

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, };

  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 );

ktalap commented 1 year ago

Okay the point was, when we accept more than 1 form-data fields, we get the files data that way: image So you gotta access it that way req.files.file[0]