【问题标题】:How do I store large files using Meteor?如何使用 Meteor 存储大文件?
【发布时间】:2014-07-30 21:51:49
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 我会说,您需要像在普通节点中一样通过将连接处理程序引入WebApp.connectHandlers来实现文件存储
  • 你尝试过使用 cfs-filesystem 的 collectionFS 吗?
  • @theo3335796 谢谢 - 我有,但我不想使用顶部有这么大警告的东西。我知道 Meteor 也是实验性的,但看起来更稳定。注意:这个包目前正在积极开发中(2014-3-31)。它有错误,API 可能会继续更改。请帮助测试它并修复错误,但不要在生产中使用。 imslavko - 你可能是对的,现在正在调查。
  • @user1431782 实际上我在 2 个应用程序上使用了 GridFs,它工作得很好

标签: mongodb meteor gridfs


【解决方案1】:

您可以在流星中使用 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) 和其他内容,例如内容类型等

【讨论】:

    猜你喜欢
    • 2016-07-15
    • 2016-09-08
    • 1970-01-01
    • 2017-10-21
    • 2018-05-31
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2016-03-26
    相关资源
    最近更新 更多