如何指定GridFS存储桶? [英] How can I specify a GridFS bucket?

查看:589
本文介绍了如何指定GridFS存储桶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的express.js代码,用于将文件上传和下载到GridFS:

This is my express.js code to upload and download files to GridFS:

var fs = require("fs");
var gridStream = require("gridfs-stream");
var mongoose = require("mongoose");

exports.init = function(app, db)
{
    var grid = gridStream(db, mongoose.mongo);

    app.post("/UploadFile", function(request, response)
    {
        var file = request.files.UploadedFile;

        var meta = request.param("Meta");
        var name = request.param("Name");

        var stream = grid.createWriteStream(
        {
            filename: name,
            metadata: meta
        });

        fs.createReadStream(file.path)
        .on("end", function()
        {
            response.send({ Success: true });
        })
        .on("Error", function(error)
        {
            HandleError(error, response);
        })
        .pipe(stream);
    });

    app.get("/DownloadFile", function(request, response)
    {
        var selector = request.param("Selector");

        response.writeHead(200, { "Content-Type" : "image/png"});
        grid.createReadStream({ filename: "FileUploadNamed" }).pipe(response);
    });
}

它可以完美工作,但是我想指定一个用于读取和写入的存储桶,但是我不确定如何执行此操作.我已经看到了在线调用GridFS构造函数的示例,但是如您所见,我在这里没有这样做.该文档还说可以提供一个不同的存储桶名称,但是我看不到任何方式.

It works perfectly, but I'd like to specify a bucket to read and write from, but I'm not sure how to do that. I've seen examples online calling a GridFS constructor, but as you can see I'm not doing that here. The documentation also says that it's possible to supply a different bucket name, but I don't see anything on how.

如何选择将文件保存到哪个存储桶并从中读取文件?

How can I select which bucket that my files are saved to and read from?

推荐答案

在gridfs-stream或它使用的基础本机mongodb驱动程序中没有对此进行很好的记录,但是这里是您的操作方法:

This is not well documented in gridfs-stream or the underlying native mongodb driver it uses, but here is how you do it:

这是gridfs-stream createWriteStream中的options对象示例 (请注意root选项)

Here is the options object from the gridfs-stream createWriteStream example (note the root option):

{
   _id: '50e03d29edfdc00d34000001', 
   filename: 'my_file.txt',         
   mode: 'w', 
   chunkSize: 1024, 
   content_type: 'plain/text', 
   root: 'my_collection',  // Bucket will be 'my_collection' instead of 'fs'
   metadata: {
       ...
   }
}

工作原理:

gridfs-stream通过options对象传递,您将对createWriteStreamcreateReadStream的调用传递给基础的mongodb驱动程序,以创建表示该文件的gridStore对象.反过来,mongodb驱动程序可识别<options对象中的c2>作为默认的"fs"网格存储桶前缀字符串的替代.

gridfs-stream passes through the options object you pass a call to createWriteStream or createReadStream to the underlying mongodb driver to create a gridStore object to represent the file. The mongodb driver in turn recognizes root in the options object as an override of the default "fs" grid bucket prefix string.

这篇关于如何指定GridFS存储桶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆