Read file from aws s3 bucket using node fs

后端 未结 11 713
逝去的感伤
逝去的感伤 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:24

    I prefer Buffer.from(data.Body).toString('utf8'). It supports encoding parameters. With other AWS services (ex. Kinesis Streams) someone may want to replace 'utf8' encoding with 'base64'.

    new AWS.S3().getObject(
      { Bucket: this.awsBucketName, Key: keyName }, 
      function(err, data) {
        if (!err) {
          const body = Buffer.from(data.Body).toString('utf8');
          console.log(body);
        }
      }
    );
    

提交回复
热议问题