gpt4 book ai didi

node.js - 如何使用 Node.js 创建带缓冲区的读取流

转载 作者:搜寻专家 更新时间:2023-10-31 22:17:33 25 4
gpt4 key购买 nike

我有这个 json:

var myJSON = '{"kind": "person", "fullName": "Rivka3"}';

我正在尝试使用 createReadStream 将它上传到 bigquery。当我在本地保存它时,我成功了:

fs.writeFile("/tmp/bq_json_file_new.json", myJSON, function(err){});
fs.createReadStream("/tmp/bq_json_file_new.json")
.pipe(table.createWriteStream(metadata))
.on('complete', function(job) {
job
.on('error', console.log)
.on('complete', function(metadata) {
console.log('job completed', metadata);
});
});

现在我尝试在不将其保存在本地的情况下执行此操作 - 使用缓冲区:

fs.createReadStream(new Buffer(myJSON, "utf8"))
.pipe(table.createWriteStream(metadata))
.on('complete', function(job) {
job
.on('error', console.log)
.on('complete', function(metadata) {
console.log('job completed', metadata);
});
});

但是我收到这个错误:

fs.js:575
binding.open(pathModule._makeLong(path),

TypeError: path must be a string

最佳答案

使用stream解决了问题:

var stream = require('stream');

var bufferStream = new stream.PassThrough();

bufferStream.end(new Buffer(myJSON));

bufferStream.pipe(table.createWriteStream(metadata))
.on('complete', function(job) {
job
.on('error', console.log)
.on('complete', function(metadata) {
console.log('job completed', metadata);
});
});

关于node.js - 如何使用 Node.js 创建带缓冲区的读取流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37833835/

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