How to get response from S3 getObject in Node.js?

Sometimes, we want to get response from S3 getObject in Node.js.

In this article, we’ll look at how to get response from S3 getObject in Node.js.

How to get response from S3 getObject in Node.js?

To get response from S3 getObject in Node.js, we can get the response data from the callback.

For instance, we write

const aws = require('aws-sdk');
const s3 = new aws.S3();

const getParams = {
  Bucket: 'abc',
  Key: 'abc.txt'
}

s3.getObject(getParams, (err, data) => {
  if (err) {
    return;
  }
  const objectData = data.Body.toString('utf-8');
  //...
});

to call s3.getObject with the parameters for the file we want to get.

Then we pass in a callback that has the retrieved file.

The file data is in the data parameter.

And we can read it as a string with data.Body.toString with the encoding of the file.

Conclusion

To get response from S3 getObject in Node.js, we can get the response data from the callback.