Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nmwh917/multer-google-storage

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nmwh917/multer-google-storage

Streaming multer storage engine for Google Cloud Storage

  • 0.0.0-development
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

multer-google-storage

Travis build

This is a multer storage engine for google's file storage.

Installation

npm install multer-google-storage --save

or

yarn add multer-google-storage

Usage

ES6

import * as  multer from 'multer';
import * as express from 'express';
import MulterGoogleCloudStorage from 'multer-google-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-google-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 Value
autoRetrybooleantrue
emailstring"test@test.com"
keyFilenamestring"./key.json"
maxRetriesnumber2
projectIdstring"test-prj-1234"
filenamefunction(request, file, callback): void
bucketstring"mybucketname"
contentTypefunction(request, file): string
aclstring"publicread"
Custom file naming

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,`${uuid()}_${file.originalname}`);
}

Keywords

FAQs

Package last updated on 15 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc