Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

nodejs aws s3 upload

import multer from 'multer'
import multerS3 from 'multer-s3'
import aws from 'aws-sdk'
import { Request } from 'express'

aws.config.update({
	accessKeyId: process.env.AWS_ACCESS_KEY_ID,
	secretAccessKey: process.env.AWS_ACCESS_KEY
})

export class Multer {
	public static upload = multer({
		storage: multerS3({
			s3: new aws.S3(),
			bucket: process.env.AWS_BUCKET_NAME,
			contentType: multerS3.AUTO_CONTENT_TYPE,
			serverSideEncryption: 'AES256',
			acl: 'public-read',
			key: function (request: Request, file: Express.Multer.File, done: any) {
				const fileName: string = `${Date.now().toString()} - ${file.originalname}`
				done(null, fileName)
			}
		}),
		fileFilter: (req: Request, file: Express.Multer.File, done: any) => {
			if (!mimeTypeSupport(file.mimetype)) throw new TypeError('mimetype not supported')
			const fileName: string = `${Date.now().toString()} - ${file.originalname}`
			done(null, fileName)
		}
	}).array('upload', 100)
}
 
PREVIOUS NEXT
Tagged: #nodejs #aws #upload
ADD COMMENT
Topic
Name
7+2 =