Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

multer-cloud-storage

alexandre-steinberg31.9kMIT4.1.0TypeScript support: included

Multer storage engine for Google Cloud Storage

multer, node, gcs, google, cloud, storage

readme

multer-cloud-storage

last commit npm version MIT License node packagephobia install size packagephobia publish size

multer-cloud-storage is a multer custom store engine for Google Cloud Storage service. It is a fork from ARozar's 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.

Installation

npm install multer-cloud-storage --save

or

yarn add multer-cloud-storage

Usage

ES6

import * as multer from 'multer';
import * as express from 'express';
import MulterGoogleCloudStorage from 'multer-cloud-storage';

const app = express();

const uploadHandler = multer({
  storage: new MulterGoogleCloudStorage()
});

app.post('/upload', uploadHandler.any(), (req, res) => {
    console.log(req.files);
    res.json(req.files);
});

ES5 / Common.js imports

var multer = require("multer");
var express = require("express");
var multerGoogleStorage = require("multer-cloud-storage");
var app = express();
var uploadHandler = multer({
    storage: multerGoogleStorage.storageEngine()
});
app.post('/upload', uploadHandler.any(), function (req, res) {
    console.log(req.files);
    res.json(req.files);
});

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.

Google Cloud

Creating a storage bucket

For instructions on how to create a storage bucket see the following documentation from Google.

Obtaining credentials

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.

Credentials

Default method

If using the MulterGoogleCloudStorage class without passing in any configuration options then the following environment variables will need to be set:

  1. GCS_BUCKET, the name of the bucket to save to.
  2. GCLOUD_PROJECT, this is your projectId. It can be found in the json credentials that you generated.
  3. GCS_KEYFILE, this is the path to the json key that you generated.

Explicit method

The constructor of the MulterGoogleCloudStorage class can be passed an optional configuration object.

Parameter Name Type Sample Value Default Value Notes
acl string "publicRead" "private" Accepted values are defined in predefinedAcl options
autoRetry boolean true true
bucket string "mybucketname" |Takes precedence over GCS_BUCKET
contentType function (request, file): string |
contentType string "application/pdf" |If set, this value will be used in place of file.mimetype
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)
email string "test@test.com" |
filename function (request, file, callback): void |
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)
filenameEncoding boolean true false If passed true then it will encode the file name before uploading to Google Cloud Storage otherwise it will use same as filename
hideFilename boolean true false If set to true, an UUID v4 will be used as object filename and Content-Type will be undefined
keyFilename string "./key.json" |Takes precedence over GCS_KEYFILE
maxRetries number 5 3 |
projectId string "test-prj-1234" |Takes precedence over GCLOUD_PROJECT
uniformBucketLevelAccess boolean true |Signifies whether uniformBucketLevelAccess is enabled on the target bucket. When true, the predefinedAcl parameter is removed from requests as it causes 400 Bad Request responses.
#### Custom file naming function
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.
getFilename(req, file, cb) {
    cb(null,`${file.originalname}`);
}

Custom content-type function

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.

getContentType( req, file ) {
    return undefined;
}

Changes in multer API

When used with multer-cloud-storage, multer will present additional file information.

File information

Each file contains the following information:

Key Description Origin
fieldname Field name specified in the form multer
originalname Name of the file on the user's computer multer
encoding Encoding type of the file multer
mimetype Mime type of the file multer
bucket Bucket name multer-cloud-storage
destination The pseudo-folder to which the file has been saved multer-cloud-storage
filename The name of the file on Google Cloud Storage multer-cloud-storage
filenameEncoding If true then it will URL encode the file name multer-cloud-storage
path The full path to the uploaded file (basically destination+ filename) multer-cloud-storage
contentType Content-type defined for stored object multer-cloud-storage
size Size of the file in bytes multer-cloud-storage
uri Google Cloud Storage path multer-cloud-storage
linkUrl Download link for allowed users (may require authentication) multer-cloud-storage
selfLink The link to the stored object (used for Cloud Storage APIs requests) multer-cloud-storage

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v4.1.0 - 2025-05-26

Maintenance

  • Package updates: @google-cloud/storage 7.15.2 -> 7.16.0 @types/express 5.0.0 -> 5.0.2 @types/node 22.13.4 -> 22.15.21 multer 1.4.5-lts.1 -> 2.0.0 typescript 5.7.3 -> 5.8.3

Security

Addresses CVE-2025-47935 and CVE-2025-47944 (#15)

v4.0.0 - 2025-02-20

⚠ BREAKING CHANGES

  • Dropped support for Node.js <= 14.x

Feature

Add support for optional filenameEncoding (#14)

Maintenance

  • Package updates: @google-cloud/storage 6.12.0 -> 7.15.2 @types/express 4.17.17 -> 5.0.0 @types/jest 29.5.3 -> 29.5.14 @types/multer 1.4.7 -> 1.4.12 @types/node 20.4.2 -> 22.13.4 @types/urlencode 1.1.2 -> 1.1.4 jest 29.6.1 -> 29.7.0 rimraf 5.0.1 -> 6.0.1 typescript 5.1.6 -> 5.7.3 urlencode 1.1.0 -> 2.0.0 uuid 9.0.0 -> 11.1.0

v3.1.0 - 2023-07-16

Maintenance

  • Package updates: @google-cloud/storage 6.8.0 -> 6.12.0 @types/express 4.17.14 -> 4.17.17 @types/jest 29.2.4 -> 29.5.3 @types/node 18.11.11 -> 20.4.2 jest 29.3.1 -> 29.6.1 rimraf 3.0.2 -> 5.0.1 typescript 4.9.3 -> 5.1.6

v3.0.0 - 2022-12-07

⚠ BREAKING CHANGES

  • tsconfig.json target updated to ES6

Fixes

Add prepare script (#11) Remove unnecessary constraint where creds or keyfile is required for instansiating Storage (#12)

Maintenance

  • Package updates: @google-cloud/storage 6.5.2 -> 6.8.0 @types/jest 29.0.3 -> 29.2.4 @types/node 18.7.21 -> 18.11.11 jest 29.0.3 -> 29.3.1 typescript 4.8.3 -> 4.9.3

v2.9.0 - 2022-09-26

Fixes

Fix missing callback function (#9)

Maintenance

  • Package updates: @google-cloud/storage 5.8.5 -> 6.5.2 @types/express 4.17.12 -> 4.17.14 @types/jest 26.0.23 -> 29.0.3 @types/node 15.12.2 -> 18.7.21 jest 27.0.4 -> 29.0.3 multer 1.4.4 -> 1.4.5-lts.1 typescript 4.3.2 -> 4.8.3 uuid 8.3.2 -> 9.0.0

    v2.8.0 - 2021-06-14

Maintenance

  • Package updates: @google-cloud/storage 5.8.3 -> 5.8.5 @types/express 4.17.11 -> 4.17.12 @types/jest 26.0.22 -> 26.0.23 @types/node 14.14.37 -> 15.12.2 jest 26.6.3 -> 27.0.4 typescript 4.2.3 -> 4.3.2

v2.7.1 - 2021-03-30

Fixes

Fix constructor missing params type checking (#8) Add test check when using uniformBucketLevelAccess (#8)

v2.7.0 - 2021-03-30

Fixes

Update index.d.ts (#3) uniformBucketLevelAccess enabled support (#4) Allow credentials OR keyFileName (#5) Swap cloud.google.com to storage.googleapis.com (#6) blobFile scoping to field uploaded. (#7)

Other changes

  • Package updates: @google-cloud/storage 5.5.0 -> 5.8.3 @types/express 4.17.9 -> 4.17.11 @types/jest 26.0.15 -> 26.0.22 @types/multer 1.4.4 -> 1.4.5 @types/node 14.14.7 -> 14.14.37 typescript 4.0.5 -> 4.2.3 uuid 8.3.1 -> 8.3.2

v2.6.0 - 2020-11-16

Other changes

  • Package updates: @google-cloud/storage 5.3.0 -> 5.5.0 @types/express 4.17.8 -> 4.17.9 @types/jest 26.0.13 -> 26.0.15 @types/node 14.10.1 -> 14.14.7 jest 26.4.2 -> 26.6.3 typescript 3.9.7 -> 4.0.5 uuid 8.3.0 -> 8.3.1

v2.5.0 - 2020-11-16

Fixes

Merge Duong Le's fix: Add mising params in StorageEngine's constructor

v2.4.0 - 2020-09-11

Other changes

  • Package updates: @google-cloud/storage 5.1.1 -> 5.3.0 @types/express 4.17.6 -> 4.17.8 @types/jest 25.2.3 -> 26.0.13 @types/multer 1.4.3 -> 1.4.4 @types/node 14.0.13 -> 14.10.1 jest 26.1.0 -> 26.4.2 typescript 3.9.5 -> 3.9.7 uuid 8.1.0 -> 8.3.0

v2.3.0 - 2020-06-23

Bug Fixes

  • array() method issue: req.files present incorrect filename property

Other changes

  • Package updates: @google-cloud/storage 5.0.1 -> 5.1.1 @types/jest 25.2.3 -> 26.0.0 @types/node 14.0.5 -> 14.0.13 jest 26.0.1 -> 26.1.0 typescript 3.8.3 -> 3.9.5

v2.2.0 - 2020-05-25

⚠ BREAKING CHANGES

  • Dropped support for Node.js <= 8.x

Other changes

  • Package updates: @google-cloud/storage 4.7.0 -> 5.0.1 @types/jest 25.2.1 -> 25.2.3 @types/node 13.13.5 -> 14.0.5 @types/urlencode 1.1.1 -> 1.1.2 typescript 3.8.3 -> 3.9.3 uuid 8.0.0 -> 8.1.0

v2.1.1 - 2020-05-07

Fixes

  • destination is not urlencoded anymore

v2.1.0 - 2020-04-26

Features

  • New options to explicit constructor
    • contentType: string
    • destination: string
    • hideFilename: boolean
  • New information available on multer API file object
    • bucket
    • destination
    • filename
    • path
    • contenType
    • size
    • uri
    • linkUrl
    • selfLink

Fixes

  • Now package.json defines engine Node.js >= 10
    • Requirement of Google API v4.7.0, not correctly observed on v2.0.0

Other changes

  • Packages added:
    • jest
  • Packages removed:
    • commitizen
    • cz-conventional-changelog
    • husky
    • nyc
    • semantic-release

v2.0.0 - 2020-04-15

⚠ BREAKING CHANGES

  • Removed support for Node.js < 8.x

Features

  • Update Google Cloud Storage Node.js Client to v4.7.0
  • Update dependencies to latest versions (most for security fixes)