# multer-storage-cloudinary

> A Cloudinary multer storage engine

Latest version **4.0.0** (published 2020-05-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install multer-storage-cloudinary
pnpm add multer-storage-cloudinary
yarn add multer-storage-cloudinary
bun add multer-storage-cloudinary
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2020-05-29 |
| First published | 2016-06-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 37.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 110 |
| Author | Affan Shahid |
| Maintainers | affanshahid |
| Keywords | multer, cloudinary, storage, engine, upload, typescript |

## Links

- npm: https://www.npmjs.com/package/multer-storage-cloudinary
- Repository: https://github.com/affanshahid/multer-storage-cloudinary
- Homepage: https://github.com/affanshahid/multer-storage-cloudinary#readme
- Issues: https://github.com/affanshahid/multer-storage-cloudinary/issues
- npm.io page: https://npm.io/package/multer-storage-cloudinary

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2020-05-29
- 3.0.1 — 2020-05-27
- 3.0.0 — 2020-05-26
- 2.2.1 — 2016-11-01
- 1.2.1 — 2016-06-26

## README

# Multer Storage Cloudinary

A multer storage engine for Cloudinary. Also consult the [Cloudinary API](https://github.com/cloudinary/cloudinary_npm).

## Installation

```sh
npm install multer-storage-cloudinary
```

## Usage

```javascript
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](https://github.com/cloudinary/cloudinary_npm#upload):

| Key        | Description                         |
| ---------- | ----------------------------------- |
| `filename` | public_id of the file on cloudinary |
| `path`     | A URL for fetching the file         |
| `size`     | Size of the file in bytes           |

### Options

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

```javascript
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:

| Parameter            | Description                                                                                                                                                                                                                                           | Type                      |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
| `options.cloudinary` | A Cloudinary API object <br>The API must be configured by the user                                                                                                                                                                                    | `object` <br>**required** |
| `options.params`     | An object or a function that resolves to an object which can contain any/all properties described in the [Cloudinary upload API docs](https://cloudinary.com/documentation/image_upload_api_reference#upload_method). Read below for more information | `object` 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](https://cloudinary.com/documentation/image_upload_api_reference#upload_method) 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:

```javascript
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

```javascript
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.

```sh
npm test
```

---
_Source: https://npm.io/package/multer-storage-cloudinary · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
