Read file from aws s3 bucket using node fs

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

    If you are looking to avoid the callbacks you can take advantage of the sdk .promise() function like this:

    const s3 = new AWS.S3();
    const params = {Bucket: 'myBucket', Key: 'myKey.csv'}
    const response = await s3.getObject(params).promise() // await the promise
    const fileContent = getObjectResult.Body.toString('utf-8'); // can also do 'base64' here if desired
    

    I'm sure the other ways mentioned here have their advantages but this works great for me. Sourced from this thread (see the last response from AWS): https://forums.aws.amazon.com/thread.jspa?threadID=116788

提交回复
热议问题