我试图从s3下载文件,并使用nodejs中的writeStream直接放入文件系统中的文件.这是我的代码: downloadFile = function(bucketName, fileName, localFileName) { //Donwload the file var bucket = new AWS.S3({ params: { Buc
downloadFile = function(bucketName, fileName, localFileName) { //Donwload the file var bucket = new AWS.S3({ params: { Bucket: bucketName }, signatureVersion: 'v4' }); var file = require('fs').createWriteStream(localFileName); var request = bucket.getObject({ Key: fileName }); request.createReadStream().pipe(file); request.send(); return request.promise(); }
运行此函数我收到此错误:
Uncaught Error: write after end
怎么了?写入完成之前文件是否已关闭?为什么?
var s3 = new AWS.S3({ accessKeyId: accessKeyId, secretAccessKey: secretAccessKey }), file = fs.createWriteStream(localFileName); s3 .getObject({ Bucket: bucketName, Key: fileName }) .on('error', function (err) { console.log(err); }) .on('httpData', function (chunk) { file.write(chunk); }) .on('httpDone', function () { file.end(); }) .send();