Socket
Socket
Sign inDemoInstall

multer-s3-sharp-resizer

Package Overview
Dependencies
133
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    multer-s3-sharp-resizer

storage engine for multer to upload multiple images of different sizes to s3 object storage


Version published
Weekly downloads
4
Maintainers
1
Install size
106 MB
Created
Weekly downloads
 

Readme

Source

Multer S3 Sharp Resizer

Multer S3 Sharp Resizer is a storage engine for multer.

It addresses a common problem you run into when creating a website that should utilize different sizes of images previously uploaded. You can easily setup the storage engine to create multiple versions of an image that has been uploaded.

Multer S3 Sharp Resizer is written completely in TypeScript and thus all types are provided with it.

Setup

To set up the Storage engine with multer you need to create it like this. Now I will be using TypeScript in the following sections but you can set it up with JavaScript just as easy.

Here we want Multer S3 to upload a thumbnail image in addition to the original image. So the configuration of multer would look like this

my-multer.ts

import AWS from "aws-sdk"
import multer from "multer"
import S3SharpStorage from "multer-s3-sharp-resizer";
import myS3Config from "./somewhere.ts";

const storage = new S3SharpStorage({
  s3: new AWS.S3(myS3Config),
  bucket: 'my-bucket',
  imageFormats: [
    {
      fileFormat: 'jpg',
      folder: 'thumbnails'
      resize: {
        width: 100,
        height: 100
      }
    }
  ]
});

export default multer({
  storage
});

Configuring the route is just the same as in any regular multer setup

route.ts

import Multer from "./my-multer";
import express from "express";

const router = express.Router();

router.post('/',
  multer.single('image'),
  (req: Request, res: Response): => {
    res.status(201).json(req.file)
  }
)

For additional scenarios please see the multer documentation

Options

And just like this the storage engine is setup. Now there are multiple Options you can choose from.

OptionDefaultTypeMandatoryDescription
s3undefinedAWS.S3yesThe S3 Service object from aws-sdk
bucketundefinedstringyesBucket you want your files to be stored in
uploadOriginalImagetruebooleannoSet to false if you don't want your original image to be uploaded
metadataSee MetadataFunctionnoFunction to add metadate to your files.
keybasesee Key BaseFunctionnoFunction to create a key for your file
imageFormats[]see Image FormatsnoThe different Image Formats you want to upload

Metadata

If you want to add Metadata to your files you can define the metadata function. It returns an object of type Record<string,string> which will be added as key value metadata pairs to your file.

An example would look like this if we wanted to add a specific category to metadata that was sent in the query params of the http request.

my-multer.ts

const storage = new S3SharpStorage({
  s3: new AWS.S3(myS3Config),
  bucket: 'my-bucket',
  imageFormats: [
    {
      fileFormat: 'jpg',
      folder: 'thumbnails',
      resize: {
        width: 100,
        height: 100
      }
    }
  ],
  metadata: (req: Request, file: Express.Multer.File) => {
    return {
      category: req.query.category
    }
  }
});

Key Base

By default the Key will be generated as a 16 character hex hash by crypto. However you can replace it with your own function like this. Remember that as we can upload different file formats in this case the key will be without a file ending. In this case we might want to change that instead of setting a category in metadata we want the image to be in the corresponding folder. So this image would in the end go to

bucket/thumbnails/[category]/[key].jpg

my-multer.ts

const storage = new S3SharpStorage({
  s3: new AWS.S3(myS3Config),
  bucket: 'my-bucket',
  uploadOriginalImage: false,
  imageFormats: [
    {
      fileFormat: 'jpg',
      folder: 'thumbnails',
      resize: {
        width: 100,
        height: 100
      }
    }
  ],
  keyBase: (req: Request, file?: Express.Multer.File) => {
    const hash = crypto.randomBytes(16).toString('hex')
    return `${req.query.category}/${hash}`
  }
});

Image Formats

Now we already saw the image formats briefly in the previous sections. Each format determines what file type will be uploaded etc.

OptionDefaultTypeMandatoryDescription
fileFormatundefined'png', 'jpg', 'webp'yesThe file type you want to be saved
folder''stringnoThe folder you want your image to be saved in
resizeundefinedsharp.ResizeOptionsnoSharp Configuration for your resize. See sharps documentation

You can use all the options for resize that are provided by their API

Deletion of Images

Now the Storage engine comes with a removeKey method which will make sure that if you want to delete an image you just need to provide one key. Now as you most likely will make a request from a website this method will be called including a file extension. It doesn't matter though which one you provide given that you maybe decided to opload webp's as well as jpg's. Multer S3 Storage Engine will find it. To delete a key just implement it like this in your route

my-multer.ts

import AWS from "aws-sdk"
import multer from "multer"
import S3SharpStorage from "multer-s3-sharp-resizer";
import myS3Config from "./somewhere.ts";

const storage = new S3SharpStorage({
  s3: new AWS.S3(myS3Config),
  bucket: 'my-bucket',
  imageFormats: [
    {
      fileFormat: 'jpg',
      folder: 'thumbnails'
      resize: {
        width: 100,
        height: 100
      }
    }
  ]
});

export { storage }

export default multer({
  storage
});

route.ts

import { storage } from "./my-multer"

const router = express.Router();

router.delete('/:Filename', async (req: Request, res: Response): => {
    const deleted = await storage.removeKey(req.params.Filename);
    res.status(200).json(deleted)
  }
)

Keywords

FAQs

Last updated on 19 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc