Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

multer upload fields nodejs s3

app.post('/upload', upload.fields([
  { name: 'avatar', maxCount: 1 },
  { name: 'gallery', maxCount: 8 }
]), function (req, res, next) {
    console.log('Uploaded!');
    res.send(req.files);
});
Comment

multer s3 file upload

import 'dotenv/config.js'
import { MulterModule, FileFieldsInterceptor, FilesInterceptor, FileInterceptor } from '@nestjs/platform-express'
import { mimeTypeSupport } from '@helpers/helper.mimeType'
import aws from 'aws-sdk'
import { Request } from 'express'
import multer from 'multer'
import multerS3 from 'multer-s3'
import fs from 'fs'

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

export class Multer {
	private static acl: String = 'ACL'

	static awsStorage: multer.StorageEngine = multerS3({
		s3: new aws.S3({
			credentials: new aws.Credentials({
				accessKeyId: process.env.AWS_ACCESS_KEY_ID,
				secretAccessKey: process.env.AWS_ACCESS_KEY
			}),
			hostPrefixEnabled: true,
			computeChecksums: true,
			correctClockSkew: true
		}),
		bucket: process.env.AWS_BUCKET_NAME,
		contentType: multerS3.AUTO_CONTENT_TYPE,
		serverSideEncryption: 'AES256',
		[Multer.acl as any]: 'public-read',
		metadata(_req: Request, file: Express.Multer.File, done: any) {
			if (!file) done(new Error('Get file upload failed'), null)
			done(null, file)
		},
		key(_req: Request, file: Express.Multer.File, done: any) {
			done(null, `${Date.now()}.${file.originalname.split('.')[1]}`)
		}
	})

	static fileFilter(_req: Request, file: Express.Multer.File, done: any) {
		if (!mimeTypeSupport(file.mimetype)) throw new TypeError('mimetype not supported')
		if (file.size >= 5242880) throw new TypeError('maximum file or image size must be 5 MB or under 5 MB')

		const fileName: string = `${Date.now()}.${file.originalname.split('.')[1]}`
		done(null, fileName)
	}
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript mocha Cannot use import statement outside a module 
Typescript :: behaviour 
Typescript :: type char typescript 
Typescript :: union type property does not exist 
Typescript :: express server in vscode extension 
Typescript :: importhtml google sheets multiple tables 
Typescript :: tiqets endpoints 
Typescript :: how to mark plots octave 
Typescript :: yarn create react app typescript 
Cpp :: latex piecewise function 
Cpp :: add c++ 
Cpp :: excel vba delete worksheet if exists 
Cpp :: how to print list in c++ 
Cpp :: celsius to kelvin formula 
Cpp :: how to print items in arduino 
Cpp :: how to convert qt string to string 
Cpp :: sfml delta time 
Cpp :: input output c++ 
Cpp :: master header file c++ 
Cpp :: grpah class data structure 
Cpp :: what are specialized classes c++ 
Cpp :: print linkedstack cpp 
Cpp :: iomanip 
Cpp :: set cmd size c++ 
Cpp :: c++ stream string into fiel 
Cpp :: c++ find key in hashmap 
Cpp :: how to hide ui elements unity 
Cpp :: c++ vector element search 
Cpp :: convert decimal to binary c++ 
Cpp :: c++ fibonacci 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =