Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

multer-storage-cloudinary

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multer-storage-cloudinary

A Cloudinary multer storage engine

  • 4.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Multer Storage Cloudinary

A multer storage engine for Cloudinary. Also consult the Cloudinary API.

Installation

npm install multer-storage-cloudinary

Usage

const cloudinary = require('cloudinary').v2;
const { CloudinaryStorage } = require('multer-storage-cloudinary');
const express = require('express');
const multer = require('multer');

const app = express();

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: {
    folder: 'some-folder-name',
    format: async (req, file) => 'png', // supports promises as well
    public_id: (req, file) => 'computed-filename-using-request',
  },
});

const parser = multer({ storage: storage });

app.post('/upload', parser.single('image'), function (req, res) {
  res.json(req.file);
});

File properties

File objects will expose the following properties mapped from the Cloudinary API:

KeyDescription
filenamepublic_id of the file on cloudinary
pathA URL for fetching the file
sizeSize of the file in bytes

Options

Storage can be configured using the options argument passed to the CloudinaryStorage constructor.

const { CloudinaryStorage } = require('multer-storage-cloudinary');

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: {
    // upload paramters
  },
});

All parameters are optional except the configured Cloudinary API object:

ParameterDescriptionType
options.cloudinaryA Cloudinary API object
The API must be configured by the user
object
required
options.paramsAn object or a function that resolves to an object which can contain any/all properties described in the Cloudinary upload API docs. Read below for more informationobject or function

Each property in the params object (either directly or resolved from the function) can either be a static value or an async function that resolves to the required value. All upload parameters specified in the Cloudinary docs are supported.

Note: public_id is different in that it must always be a functional parameter

Functional parameters are called on every request and can be used in the following way:

const cloudinary = require('cloudinary').v2;
const { CloudinaryStorage } = require('multer-storage-cloudinary');

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: {
    folder: (req, file) => 'folder_name',
    format: async (req, file) => {
      // async code using `req` and `file`
      // ...
      return 'jpeg';
    },
    public_id: (req, file) => 'some_unique_id',
  },
});

You can also provide all params using a single function

const cloudinary = require('cloudinary').v2;
const { CloudinaryStorage } = require('multer-storage-cloudinary');

const storage = new CloudinaryStorage({
  cloudinary: cloudinary,
  params: async (req, file) => {
    // async code using `req` and `file`
    // ...
    return {
      folder: 'folder_name',
      format: 'jpeg',
      public_id: 'some_unique_id',
    };
  },
});

Typescript

This library is written is typescript and so provides all types necessary for use in a typescript project.

Testing

The Cloudinary API must be configured using the CLOUDINARY_URL environment variable in order to run the tests. All test files are stored in a seperate Cloudinary folder, which is deleted after tests finish.

npm test

Keywords

FAQs

Package last updated on 29 May 2020

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc