如何使用Meteor存储大文件?

时间:2014-07-30 21:51:50

标签: mongodb meteor gridfs

我正在构建一个需要存储和显示PDF文件的Meteor(meteorjs)应用程序,有时甚至高达500Mb。 GridFS似乎还没有集成,所以我想知道在这种情况下是否值得使用Meteor或坚持使用Rails。

理想情况下,我不会使用S3 - 我希望将文件保留在我的服务器上。

更新:似乎可以直接在Meteor外部连接,我不需要自动移动PDF - 而且它可能没有意义。

更具体地说,我现在正在关注: MongoDB - > ElasticSearch使用https://github.com/richardwilly98/elasticsearch-river-mongodb

使用https://github.com/richardwilly98/elasticsearch-river-mongodb/wiki

上的说明

1 个答案:

答案 0 :(得分:2)

您可以在流星内使用GridFS而无需触及任何额外的包

var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db; //grab the database object
var GridStore = MongoInternals.NpmModule.GridStore;


WebApp.connectHandlers.use('/someurl', function(req, res) {
  var bigFile = new GridStore(db, 'bigfile.iso', 'r') //to read
  bigFile.open(function(error, result) {

    if (error) return

    bigFile.stream(); //stream the file
    bigFile.on('error', function(e) {...}) //handle error etc
    bigFile.on('end', function() {bigFile.close();}); //close the file when done

    bigFile.pipe(res); //pipe the file to res
  });
});

然而,Meteor使用的当前GridStore / mongo(v1.3.x)有点过时,最新版本是来自http://mongodb.github.io/node-mongodb-native/2.0/api-docs/的2.x v1.x似乎管道不好,因此您可能需要使用较新的版本

第二个选项

var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db; //grab the database object
var GridStore = Npm.require('mongodb').GridStore; //add Npm.depends({mongodb:'2.0.13'}) in your package.js

WebApp.connectHandlers.use('/someurl', function(req, res) {
    var bigFile = new GridStore(db, 'bigfile.iso', 'r').stream(true); //the new API doens't require bigFile.open() and will close automatically on end
    bigFile.on('error', function(e) {...}); //handle error etc
    bigFile.on('end', function() {...});
    bigFile.pipe(res); //pipe the file to res
});

在这个例子中,我使用WebApp.connectHandlers,但当然你可以使用iron:router或者其他东西。我尝试使用500 MB的文件,它管道很好。您还需要设置res.writeHead(200)和其他内容,如内容类型等

相关问题