Last active
October 12, 2021 07:05
-
-
Save u007/495149c1047c2cc3faa7b8924d5921de to your computer and use it in GitHub Desktop.
multer s3 contentType fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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