gcloud-storage-api

1.2.2 • Public • Published

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

Package Sidebar

Install

npm i gcloud-storage-api

Weekly Downloads

2

Version

1.2.2

License

ISC

Last publish

Collaborators

  • wahengchang