Read file from aws s3 bucket using node fs

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

    With the new version of sdk, the accepted answer does not work - it does not wait for the object to be downloaded. The following code snippet will help with the new version:

    // dependencies
    
    const AWS = require('aws-sdk');
    
    // get reference to S3 client
    
    const s3 = new AWS.S3();
    
    exports.handler = async (event, context, callback) => {
    
    var bucket = "TestBucket"
    
    var key = "TestKey"
    
       try {
    
          const params = {
                Bucket: Bucket,
                Key: Key
            };
    
           var theObject = await s3.getObject(params).promise();
    
        } catch (error) {
            console.log(error);
            return;
        }  
    }
    

提交回复
热议问题