admin管理员组文章数量:1356294
Can someone explaint Difference between createReadStream
and readable
in node.js? 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)
Can someone explaint Difference between createReadStream
and readable
in node.js? 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)
Share
Improve this question
edited May 3, 2021 at 22:54
Vakho Jgenti
asked May 3, 2021 at 22:03
Vakho JgentiVakho Jgenti
4056 silver badges9 bronze badges
1
-
fs.createReadStream()
creates a Readable that is getting its data from a file. Readable is a generic thing. It can be hooked to lots of different types of sources of the data. So,fs.createReadStream()
creates one hooked to a file. – jfriend00 Commented May 4, 2021 at 0:14
2 Answers
Reset to default 8In a NodeJS, you can create a readable stream in a few ways:
SOLUTION 1
You can do it with fs
module. 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.
const fs = require('fs');
const readable_stream = fs.createReadStream('file_path');
SOLUTION 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). You can do this with stream
module. You can import Readable
from stream
module and you can create a readable stream. When creating an object, you can also implement read()
method which is used to read the data out of the internal buffer. If no data available to be read, null
is returned. The optional size
argument specifies a specific number of bytes to read. If the size
argument is not specified, all of the data contained in the internal buffer will be returned.
const Readable = require('stream').Readable;
const readable_stream = new Readable({
read(size) {
// ...
}
});
SOLUTION 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).
const axios = require('axios');
const readable_stream = await axios({
method: 'get',
url: "pdf_resource_url",
responseType: 'stream'
}).data;
SOLUTION 4
Third party packages can support creating of streams as a feature. That is a way with aws-sdk
package from your example.
SUMMARIZE AND CONCLUSION
You can create a readable
stream in a few ways. 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.
Another solution that worth to mention is the Readable.from
method which allows you to create a reable stream by providing an iteratable
to it (a String
for example). See the following example:
const { Readable } = require("stream");
const readableStream = Readable.from("In-memory data to a readable stream");
版权声明:本文标题:javascript - What is the difference between "createReadStream" and "Readable" class? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743970759a2570681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论