How to pass a Buffer as argument of fs.createReadStream

后端 未结 3 2156
南旧
南旧 2021-02-18 15:53

According to docs https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options

fs.createReadStream() can accept Buffer as first argument

my node code:

相关标签:
3条回答
  • 2021-02-18 16:23

    Creating a Readable Stream From Buffer.

    You can easily create a Readable Stream from a Buffer, however using fs.createReadStream() does indeed require first writing it to a file path.

    Using stream.Duplex()

    • No need to write a local file in order to get a readable stream
    • Saves I/O and speeds things up.
    • Works everywhere fs.createReadStream() is desired.
    • Great for writing to cloud services where local storage my not be feasible.

    Example:

    const {Duplex} = require('stream'); // Native Node Module 
    
    function bufferToStream(myBuuffer) {
        let tmp = new Duplex();
        tmp.push(myBuuffer);
        tmp.push(null);
        return tmp;
    }
    
    const myReadableStream = bufferToStream(your_buffer);
    
    // use myReadableStream anywhere you would use a stream 
    // created using fs.createReadStream('some_path.ext');
    // For really large streams, you may want to pipe the buffer into the Duplex.
    
    0 讨论(0)
  • 2021-02-18 16:26

    The first argument to fs.createReadStream() must be the file path. You can apparently pass the path in a Buffer object, but it still must be an acceptable OS path when the Buffer is converted to a string.

    It appears you are trying to pass the file content to fs.createReadStream(). That is not how that API works. If you look into the code for fs.createReadStream() it is completely clear in the code that it is going to call fs.open() and pass the first argument from fs.createReadStream() as the file path for fs.open().

    If what you're trying to do is to create a readable stream from a buffer (no file involved), then you need to do that a different way. You can see how to do that here in this answer: Converting a Buffer into a ReadableStream in Node.js.

    Conceptually, you just create a readable stream object, push the data you already have into it from your Buffer, push a null to signify the end of the stream and create a noop _read() method and you're done. You can then use that readable stream with any other code that expects to read it.

    0 讨论(0)
  • 2021-02-18 16:46

    @jfriend00 has already provided a very clear explanation on this issue. If a Buffer object is passed as argument to fs.createReadStream(), it should indicate the file path, not file content. As @Littlee asked in comment, here is an example code:

    var express = require('express');
    var router = express.Router();
    var fs = require('fs')
    
    router.get('/test', function(req, res) {
      var buf = Buffer.from('./test.html');
      fs.createReadStream(buf).pipe(res);
    });
    

    Please note the Buffer buf indicates a file path ("./test.html"), not the file test.html's content.

    0 讨论(0)
提交回复
热议问题