How to use multer-s3 - 10 common examples

To help you get started, we’ve selected a few multer-s3 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lskjs / lskjs / packages / upload / src / server.js View on Github external
getS3Storage() {
    return multerS3({
      s3: this.s3,
      bucket: this.config.s3.bucket,
      contentType: multerS3.AUTO_CONTENT_TYPE,
      acl: 'public-read',
      key3: (req, file, cb) => {
        const filename = nodepath.parse(file.originalname);
        // console.log({ req, file });
        // console.log(`avatar_${req.user._id}.${filename.ext}`);
        cb(null, `avatar_${req.user._id}.${filename.ext}`);
      },
      key: async (req, file, cb) => {
        let filename;
        try {
          // console.log('req, file', file);
          filename = this.getFilePath(req, file);
          // console.log('filename', filename);
github lskjs / lskjs / packages / upload / src / server.js View on Github external
getS3Storage() {
    return multerS3({
      s3: this.s3,
      bucket: this.config.s3.bucket,
      contentType: multerS3.AUTO_CONTENT_TYPE,
      acl: 'public-read',
      key3: (req, file, cb) => {
        const filename = nodepath.parse(file.originalname);
        // console.log({ req, file });
        // console.log(`avatar_${req.user._id}.${filename.ext}`);
        cb(null, `avatar_${req.user._id}.${filename.ext}`);
      },
      key: async (req, file, cb) => {
        let filename;
        try {
          // console.log('req, file', file);
          filename = this.getFilePath(req, file);
          // console.log('filename', filename);
          filename = filename.replace(/\//g, '__');
          // console.log('filename2', filename);
        } catch (err) {
github ditojs / dito / packages / server / src / storage / S3Storage.js View on Github external
constructor(app, config) {
    super(app, config)
    const {
      name,
      s3,
      acl,
      bucket,
      contentType,
      ...options
    } = config
    this.s3 = new aws.S3(s3)
    this.acl = acl
    this.bucket = bucket

    this.setStorage(multerS3({
      s3: this.s3,
      acl,
      bucket,
      contentType: contentType || multerS3.AUTO_CONTENT_TYPE,
      ...options,

      key: (req, file, cb) => {
        cb(null, this.getUniqueFilename(file.originalname))
      },

      metadata: (req, file, cb) => {
        // Store the determined width and height as meta-data on the s3 object
        // as well. You never know, it may become useful :)
        const { width, height } = file
        if (width != null || height != null) {
          cb(null, {
github tabvn / fileapp-reactjs / api / src / index.js View on Github external
// Setup Email

let email = nodemailer.createTransport(smtp);


// File storage config

const storageDir = path.join(__dirname, '..', 'storage');


//const upload = multer({ storage: storageConfig }); // local upload.

const upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: s3Bucket,
        metadata: function (req, file, cb) {
            cb(null, {fieldName: file.fieldname});
        },
        key: function (req, file, cb) {
            const filename = `${Date.now().toString()}-${file.originalname}`;
            cb(null, filename)
        }
    })
})



// End file storage config
github kennethlumalicay / betterweb / src / server.js View on Github external
var app = express();
mongoose.connect(process.env.MONGO_URI);
mongoose.Promise = global.Promise;

// Session
app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: true,
  store: new mongoStore({ mongooseConnection: mongoose.connection })
}));

// File Upload
const s3 = new aws.S3();
const upload = multer({
  storage: multers3({
    s3: s3,
    bucket: process.env.S3_BUCKET_NAME,
    metdata: function(req, file, cb) {
      cb(null, {fieldName: file.fieldName});
    },
    key: function(req, file, cb) {
      cb(null, req.query.uid + Date.now().toString(36) + path.extname(file.originalname));
    }
  }),
  limits: {
    fileSize: 1500000
  },
  fileFilter: function(req, file, cb) {
    switch(path.extname(file.originalname)) {
      case '.jpg':
      case '.png':
github koddsson / micropub-media-endpoint / app.js View on Github external
contentType(req, file, cb) {
      // A hack that pipes the output stream through the exif transformer before after we detect the content type
      // of the stream but before we send it to S3
      multerS3.AUTO_CONTENT_TYPE(req, file, function(_, mime, outputStream) {
        cb(null, mime, outputStream.pipe(new ExifTransformer({readableObjectMode: true, writableObjectMode: true})))
      })
    },
    cacheControl: 'max-age=31536000',
github ditojs / dito / packages / server / src / storage / S3Storage.js View on Github external
name,
      s3,
      acl,
      bucket,
      contentType,
      ...options
    } = config
    this.s3 = new aws.S3(s3)
    this.acl = acl
    this.bucket = bucket

    this.setStorage(multerS3({
      s3: this.s3,
      acl,
      bucket,
      contentType: contentType || multerS3.AUTO_CONTENT_TYPE,
      ...options,

      key: (req, file, cb) => {
        cb(null, this.getUniqueFilename(file.originalname))
      },

      metadata: (req, file, cb) => {
        // Store the determined width and height as meta-data on the s3 object
        // as well. You never know, it may become useful :)
        const { width, height } = file
        if (width != null || height != null) {
          cb(null, {
            width: `${width}`,
            height: `${height}`
          })
        } else {
github argos-ci / argos / src / server / routes / api.js View on Github external
import { formatUrlFromBuild } from 'modules/urls/buildUrl'
import Build from 'server/models/Build'
import Repository from 'server/models/Repository'
import ScreenshotBucket from 'server/models/ScreenshotBucket'
import buildJob from 'server/jobs/build'
import errorHandler from 'server/middlewares/errorHandler'

const router = new express.Router()
const s3 = new S3({
  signatureVersion: 'v4',
})
const upload = multer({
  storage: multerS3({
    s3,
    bucket: config.get('s3.screenshotsBucket'),
    contentType: multerS3.AUTO_CONTENT_TYPE,
  }),
})

/**
 * Takes a route handling function and returns
 * a function that wraps it in a `try/catch`. Caught
 * exceptions are forwarded to the `next` handler.
 */
export function errorChecking(routeHandler) {
  return async (req, res, next) => {
    try {
      await routeHandler(req, res, next)
    } catch (err) {
      // Handle objection errors
      const candidates = [err.status, err.statusCode, err.code, 500]
      err.status = candidates.find(Number.isInteger)
github argos-ci / argos / src / server / routes / api.js View on Github external
import config from 'config'
import bodyParser from 'body-parser'
import { getRedisLock } from 'server/services/redis'
import { formatUrlFromBuild } from 'modules/urls/buildUrl'
import Build from 'server/models/Build'
import Repository from 'server/models/Repository'
import ScreenshotBucket from 'server/models/ScreenshotBucket'
import buildJob from 'server/jobs/build'
import errorHandler from 'server/middlewares/errorHandler'

const router = new express.Router()
const s3 = new S3({
  signatureVersion: 'v4',
})
const upload = multer({
  storage: multerS3({
    s3,
    bucket: config.get('s3.screenshotsBucket'),
    contentType: multerS3.AUTO_CONTENT_TYPE,
  }),
})

/**
 * Takes a route handling function and returns
 * a function that wraps it in a `try/catch`. Caught
 * exceptions are forwarded to the `next` handler.
 */
export function errorChecking(routeHandler) {
  return async (req, res, next) => {
    try {
      await routeHandler(req, res, next)
    } catch (err) {
github minhuyen / generator-expressjs-rest / generators / app / templates / backend / src / services / s3.js View on Github external
import mime from 'mime';
import crypto from 'crypto';
import path from 'path';
import fs from 'fs';
import multer from 'multer';
import multerS3 from 'multer-s3';
import aws from 'aws-sdk';

import config from '../config';

const s3 = new aws.S3({
  accessKeyId: config.aws.accessKeyId,
  secretAccessKey: config.aws.secretAccessKey
});

const s3Storage = multerS3({
  s3: s3,
  bucket: config.aws.bucketName,
  acl: 'public-read',
  contentType: multerS3.AUTO_CONTENT_TYPE,
  metadata: function(req, file, cb) {
    cb(null, { fieldName: file.fieldname });
  },
  key: function(req, file, cb) {
    crypto.pseudoRandomBytes(16, function(err, raw) {
      cb(
        null,
        raw.toString('hex') + Date.now() + '.' + mime.extension(file.mimetype)
      );
    });
  }
});

multer-s3

Streaming multer storage engine for AWS S3

MIT
Latest version published 2 years ago

Package Health Score

62 / 100
Full package analysis

Popular multer-s3 functions