Read file from aws s3 bucket using node fs

后端 未结 11 787
逝去的感伤
逝去的感伤 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 couldn't figure why yet, but the createReadStream/pipe approach didn't work for me. I was trying to download a large CSV file (300MB+) and I got duplicated lines. It seemed a random issue. The final file size varied in each attempt to download it.

    I ended up using another way, based on AWS JS SDK examples:

    var s3 = new AWS.S3();
    var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};
    var file = require('fs').createWriteStream('/path/to/file.jpg');
    
    s3.getObject(params).
        on('httpData', function(chunk) { file.write(chunk); }).
        on('httpDone', function() { file.end(); }).
        send();
    

    This way, it worked like a charm.

提交回复
热议问题