如何使用nodejs使用缓冲区创build一个readstream

我有这个json:

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

我试图将其上传到bigquery,使用createReadStream。 当我保存它当地我成功:

 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); }); });