Code Monkey home page Code Monkey logo

pipe-to-storage's Introduction

pipe-to-storage

Build Status Known Vulnerabilities Language grade: JavaScript Total alerts

Importing and Setup

On Google platforms:

// storage version 2.x

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();

// storage version 1.x

const storage = require('@google-cloud/storage')(); // storage 1.x

On other platforms: set up your API key, see relevant docs

Pass the storage object when setting up pipeToStorage for usage

const pipeToStorage = require('pipe-to-storage')(storage);

Usage

pipeToStorage(source, bucketName, fileName , optional )
.then(function(what){ 
   console.log("hooray! I wrote "+what.file+" to bucket "+what.bucket+" and it should have md5 "+what.md5);
 })
 .catch(function(e){
   console.log("oh no! an error occurred. here it is:");
   console.log(e);
 });

source must be either:

  • a string, which will be sent as-is to the new file [retry will use the same string]
  • a function that returns a readable stream [retry will call the function to get a fresh readable stream]
  • a readable stream [can not retry]

npm:promise-retry will be used to retry failed storage promises in the first two cases. A retry strategy is used that is appropriate for a cloud back-end environment: up to 3 retries per call within 30 seconds, then fail.

bucketname and filename are the Google Cloud Storage[tm] bucket and file names. gs:// is not needed in bucketName

optional is optional and may be:

  • absent or undefined -- pipeToStorage should try to look up the content-type from the fileName extension
  • the string 'json' -- shorthand to set metadata content-type: application/json
  • other strings -- sets the metadata content-type manually
  • an object, to set any writeStream.options in the internal storage createWriteStream call

returns a Promise that resolves to {bucket, file, md5, length} after saving the contents of source to storage.bucket(bucketName).file(fileName) or rejects with any errors not caught in retrys

md5 is in base64

Tests

This module is tested on Travis CI, but you won't be able to run the same tests yourself without some adjustments.

To run the tests in your own environment, change the storage API credentials (projectId, keyFilename) and the bucket name referenced in ./test/index.js.

Copyright

Copyright 2017 Paul Brewer, Economic and Financial Technology Consulting LLC [email protected]

License

The MIT License

No relationship to Google, Inc.

This is third party software, not a product of Google Inc.

The author(s) have no relationship to Google, Inc.

pipe-to-storage's People

Contributors

drpaulbrewer avatar greenkeeper[bot] avatar

Watchers

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