【问题标题】:Trying to download a file from aws s3 into nodejs writeStream尝试将文件从 aws s3 下载到 nodejs writeStream
【发布时间】:2018-02-16 06:00:16
【问题描述】:

我正在尝试从 s3 下载文件并使用 nodejs 中的 writeStream 直接放入文件系统上的文件中。这是我的代码:

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

发生了什么?在写入完成之前文件是否关闭?为什么?

【问题讨论】:

    标签: node.js amazon-web-services


    【解决方案1】:
    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();
    

    【讨论】:

    • 嘿,这当然有帮助。但我仍然想知道为什么我的例子失败了。两个版本之间是否存在效率差异(我的版本和这个答案中的版本?)
    • 你如何处理错误No sich file or directory
    【解决方案2】:

    AWS 还提到了一个这样使用 Promise 的示例:

    const s3 = new aws.S3({apiVersion: '2006-03-01'});
    const params = { Bucket: 'yourBucket', Key: 'yourKey' };
    const file = require('fs').createWriteStream('./local_file_path');
    
    const s3Promise = s3.getObject(params).promise();
    
    s3Promise.then((data) => {
      file.write(data.Body, () => {
        file.end();
        fooCallbackFunction();
      });
    }).catch((err) => {
      console.log(err);
    });
    

    这对我来说非常适合。
    https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html

    编辑:(2018 年 2 月 15 日)更新了代码,因为您必须结束写入流 (file.end())。

    【讨论】:

      【解决方案3】:

      我已将上述响应与管道中的典型gunzip 操作相结合:

                  var s3DestFile = "./archive.gz";
                  var s3UnzippedFile    = './deflated.csv';
      
                  var gunzip = zlib.createGunzip();
                  var file = fs.createWriteStream( s3DestFile );
                  s3.getObject({ Bucket: Bucket, Key: Key })
                  .on('error', function (err) {
                      console.log(err);
                  })
                  .on('httpData', function (chunk) {
                      file.write(chunk);
                  })
                  .on('httpDone', function () {
                      file.end();
      
                      console.log("downloaded file to" + s3DestFile);
                      fs.createReadStream( s3DestFile )
                      .on('error', console.error)
                      .on('end', () => {
                          console.log("deflated to "+s3UnzippedFile)
                      })
                      .pipe(gunzip)
                      .pipe(fs.createWriteStream( s3UnzippedFile ))
                  })
                  .send();
      

      【讨论】:

        猜你喜欢
        • 2021-06-26
        • 2019-09-06
        • 2014-04-04
        • 1970-01-01
        • 1970-01-01
        • 2019-02-16
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        相关资源
        最近更新 更多