mongoose-crate-gcs

mongoose-crate StorageProvider that uploads files to Google Cloud Services

Usage no npm install needed!

<script type="module">
  import mongooseCrateGcs from 'https://cdn.skypack.dev/mongoose-crate-gcs';
</script>

README

mongoose-crate-gcs

Dependency Status devDependency Status Build Status Coverage Status

A StorageProvider for mongoose-crate that stores files in Google Cloud Storage.

Usage

var mongoose = require('mongoose'),
  crate = require('mongoose-crate'),
  GCS = require('mongoose-crate-gcs')

var PostSchema = new mongoose.Schema({
  title: String,
  description: String
})

PostSchema.plugin(crate, {
  storage: new GCS({
    iss: 'A Google service account email',
    bucket: 'Google Cloud Storage bucket',
    keyFile: '/path/to/keyfile', // pass either key or keyFile
    key: 'key as a string', // pass either key or keyFile
    scope: '<scope-here>', // defaults to https://www.googleapis.com/auth/devstorage.full_control
    acl: '<acl-here>', // defaults to public-read
    path: function(attachment) { // where the file is stored in the bucket - defaults to this function
      return return '/' + path.basename(attachment.path)
    }
  }),
  fields: {
    file: {}
  }
});

var Post = mongoose.model('Post', PostSchema)

.. then later:

var post = new Post()
post.attach('image', {path: '/path/to/image'}, function(error) {
    // file is now uploaded and post.file is populated e.g.:
    // post.file.url
});