Skip to content

Instantly share code, notes, and snippets.

@u007
Last active October 12, 2021 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u007/495149c1047c2cc3faa7b8924d5921de to your computer and use it in GitHub Desktop.
Save u007/495149c1047c2cc3faa7b8924d5921de to your computer and use it in GitHub Desktop.
multer s3 contentType fix
import stream from 'stream';
const mime = require('mime-types');
import aws from 'aws-sdk';
import multer from 'multer';
import multerS3 from 'multer-s3';
multer({
//...,
storage: multerS3({
s3: aws.S3({
accessKeyId: process.env.accessKeyId,
secretAccessKey: process.env.secretAccessKey,
region: process.env.region,
}),
contentType: (req: Express.Request, file: Express.Multer.File, cb: (error: any, mime?: string, stream?: NodeJS.ReadableStream) => void) => {
const contentType = mime.lookup(file.originalname);
file.stream.once('data', function (firstChunk) {
var outStream = new stream.PassThrough();
outStream.write(firstChunk);
file.stream.pipe(outStream);
cb(null, contentType, outStream);
});
},
//...
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment