简体   繁体   English

“createReadStream”和“Readable”class 有什么区别?

[英]What is the difference between "createReadStream" and "Readable" class?

Can someone explaint Difference between createReadStream and readable in node.js?有人可以解释 node.js 中createReadStreamreadable之间的区别吗? By my observes they are similar, so what is under the hood difference, and when should each be used?根据我的观察,它们是相似的,那么引擎盖下的区别是什么,什么时候应该使用它们?

for example例如

const s3 = new AWS.S3({
      accessKeyId: AWSConfig.AWSConfig.ACCESS_KEY_ID,
      secretAccessKey: AWSConfig.AWSConfig.SECRET_ACCESS_KEY,
    });

    const params = {
      Bucket: AWSConfig.AWSConfig.bucket,
      Key: "somebucketName/1620072325205",
    };
    const file = await s3.getObject(params).promise();
    const fileSize = file.ContentLength / (1024 * 1024);

    const read = new Readable({
      read(fileSize) {
        this.push(file.Body);
        this.push(null);
      },
    });

    read.pipe(res);

this is similar to这类似于

const s3 = new AWS.S3({
      accessKeyId: AWSConfig.AWSConfig.ACCESS_KEY_ID,
      secretAccessKey: AWSConfig.AWSConfig.SECRET_ACCESS_KEY,
    });

    const params = {
      Bucket: AWSConfig.AWSConfig.bucket,
      Key: "somebucketName/1620072325205",
    };
    const file = await s3.getObject(params).createReadStream();
    file.pipe(res)
    

In a NodeJS, you can create a readable stream in a few ways:在 NodeJS 中,您可以通过以下几种方式创建可读的 stream:

SOLUTION 1解决方案 1

You can do it with fs module.您可以使用fs模块来完成。 The function fs.createReadStream() allows you to open up a readable stream and all you have to do is pass the path of the file to start streaming in. function fs.createReadStream()允许您打开可读的 stream 并且您所要做的就是传递文件的路径以开始流式传输。

const fs = require('fs');

const readable_stream = fs.createReadStream('file_path');

SOLUTION 2解决方案 2

If you don't want to create file, you can create an in-memory stream and do something with it (for example, upload it somewhere).如果你不想创建文件,你可以在内存中创建一个 stream 并用它做一些事情(例如,上传到某个地方)。 You can do this with stream module.您可以使用stream模块执行此操作。 You can import Readable from stream module and you can create a readable stream.您可以从stream模块导入Readable ,您可以创建可读的 stream。 When creating an object, you can also implement read() method which is used to read the data out of the internal buffer.在创建 object 时,您还可以实现read()方法,用于从内部缓冲区中读取数据。 If no data available to be read, null is returned.如果没有数据可供读取,则返回null The optional size argument specifies a specific number of bytes to read.可选size参数指定要读取的特定字节数。 If the size argument is not specified, all of the data contained in the internal buffer will be returned.如果未指定size参数,则将返回内部缓冲区中包含的所有数据。

const Readable = require('stream').Readable;

const readable_stream = new Readable({
  ​read(size) {
   ​// ...
​  }
});

SOLUTION 3解决方案 3

When you are fetching something over the network, that can be fetched like stream (for example you are fetching a PDF document from some API).当您通过网络获取某些内容时,可以像 stream 一样获取(例如,您正在从某个 API 获取 PDF 文档)。

const axios = require('axios');

const readable_stream = await axios({
  method: 'get',
  url: "pdf_resource_url",
  responseType: 'stream'
}).data;

SOLUTION 4解决方案 4

Third party packages can support creating of streams as a feature.第三方包可以支持创建流作为一项功能。 That is a way with aws-sdk package from your example.这是您示例中aws-sdk package 的一种方式。

SUMMARIZE AND CONCLUSION总结和结论

You can create a readable stream in a few ways.您可以通过几种方式创建readable的 stream。 Since you are already using aws-sdk package, I would say that you should go with using their createReadStream() , instead of importing stream module and creating readable stream with it. Since you are already using aws-sdk package, I would say that you should go with using their createReadStream() , instead of importing stream module and creating readable stream with it.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM