Ranisterio
Ranisterio

Reputation: 107

Read content of txt file from s3 bucket with Node

I would like to read the content of a .txt file stored within an s3 bucket.

I tried :

var s3 = new AWS.S3({apiVersion: '2006-03-01'});
var params = {Bucket: 'My-Bucket', Key: 'MyFile.txt'};
var s3file = s3.getObject(params)

But the s3file object that i get does not contain the content of the file.

Do you have an idea on what to do ?

Upvotes: 1

Views: 8238

Answers (2)

ExpertCoder
ExpertCoder

Reputation: 23

Agree with zishone and here is the code with exception handling:

var s3 = new AWS.S3({apiVersion: '2006-03-01'});
var params = {Bucket: 'My-Bucket', Key: 'MyFile.txt'};

s3.getObject(params , function (err, data) {

    if (err) {
        console.log(err);
    } else {
        console.log(data.Body.toString());
    }

})

Upvotes: 1

zishone
zishone

Reputation: 1244

According to the docs the contents of your file will be in the Body field of the result and it will be a Buffer.

And another problem there is that s3.getObject( should have a callback.

s3.getObject(params, (err, s3file) => {
    const text = s3file.Body.toString();
})

Upvotes: 0

Related Questions