因此,我试图从s3检索mp3音频文件(大约9 MB),将数据通过管道传输到写流,然后使用来自/ tmp /文件的readStream上载到另一个目标s3存储桶。这是一个lambda函数,它接收s3上传事件,并尝试将数据从创建的对象写入另一个存储桶。
const fs = require('fs');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.handler = (event, context, callback) => {
var sourceBucket = event.Records[0].s3.bucket.name;
var sourceKey = event.Records[0].s3.object.key;
var getParams = {
Bucket: sourceBucket,
Key: sourceKey
};
const inputFilename = '/tmp/' + sourceKey;
//writing and reading streams
const writeStream = fs.createWriteStream(inputFilename);
s3.getObject(getParams).createReadStream().pipe(writeStream);
var putParams = {
Body: fs.createReadStream(inputFilename),
Bucket: "example-destination-bucket",
Key: 'transfer-' + sourceKey
};
s3.upload(putParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log('logging data' + data); // successful response
});
};
这将密钥成功放入s3存储桶,但上传的文件大小为0字节。知道为什么这会导致上传为空吗?
答案 0 :(得分:0)
该文件需要下载,这需要一些时间,因此您需要使用该文件。on('finish')这样的调用。
const writeStream = fs.createWriteStream(inputFilename);
s3.getObject(getParams).createReadStream().pipe(writeStream);
writeStream.on('finish', function() {
//upload to S3 code
}
答案 1 :(得分:0)