【问题标题】:How to pipe large file into mongodb using gridfsbucket, when receiving file in chunks?以块的形式接收文件时,如何使用gridfsbucket将大文件通过管道传输到mongodb?
【发布时间】:2018-01-26 02:41:41
【问题描述】:

我正在使用 nodejs、mongodb 和 gridfsbucket。

我正在将一个 255 字节块的文件接收到我的服务器中,这些文件可能非常大,因此创建一个变量来存储这些块,然后使用 gridfsbucket 将其通过管道传输到 mongo 中是不可行的选择。

目前我有一种将文件临时存储在磁盘上然后将其通过管道传输到 mongo 的工作方法。这实际上效果很好,唯一的问题是我不想在使用 gridfsbucket 流式传输到 mongo 之前临时存储数据。

有谁知道如何在这些块进入我的服务器时获取它们并立即使用 gridfsbucket 将它们流式传输到 mongo 中?我在想我需要打开管道,然后不断地将块流到管道中,但我不确定如何完成。

这是我当前用于存储到磁盘的代码:

fs.appendFileSync(this.tempFileName, Buffer.from(this.currfile.currdatachunk));
this.currfile.filename = this.currfile.filename.replace(")", Date.now() + ")");
var fileName = decodeURI(this.currfile.filename.replace("$(", "").replace(")", ""));
 fileName = encodeURI(fileName);
 var self = this;
 var gb = new GridFSBucket(mongoCacheDb, { bucketName: this.cacheCollection });
 var uploadStream = gb.openUploadStream(fileName);

 uploadStream.options.metadata = {
     'url': "/getFile/" + fileName,
     'id': uploadStream.id
 }

 uploadStream.once("finish", function uploadStream_onceFinish() {
                        if (this.length > 0) {
                            var ms = new Message();
                            ms.data = self.cacheCollection + "/" + self.currfile.filename;
                            ms.datatype = "URL";
                            ms.hasdata = "yes";
                            ms.haserrors = "no";
                            ms.type = "APPXLOADURL";
                            sendMessage(ws, ms);

                            /*Send response to server indicating file receipt*/
                            var nameAB = Buffer.from(self.currfile.filename);
                            self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                            self.clientsocket.write(Buffer.from(nameAB));
                            self.clientsocket.write(Buffer.from([3, 1]));
                            console.log("Finished: " + Date.now());
                        } else {
                            var nameAB = Buffer.from(self.currfile.filename);
                            self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                            self.clientsocket.write(Buffer.from(nameAB));
                            self.clientsocket.write(Buffer.from([0, 0]));
                        }
                        fs.unlinkSync(self.tempFileName);
                    });
                    fs.createReadStream(this.tempFileName).pipe(uploadStream);

【问题讨论】:

    标签: node.js mongodb gridfs


    【解决方案1】:

    我想我应该再等一天。最后把我的头绕过去,不再想太多。我在第一次运行时创建了管道,然后在收到缓冲块时将它们推送到管道中。代码如下:

    if (this.chunksReceived == 0) {
        this.rStream = new Readable({ read(size) { } });
    
        this.currfile.filename = this.currfile.filename.replace(")", Date.now() + ")");
        var fileName = decodeURI(this.currfile.filename.replace("$(", "").replace(")", ""));
        fileName = encodeURI(fileName);
        var self = this;
        var gb = new GridFSBucket(mongoCacheDb, { bucketName: this.cacheCollection });
        this.uploadStream = gb.openUploadStream(fileName);
    
        this.uploadStream.options.metadata = {
            'url': "/getFile/" + fileName,
            'id': this.uploadStream.id
        }
    
        this.uploadStream.once("finish", function uploadStream_onceFinish() {
            if (this.length > 0) {
                var ms = new Message();
                ms.data = self.cacheCollection + "/" + self.currfile.filename;
                ms.datatype = "URL";
                ms.hasdata = "yes";
                ms.haserrors = "no";
                ms.type = "APPXLOADURL";
                sendMessage(ws, ms);
    
                /*Send response to server indicating file receipt*/
                var nameAB = Buffer.from(self.currfile.filename);
                self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                self.clientsocket.write(Buffer.from(nameAB));
                self.clientsocket.write(Buffer.from([3, 1]));
                console.log("Finished: " + Date.now());
            } else {
                var nameAB = Buffer.from(self.currfile.filename);
                self.clientsocket.write(Buffer.from(hton32(nameAB.length)));
                self.clientsocket.write(Buffer.from(nameAB));
                self.clientsocket.write(Buffer.from([0, 0]));
            }
        });
    
        this.rStream.pipe(this.uploadStream);
    }
    this.rStream.push(Buffer.from(this.currfile.currdatachunk));
    this.chunksReceived++;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 2016-06-11
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      相关资源
      最近更新 更多