Question : nodejs express multer s3
Answered by : restu-wahyu-saputra
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)
}
Source : | Last Update : Wed, 12 Jan 22
Question : multer in express.js
Answered by : zeamanual-feleke
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
})
Source : https://www.npmjs.com/package/multer | Last Update : Thu, 10 Mar 22