UNPKG

4.61 kBMarkdownView Raw
1# Multer Storage Cloudinary
2
3A multer storage engine for Cloudinary. Also consult the [Cloudinary API](https://github.com/cloudinary/cloudinary_npm).
4
5## Installation
6
7```sh
8npm install multer-storage-cloudinary
9```
10
11## Usage
12
13```javascript
14const cloudinary = require('cloudinary').v2;
15const { CloudinaryStorage } = require('multer-storage-cloudinary');
16const express = require('express');
17const multer = require('multer');
18
19const app = express();
20
21const storage = new CloudinaryStorage({
22 cloudinary: cloudinary,
23 params: {
24 folder: 'some-folder-name',
25 format: async (req, file) => 'png', // supports promises as well
26 public_id: (req, file) => 'computed-filename-using-request',
27 },
28});
29
30const parser = multer({ storage: storage });
31
32app.post('/upload', parser.single('image'), function (req, res) {
33 res.json(req.file);
34});
35```
36
37### File properties
38
39File objects will expose the following properties mapped from the [Cloudinary API](https://github.com/cloudinary/cloudinary_npm#upload):
40
41| Key | Description |
42| ---------- | ----------------------------------- |
43| `filename` | public_id of the file on cloudinary |
44| `path` | A URL for fetching the file |
45| `size` | Size of the file in bytes |
46
47### Options
48
49Storage can be configured using the `options` argument passed to the `CloudinaryStorage` constructor.
50
51```javascript
52const { CloudinaryStorage } = require('multer-storage-cloudinary');
53
54const storage = new CloudinaryStorage({
55 cloudinary: cloudinary,
56 params: {
57 // upload paramters
58 },
59});
60```
61
62All parameters are optional except the configured Cloudinary API object:
63
64| Parameter | Description | Type |
65| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------- |
66| `options.cloudinary` | A Cloudinary API object <br>The API must be configured by the user | `object` <br>**required** |
67| `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` |
68
69Each property in the params object (either directly or resolved from the function)
70can either be a static value or an async function that resolves to the required value.
71All upload parameters specified in the [Cloudinary docs](https://cloudinary.com/documentation/image_upload_api_reference#upload_method) are supported.
72
73_Note: `public_id` is different in that it must always be a functional parameter_
74
75Functional parameters are called on every request and can be used in the following way:
76
77```javascript
78const cloudinary = require('cloudinary').v2;
79const { CloudinaryStorage } = require('multer-storage-cloudinary');
80
81const storage = new CloudinaryStorage({
82 cloudinary: cloudinary,
83 params: {
84 folder: (req, file) => 'folder_name',
85 format: async (req, file) => {
86 // async code using `req` and `file`
87 // ...
88 return 'jpeg';
89 },
90 public_id: (req, file) => 'some_unique_id',
91 },
92});
93```
94
95You can also provide all params using a single function
96
97```javascript
98const cloudinary = require('cloudinary').v2;
99const { CloudinaryStorage } = require('multer-storage-cloudinary');
100
101const storage = new CloudinaryStorage({
102 cloudinary: cloudinary,
103 params: async (req, file) => {
104 // async code using `req` and `file`
105 // ...
106 return {
107 folder: 'folder_name',
108 format: 'jpeg',
109 public_id: 'some_unique_id',
110 };
111 },
112});
113```
114
115### Typescript
116
117This library is written is typescript and so provides all types necessary for use
118in a typescript project.
119
120## Testing
121
122The Cloudinary API must be configured using the `CLOUDINARY_URL` environment variable in order to run the tests.
123All test files are stored in a seperate Cloudinary folder, which is deleted after tests finish.
124
125```sh
126npm test
127```