Read file from aws s3 bucket using node fs

后端 未结 11 788
逝去的感伤
逝去的感伤 2020-12-07 21:37

I am attempting to read a file that is in a aws s3 bucket using

fs.readFile(file, function (err, contents) {
  var myLines = contents.Body.toString().split(         


        
11条回答
  •  庸人自扰
    2020-12-07 22:26

    You have a couple options. You can include a callback as a second argument, which will be invoked with any error message and the object. This example is straight from the AWS documentation:

    s3.getObject(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });
    

    Alternatively, you can convert the output to a stream. There's also an example in the AWS documentation:

    var s3 = new AWS.S3({apiVersion: '2006-03-01'});
    var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};
    var file = require('fs').createWriteStream('/path/to/file.jpg');
    s3.getObject(params).createReadStream().pipe(file);
    

提交回复
热议问题