Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including multer-cloud-storage with all npm packages installed. Try it out:

var multerCloudStorage = require("multer-cloud-storage")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

multer-cloud-storage v2.4.0

Multer storage engine for Google Cloud Storage

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 NameTypeSample ValueDefault ValueNotes
aclstring"publicRead""private"Accepted values are defined in predefinedAcl options
autoRetrybooleantruetrue
bucketstring"mybucketname"Takes precedence over GCS_BUCKET
contentTypefunction(request, file): string
contentTypestring"application/pdf"If set, this value will be used in place of file.mimetype
destinationstring"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)
emailstring"test@test.com"
filenamefunction(request, file, callback): void
filenamestring"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)
hideFilenamebooleantruefalseIf set to true, an UUID v4 will be used as object filename and Content-Type will be undefined
keyFilenamestring"./key.json"Takes precedence over GCS_KEYFILE
maxRetriesnumber53
projectIdstring"test-prj-1234"Takes precedence over GCLOUD_PROJECT

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:

KeyDescriptionOrigin
fieldnameField name specified in the formmulter
originalnameName of the file on the user's computermulter
encodingEncoding type of the filemulter
mimetypeMime type of the filemulter
bucketBucket namemulter-cloud-storage
destinationThe pseudo-folder to which the file has been savedmulter-cloud-storage
filenameThe name of the file on Google Cloud Storagemulter-cloud-storage
pathThe full path to the uploaded file (basically destination+ filename)multer-cloud-storage
contentTypeContent-type defined for stored objectmulter-cloud-storage
sizeSize of the file in bytesmulter-cloud-storage
uriGoogle Cloud Storage pathmulter-cloud-storage
linkUrlDownload link for allowed users (may require authentication)multer-cloud-storage
selfLinkThe link to the stored object (used for Cloud Storage APIs requests)multer-cloud-storage

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free