Coder Social home page Coder Social logo

gcloud-storage-api's Introduction

gcloud-storage-api

An easy google storage tool to upload file and generate public link as return (both file uploading and buffer streaming).

A wrapper base on gcloud, which around the google storage functions for upload (both file uploading and streaming) and delete.NPM

Install

$ npm install --save gcloud-storage-api

Functions of this package:

googleAPI.uploadLocalFile(BUCKET_NAME, fileName, fileLocalPath)
googleAPI.uploadBuffer(BUCKET_NAME, fileName, buffer)
googleAPI.deleteStorageFile(url)

Usage

To get the projectId and key.json, it is needed to Visit the Google Developers Console. There is the offical instruction from gcloud: https://www.npmjs.com/package/gcloud

Initialization

Create a service account key on Google Cloud Console:

alt tag

Key type, select JSON, and then a service-key.json is generated and able to download:

alt tag

Paste the service-key to root of NODE project

alt tag

service-key.json

alt tag

Initializing gcloud

var googleAPI = require('gcloud-storage-api')
googleAPI.init('projectId',"service-key.json")

Upload local file

  • googleAPI.formParsing is a fomidable function which parsing local file with POST is multipart
  • googleAPI.uploadLocalFile(BUCKET_NAME, fileName, fileLocalPath) is a function that does local file uploading, respose the public link and resolve a promise.
## Usage
app.post('/upload', googleAPI.formParsing, function(req, res, next) {

    var fileName = "path/to/"+req.uploadFile.name
    var fileLocalPath  = req.uploadFile.path
    var BUCKET_NAME = "yourbucket"

    googleAPI.uploadLocalFile(BUCKET_NAME, fileName, fileLocalPath).then(function(result) {
        console.log("finished uploadLocalFile: "+result)
        res.send(result)
    },function(err){
        console.log(err)
    })
});

Upload Streaming/Buffer

  • fileName = "path/to/upload/abc.file"
  • googleAPI.uploadBuffer(BUCKET_NAME, fileName, buffer) is a function that does buffer uploading, respose the public link and resolve a promise.
app.post('/streamupload', googleAPI.formParsing, function(req, res, next) {

    var fileName = "path/to/"+req.uploadFile.name
    var fileLocalPath  = req.uploadFile.path
    var BUCKET_NAME = "peterbucket"

    fs.readFile(fileLocalPath,function(err, buffer){
        googleAPI.uploadBuffer(BUCKET_NAME, fileName, buffer).then(function(result) {
            console.log("after googleAPI.deleteDownloadableFile: "+result)
            res.send(result)
        })
    })
});

Delete storage file

app.post('/delete', function(req, res, next) {
    var url = req.query.url
    googleAPI.deleteStorageFile(url).then(function(result) {
        console.log("after googleAPI.deleteStorageFile: "+result)
        res.send(result)
    })
});

License

MIT

gcloud-storage-api's People

Contributors

wahengchang avatar

Watchers

 avatar  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.