Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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

express multer

$ npm install --save multer

var express = require("express");
var multer = require('multer');
var upload = multer({dest:'uploads/'});
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript match against array 
Javascript :: form data to json 
Javascript :: how to alert in javascript 
Javascript :: Get Input arrays 
Javascript :: order by type 
Javascript :: javascript xhr set parameters 
Javascript :: json ld product schema 
Javascript :: shopify get list of all products ajax api 
Javascript :: await in node js 
Javascript :: scrape data from ao3 
Javascript :: what is react mounting 
Javascript :: run javascript in flutter 
Javascript :: javascript find method 
Javascript :: split the string on any and all periods, question mark using regex 
Javascript :: dynamic array of months js 
Javascript :: how to push object in array in javascript 
Javascript :: exit node 
Javascript :: getusermedia close stream 
Javascript :: js.l2 
Javascript :: how to use socket io in production 
Javascript :: show ad on facebook game 
Javascript :: Resize Image Using HTML Canvas in JavaScript 
Javascript :: timestamp discord.js 
Javascript :: href before onclick js 
Javascript :: Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.4.6/$injector/modulerr?p0=myApp 
Javascript :: vuejs slots events 
Javascript :: change dictionary value in React js 
Javascript :: calcular sobra de divisão de parcelas js 
Javascript :: creating room in ws nodejs 
Javascript :: javascript switch statement 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =