Last active
October 8, 2020 08:48
-
-
Save SiddharthaChowdhury/eb86a664ca212bd056309d066e2bfadd to your computer and use it in GitHub Desktop.
AWS_S3_UPLOAD
This file contains hidden or 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 AWS from 'aws-sdk'; | |
import multer, { FileFilterCallback } from "multer" | |
import multerS3 from "multer-s3"; | |
import {Request} from 'express'; | |
import { v4 as uuidv4 } from 'uuid'; | |
AWS.config.update({ | |
accessKeyId: process.env.AWS_ACCESS_KEY_ID, | |
secretAccessKey: process.env.AWS_SECRET_KEY, | |
signatureVersion: 'v4' | |
}); | |
export const S3 = new AWS.S3(); | |
const isAllowedMimetype = (mime: string) => ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/x-ms-bmp', 'image/webp'].includes(mime.toString()); | |
const fileFilter = (req: Request, file: Express.Multer.File, callback: FileFilterCallback) => { | |
const fileMime = file.mimetype; | |
if(isAllowedMimetype(fileMime)) { | |
callback(null, true) | |
} else { | |
callback(null, false) | |
} | |
} | |
const getUniqFileName = (originalname: string) => { | |
const name = uuidv4(); | |
const ext = originalname.split('.').pop(); | |
return `${name}.${ext}`; | |
} | |
export const handleUploadMiddleware = multer({ | |
fileFilter, | |
storage: multerS3({ | |
s3: S3, | |
bucket: process.env.AWS_BUCKET_NAME!, | |
acl: 'public-read', | |
contentType: multerS3.AUTO_CONTENT_TYPE, | |
key: function (req: Request, file: any, cb) { | |
const fileName = getUniqFileName(file.originalname); | |
const s3_inner_directory = 'public_asset'; | |
const finalPath = `${s3_inner_directory}/${fileName}`; | |
file.newName = fileName; | |
cb(null, finalPath ); | |
} | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment