gpt4 book ai didi

node.js - 如何克隆可读流

转载 作者:太空宇宙 更新时间:2023-11-04 02:44:48 26 4
gpt4 key购买 nike

我有一个流,我正在尝试将相同的流提交到两个不同的目的地。第一个目的地是 AWS S3,第二个目的地是通过 http 请求到达其他后端。

const document = fs.createReadStream(process.cwd() + "/test/resources/" + "id/document.jpg");

const s3Response = await submitToS3(document);

const backendResponse = await submitToBackend(document);

据我了解,流只能读取一次。我如何将相同的流发送到两个不同的目的地。

我考虑过克隆流,但简单地创建一个新变量并将流分配给该变量是行不通的。

最佳答案

你可以检查这个npm模块:https://www.npmjs.com/package/readable-stream-clone

npm install readable-stream-clone
const fs = require("fs");
const ReadableStreamClone = require("readable-stream-clone");

const readStream = fs.createReadStream('text.txt');

const readStream1 = new ReadableStreamClone(readStream);
const readStream2 = new ReadableStreamClone(readStream);

const writeStream1 = fs.createWriteStream('sample1.txt');
const writeStream2 = fs.createWriteStream('sample2.txt');

readStream1.pipe(writeStream1)
readStream2.pipe(writeStream2)

关于node.js - 如何克隆可读流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60382914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com