node.js - How to store a file with Mongoose-Crate via an Express Route -
i building restful api using node + express 4 + mongodb + mongoose.
one thing api needs store , retrieve files. store in amazon s3. mongoose has specific plugin attaching files mongo documents called mongoose-crate, in turn has storage provider mongoose-crate-s3 uploads files s3.
i've done best adapt example code mongoose-crate-s3 npm page work express route, far i've not gotten image upload s3 storage. documents of 'file' model being created in mongo database, have '_id' , '__v' fields. no 'title', no 'description', nothing indicate .post endpoint receiving files try post. keep making slight adjustments code getting variation on "could not response".
here mongoose schema file.js (with s4 credentials removed of course)
var mongoose = require('mongoose'); var schema = mongoose.schema; var crate = require('mongoose-crate'); var s3 = require('mongoose-crate-s3'); var fileschema = new schema({ title: string, description: string }); fileschema.plugin(crate, { storage: new s3({ key: '<api-key-here>', secret: '<secret-here>', bucket: '<bucket-here>', acl: '<acl-here>', // defaults public-read region: '<region-here>', // defaults us-standard path: function(attachment) { // file stored in bucket - defaults function return '/' + path.basename(attachment.path) } }), fields: { file: {} } }); module.exports = mongoose.model('file', fileschema); and here relevant snippet of api routes file. i'm code need fix goes in here.
apirouter.route('/files') .post(function(req, res){ var file = new file() //.attach = function(field, attachment, callback) file.attach('image', req.body, function(error) { // file uploaded , post.file populated e.g.: // post.file.url }) }) .get(function(req, res){ //get list of files goes here }); i expect missing obvious, mean stack programming new me, , i've scoured stackoverflow & web @ large looking more examples or hint @ missing. please help!
first of should set location path of want file saved on s3, in example it's using same path origin file (which /var/tmp/y7sday... or c:/users/someguy/pictures/..) need sort out first.
in code i'm using same name file gave, in production might want sort these date , add random uuid them. full example
fileschema.plugin(crate, { storage: new s3({ key: process.env.key, secret: process.env.secret, bucket: process.env.bucket, acl: 'public-read', // defaults public-read region: 'eu-west-1', // defaults us-standard path: function(attachment) { // file stored in bucket - defaults function return '/' + attachment.name } }), fields: { file: {} } }); next in api endpoint want include form information in body , add object, these in req.body part.
the important thing note here i've set attachment file, needs match field have declared in mongoose schema or die.
next include req.files.file second argument. full example
exports.create = function (req, res) { var file = new file(); file.title = req.body.title; file.description = req.body.description; file.attach('file', req.files.file, function (error) { if (error) { return res.send(error); } else { return res.send(file); } }); }; i've uploaded workings github, please clone , try out.
all need post /file/
{ "description": "my description", "title": "my title", "file": { "url": "https://mysecretbucket.s3-eu-west-1.amazonaws.com/bb4bf9f69bf41ab478824d80a338beb6.png", "type": "image/png", "name": "bb4bf9f69bf41ab478824d80a338beb6.png", "size": 8912 }, "_id": "553e8ed8282cc30000000001" } make sure set field in form file image below

Comments
Post a Comment