Coder Social home page Coder Social logo

gcs-upload's Introduction

gcs-upload

This is a convenient wrapper around multer and google cloud storage API as a single express middleware to upload file from user to Google Cloud Storage.

It will change the field for the uploaded file with a url string like http://storage.googleapis.com/`bucket-name`/`filename` that you can save in database.

Requirements

  1. Make sure you have a google cloud project with billing enabled.
  2. Enable Google Storage API for the project.
  3. Create a service account and download the credential in JSON format.

Installation

npm install gcs-upload

Usage

options:

  • limit: limits of uploaded data in object form (similar to limits option in multer)
  • gcsConfig:
    • keyFilename: file path for credential that you have downloaded before.
    • bucketName: the bucket name that will contain the uploaded file, you can create one through google cloud console.

It will return an upload object that have 3 methods: (.single(), .array(), and .fields()). You can use all of them just like how you would use multer.

Basic Example

Don't forget the enctype="multipart/form-data" in your form.

<form action="/upload-single" method="post" enctype="multipart/form-data">
  <input type="file" name="file" />
</form>
const express = require('express')
const gcsUpload = require('gcs-upload')

const app = express()

// .......

const upload = gcsUpload({
  limits: {
    fileSize: 1e6 // in bytes
  },
  gcsConfig: {
    keyFilename: '/Users/me/google-credential-keyfile.json',
    bucketName: 'my-bucket'
  }
})

app.post('/upload-single', upload.single('file'), (req, res) => {
  console.log(req.body)
  res.end()
})

app.post('/upload-array', upload.array('files'), (req, res) => {
  console.log(req.body)
  res.end()
})

app.post('/upload-fields',
  upload.fields([{ name: 'file1' }, { name: 'file2' }]),
  (req, res) => {
    console.log(req.body)
    res.end()
  }
)

// .......

To make uploaded files available for public view, add Storage Object Viewer role for allUsers. Step by step instruction can be found here

gcs-upload's People

Contributors

armedi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.