Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including multer-s3-rotate with all npm packages installed. Try it out:

var multerS3Rotate = require("multer-s3-rotate")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

multer-s3-rotate v1.1.0

Extended multer-s3, resizing and rotating image using EXIF orientation.

Multer S3

This project is mostly an extension of multer-s3. As many images from an iPhone were rotating while uploading, we extended the code to solve this issue using sharp.

Installation

npm install --save multer-s3-rotate

Usage

var aws = require('aws-sdk')
var express = require('express')
var multer = require('multer')
var multerS3 = require('multer-s3-rotate')

var app = express()
var s3 = new aws.S3({ /* ... */ })

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

app.post('/upload', upload.array('photos', 3), function(req, res, next) {
  res.send('Successfully uploaded ' + req.files.length + ' files!')
})

Newly Added

To resize image, add resize and resizeOpts keys to the object

var aws = require('aws-sdk')
var express = require('express')
var multer = require('multer')
var multerS3 = require('multer-s3-rotate')

var app = express()
var s3 = new aws.S3({ /* ... */ })

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    resize: true // default false,
    resizeOpts: { // Required if resize is true
      width: 400,
      height:400,
      fit: 'inside'
    },
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

app.post('/upload', upload.array('photos', 3), function(req, res, next) {
  res.send('Successfully uploaded ' + req.files.length + ' files!')
})

Value for resize

  • resize ( Boolean ) If true, resize action occurs else only images are rotated

Keys for resizeOpts

  • width ( String || Number )
  • height ( String || Number )
  • fit ( String ) how the image should be resized to fit both provided dimensions, one of cover, contain, fill, inside or outside. (optional, default 'cover')
  • position ( String ) position, gravity or strategy to use when fit is cover or contain. (optional, default 'centre')
  • background ( String || Object ) background colour when using a fit of contain, parsed by the colormodule, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1})
  • kernel ( String ) the kernel to use for image reduction. (optional, default 'lanczos3')
  • withoutEnlargement ( Boolean ) do not enlarge if the width or height are already less than the specified dimensions, equivalent to GraphicsMagick's > geometry option. (optional, default false)
  • fastShrinkOnLoad ( Boolean ) take greater advantage of the JPEG and WebP shrink-on-load feature, which can lead to a slight moiré pattern on some images. (optional, default true)

Testing

The tests mock all access to S3 and can be run completely offline.

npm test
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free