1 | # Multer Storage Cloudinary
|
2 |
|
3 | A multer storage engine for Cloudinary. Also consult the [Cloudinary API](https://github.com/cloudinary/cloudinary_npm).
|
4 |
|
5 | ## Installation
|
6 |
|
7 | ```sh
|
8 | npm install multer-storage-cloudinary
|
9 | ```
|
10 |
|
11 | ## Usage
|
12 |
|
13 | ```javascript
|
14 | const cloudinary = require('cloudinary').v2;
|
15 | const { CloudinaryStorage } = require('multer-storage-cloudinary');
|
16 | const express = require('express');
|
17 | const multer = require('multer');
|
18 |
|
19 | const app = express();
|
20 |
|
21 | const 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 |
|
30 | const parser = multer({ storage: storage });
|
31 |
|
32 | app.post('/upload', parser.single('image'), function (req, res) {
|
33 | res.json(req.file);
|
34 | });
|
35 | ```
|
36 |
|
37 | ### File properties
|
38 |
|
39 | File 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 |
|
49 | Storage can be configured using the `options` argument passed to the `CloudinaryStorage` constructor.
|
50 |
|
51 | ```javascript
|
52 | const { CloudinaryStorage } = require('multer-storage-cloudinary');
|
53 |
|
54 | const storage = new CloudinaryStorage({
|
55 | cloudinary: cloudinary,
|
56 | params: {
|
57 | // upload paramters
|
58 | },
|
59 | });
|
60 | ```
|
61 |
|
62 | All 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 |
|
69 | Each property in the params object (either directly or resolved from the function)
|
70 | can either be a static value or an async function that resolves to the required value.
|
71 | All 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 |
|
75 | Functional parameters are called on every request and can be used in the following way:
|
76 |
|
77 | ```javascript
|
78 | const cloudinary = require('cloudinary').v2;
|
79 | const { CloudinaryStorage } = require('multer-storage-cloudinary');
|
80 |
|
81 | const 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 |
|
95 | You can also provide all params using a single function
|
96 |
|
97 | ```javascript
|
98 | const cloudinary = require('cloudinary').v2;
|
99 | const { CloudinaryStorage } = require('multer-storage-cloudinary');
|
100 |
|
101 | const 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 |
|
117 | This library is written is typescript and so provides all types necessary for use
|
118 | in a typescript project.
|
119 |
|
120 | ## Testing
|
121 |
|
122 | The Cloudinary API must be configured using the `CLOUDINARY_URL` environment variable in order to run the tests.
|
123 | All test files are stored in a seperate Cloudinary folder, which is deleted after tests finish.
|
124 |
|
125 | ```sh
|
126 | npm test
|
127 | ```
|