这里的想法是使用xmlbuilder创建一个 XML 文件,并使用xmlbuilder 的streamWriter
功能将输出直接通过管道传输到 S3。
我这里的方法是将可写流的输入通过管道传输到可读流中,并将可读流上传到 S3。如本答案中所述,我为此使用了 PassThrough 流。
但是当我尝试上传 xmlbuilder 使用的流时,S3upload
函数什么也不做(即使错误对象是空的)。
尽管如此,当我将流传输到outStream
.
所以这里是代码:
const fs = require('fs');
const stream = require('stream');
const AWS = require('aws-sdk');
const xmlbuilder = require('xmlbuilder');
const s3 = new AWS.S3({
secretAccessKey: 'SECRETACCESSKEY',
accessKeyId: 'ACCESSKEYID',
region: 'eu-central-1',
apiVersion: '2006-03-01',
});
const BUCKET = 'mybucket';
/**
* Code for writeable stream from:
* https://medium.freecodecamp.org/node-js-streams-everything-you-need-to-know-c9141306be93
*/
const outStream = new stream.Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
},
});
/**
* Try to upload stream from XMLBuilder
*/
const pass1 = new stream.PassThrough();
const pass2 = new stream.PassThrough();
const writer = xmlbuilder.streamWriter(pass1);
const xml = xmlbuilder.create('root', { version: '1.0', encoding: 'UTF-8' }).end(writer);
// Fails
const params1 = { Bucket: BUCKET, Key: 'sitemap/test1.txt', Body: pass2 };
s3.upload(params1, function(err, data) {
console.log(err, data);
});
// Convert writeable stream in readable
pass1.pipe(pass2);
// But why is this working?
pass2.pipe(outStream);