0.6.7 • Published 3 years ago

mongoose-media-plugin v0.6.7

Weekly downloads
29
License
MIT
Repository
github
Last release
3 years ago

mongoose-media-plugin

Build Status codecov

Description

Mongoose plugin for managing S3 files as if they were stored in mongo.

Adds read and write stream capabilities to a mongo collection. The stream is offloaded to S3 during save.

Prerequisites

You will need a aws account with full access to S3:

///arn:aws:iam::aws:policy/AmazonS3FullAccess
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": "*"
        }
    ]
}

It is highly recommended you pass your AWS credentials via envoiroment vars

$ export AWS_ACCESS_KEY_ID=<your access key>
$ export AWS_SECRET_ACCESS_KEY=<your secret>

Dependancies

node v10+
mongoose@5.*

Installation

$ npm install mongoose-media-plugin

Example usage

import mongoose from 'mongoose';
import MMD, { Configuration } from 'mongoose-media-plugin';
import fs from 'fs';

const
    Schema = mongoose.Schema( {
        name: { type: String },
        //...restofschema
    } ),
    Setup = new Configuration({
        bucket: "<Bucket name>",
        createBucket: true // Set this to create the bucket if it  doesnt exist.
    } );

( async () => {

    await mongoose.connect( process.env.MONGOCONN || "mongodb://localhost:27017/mnp-example", { useNewUrlParser: true, useUnifiedTopology: true } );

    let
        config = await Setup.Configure(),
        local = fs.createReadStream( "<path to local file>" ),
        Media, Insert;

    Schema.plugin( MMP, config );
    Media = new mongoose.model( "media", Schema );

    Insert = new Media( {
        name: "first document",
        body: local
    } );
    
    Insert.save( ( err, saved ) => {
        console.log( err, saved );
        /**
        * Returns....
        * {
        *     _id: <ObjectId>
        *     name: "first document",
        *     ContentType: "video/mp4", //etc
        *     ETag: <S3 Etag>, //useful for setting cache see express example.
        *     Size: <size in bytes for file>,
        *     body: <ReadableStream> // from S3
        *     created: Date,
        *     updated: Date,
        *     uploaded: Date
        * }
        */
        
        // download the file...
        let download = fs.createWriteStream( "<path to new file>" );
        saved.body.pipe( download );
    } );
} )()

Running the express example

Ensure you have mongo running locally and install dependancies:

$ npm install
$ npm run build && npm run example

Running the tests

Ensure you have mongo running locally and install dependancies:

$ npm install
$ npm test

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details

0.6.7

3 years ago

0.6.6

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.4

4 years ago

0.6.1

4 years ago

0.5.0

4 years ago

0.6.0

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.3.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago