Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

nodejs express multer s3

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)
}
Comment

multer in express.js

const express = require('express')
const multer  = require('multer')
const upload = multer({ dest: 'uploads/' })

const app = express()

app.post('/profile', upload.single('avatar'), function (req, res, next) {
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

app.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
  // req.files is array of `photos` files
  // req.body will contain the text fields, if there were any
})

const cpUpload = upload.fields([{ name: 'avatar', maxCount: 1 }, { name: 'gallery', maxCount: 8 }])
app.post('/cool-profile', cpUpload, function (req, res, next) {
  // req.files is an object (String -> Array) where fieldname is the key, and the value is array of files
  //
  // e.g.
  //  req.files['avatar'][0] -> File
  //  req.files['gallery'] -> Array
  //
  // req.body will contain the text fields, if there were any
})
Comment

PREVIOUS NEXT
Code Example
Typescript :: generic in typescript 
Typescript :: init tsconfig file 
Typescript :: typerscript online compiler 
Typescript :: how to validate email address in typescript 
Typescript :: laravel validation check if email exists forget password 
Typescript :: how to add id in array javascript 
Typescript :: how to get value from observable 
Typescript :: simple input for games javascript 
Typescript :: api service in angular 
Typescript :: ANGULAR: create component in module 
Typescript :: google sheets new line 
Typescript :: find unique values between 2 lists R 
Typescript :: replace element in array typescript 
Typescript :: array of objects value repeat check 
Typescript :: select constraints in sql 
Typescript :: typescript treat all errors as warnings 
Typescript :: ether.js 
Typescript :: typescript function as type 
Typescript :: comments visual studio code html 
Typescript :: remove elements from array that has same value from other array 
Typescript :: validation minlength angular 
Typescript :: create and use constants in angularjs 
Typescript :: does any event get triggered when checked value changes programatically? 
Typescript :: js pop object from id 
Typescript :: Two sets of parentheses after function call 
Typescript :: parse object typescript 
Typescript :: 2 positional arguments but 3 were given 
Typescript :: How to disable form control but keep value 
Typescript :: typescript readonly 
Typescript :: typescript delete value from map 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =