1 | # multer-cloud-storage
|
2 | [![last commit](https://badgen.net/github/last-commit/alexandre-steinberg/multer-cloud-storage)](https://github.com/alexandre-steinberg/multer-cloud-storage)
|
3 | [![npm version](https://badgen.net/npm/v/multer-cloud-storage)](https://www.npmjs.com/package/multer-cloud-storage)
|
4 | [![MIT License](https://badgen.net/npm/license/multer-cloud-storage)](https://opensource.org/licenses/MIT)
|
5 | [![node](https://badgen.net/npm/node/multer-cloud-storage)](https://nodejs.org/en/)
|
6 | [![packagephobia install size](https://badgen.net/packagephobia/install/multer-cloud-storage)](https://packagephobia.now.sh/result?p=multer-cloud-storage)
|
7 | [![packagephobia publish size](https://badgen.net/packagephobia/publish/multer-cloud-storage)](https://packagephobia.now.sh/result?p=multer-cloud-storage)
|
8 |
|
9 | multer-cloud-storage is a [multer](https://github.com/expressjs/multer) custom store engine for Google Cloud Storage service. It is a fork from ARozar's [multer-google-storage](https://github.com/ARozar/multer-google-storage) that uses latest version of Google Cloud's API, allows additional information (like destination and contentType) to be set and reduces module footprint.
|
10 |
|
11 | ## Installation
|
12 |
|
13 | npm install multer-cloud-storage --save
|
14 |
|
15 | or
|
16 |
|
17 | yarn add multer-cloud-storage
|
18 |
|
19 |
|
20 | ## Usage
|
21 | ### ES6
|
22 |
|
23 | import * as multer from 'multer';
|
24 | import * as express from 'express';
|
25 | import MulterGoogleCloudStorage from 'multer-cloud-storage';
|
26 |
|
27 | const app = express();
|
28 |
|
29 | const uploadHandler = multer({
|
30 | storage: new MulterGoogleCloudStorage()
|
31 | });
|
32 |
|
33 | app.post('/upload', uploadHandler.any(), (req, res) => {
|
34 | console.log(req.files);
|
35 | res.json(req.files);
|
36 | });
|
37 |
|
38 | ### ES5 / Common.js imports
|
39 |
|
40 | var multer = require("multer");
|
41 | var express = require("express");
|
42 | var multerGoogleStorage = require("multer-cloud-storage");
|
43 | var app = express();
|
44 | var uploadHandler = multer({
|
45 | storage: multerGoogleStorage.storageEngine()
|
46 | });
|
47 | app.post('/upload', uploadHandler.any(), function (req, res) {
|
48 | console.log(req.files);
|
49 | res.json(req.files);
|
50 | });
|
51 |
|
52 | NB: This package is written to work with es5 or higher. If you have an editor or IDE that can understand d.ts (typescript) type definitions you will get additional support from your tooling though you do not need to be using typescript to use this package.
|
53 |
|
54 | ## Google Cloud
|
55 | ### Creating a storage bucket
|
56 | For instructions on how to create a storage bucket see the following [documentation from Google](https://cloud.google.com/storage/docs/creating-buckets#storage-create-bucket-console).
|
57 |
|
58 | ### Obtaining credentials
|
59 | For instructions on how to obtain the JSON keyfile as well a *projectId* (contained in the key file) please refer to the following [documentation from Google](https://cloud.google.com/docs/authentication/getting-started).
|
60 |
|
61 | ### Credentials
|
62 | #### Default method
|
63 | If using the MulterGoogleCloudStorage class without passing in any configuration options then the following environment variables will need to be set:
|
64 | 1. GCS_BUCKET, the name of the bucket to save to.
|
65 | 2. GCLOUD_PROJECT, this is your projectId. It can be found in the json credentials that you generated.
|
66 | 3. GCS_KEYFILE, this is the path to the json key that you generated.
|
67 |
|
68 | #### Explicit method
|
69 | The constructor of the MulterGoogleCloudStorage class can be passed an optional configuration object.
|
70 |
|
71 | Parameter Name | Type | Sample Value | Default Value | Notes
|
72 | --- | --- | --- | --- | ---
|
73 | `acl`|`string`|`"publicRead"`|`"private"`|Accepted values are defined in [*predefinedAcl*](https://googleapis.dev/nodejs/storage/latest/global.html#CreateWriteStreamOptions) options
|
74 | `autoRetry`|`boolean`|`true`| `true`|
|
75 | `bucket`|`string`|`"mybucketname"`| |Takes precedence over GCS_BUCKET
|
76 | `contentType`|`function`|`(request, file): string`| |
|
77 | `contentType`|`string`|`"application/pdf"`| |If set, this value will be used in place of *file.mimetype*
|
78 | `destination`|`string`|`"my_folder/"`|`""`|Despite Google Cloud Storage service stores objects in a flat name space, it is possible to list and filter them in a tree-like structure (more on [How Subdirectories Work article](https://cloud.google.com/storage/docs/gsutil/addlhelp/HowSubdirectoriesWork))
|
79 | `email`|`string`|`"test@test.com"`| |
|
80 | `filename`| `function`|`(request, file, callback): void`| |
|
81 | `filename`| `string`|`"my_file.pdf"`| |If defined, this name will be used in place of *file.originalname* - use with caution, because the object can be easily overwritten (consider to configure [Object Versioning and Concurrency Control](https://cloud.google.com/storage/docs/gsutil/addlhelp/ObjectVersioningandConcurrencyControl))
|
82 | `hideFilename`|`boolean`|`true`|`false`|If set to *true*, an UUID v4 will be used as object filename and *Content-Type* will be undefined
|
83 | `keyFilename`|`string`|`"./key.json"`| |Takes precedence over GCS_KEYFILE
|
84 | `maxRetries`|`number`|`5`|`3`| |
|
85 | `projectId`|`string`|`"test-prj-1234"`| |Takes precedence over GCLOUD_PROJECT
|
86 |
|
87 | #### Custom file naming function
|
88 | If you need to customize the naming of files then you are able to provide a function that will be called before uploading the file. The third argument of the function must be a standard node callback so pass any error in the first argument (or null on sucess) and the string name of the file on success.
|
89 |
|
90 | getFilename(req, file, cb) {
|
91 | cb(null,`${file.originalname}`);
|
92 | }
|
93 |
|
94 | #### Custom content-type function
|
95 | If you need to customize the content-type of files then you are able to provide a function that will be called before uploading the file.
|
96 |
|
97 | getContentType( req, file ) {
|
98 | return undefined;
|
99 | }
|
100 |
|
101 | ## Changes in multer API
|
102 |
|
103 | When used with multer-cloud-storage, multer will present additional file information.
|
104 |
|
105 | ### File information
|
106 |
|
107 | Each file contains the following information:
|
108 |
|
109 | Key | Description | Origin
|
110 | --- | --- | ---
|
111 | `fieldname` | Field name specified in the form | `multer`
|
112 | `originalname` | Name of the file on the user's computer | `multer`
|
113 | `encoding` | Encoding type of the file | `multer`
|
114 | `mimetype` | Mime type of the file | `multer`
|
115 | `bucket` | Bucket name | `multer-cloud-storage`
|
116 | `destination` | The pseudo-folder to which the file has been saved | `multer-cloud-storage`
|
117 | `filename` | The name of the file on Google Cloud Storage | `multer-cloud-storage`
|
118 | `path` | The full path to the uploaded file (basically `destination`+ `filename`) | `multer-cloud-storage`
|
119 | `contentType` | Content-type defined for stored object | `multer-cloud-storage`
|
120 | `size` | Size of the file in bytes | `multer-cloud-storage`
|
121 | `uri` | Google Cloud Storage path | `multer-cloud-storage`
|
122 | `linkUrl` | Download link for allowed users (may require authentication) | `multer-cloud-storage`
|
123 | `selfLink` | The link to the stored object (used for Cloud Storage APIs requests) | `multer-cloud-storage` |
\ | No newline at end of file |