How to Get Response From S3 GetObject() Method in Node JS – Definitive Guide

AWS S3 is a simple storage service that allows you to store file objects in Buckets. AWS also provides APIs to get the objects programmatically.

This tutorial teaches you the different methods to get the response from the S3 GetObject() method in Node JS while getting the S3 object from your Node application.

To use the Get, you must have read access to the object. Else, the object must be made available to the public.

Get Response from S3 GetObject Using data.Body (AWS JavaScript SDK Version V2)

The getObject() method allows you to retrieve the S3 object from AWS S3.

To get the response from the getObject() method,

  • Access the body of the response object using data.body
  • Convert the response to a String using the toString() method
  • Pass the utf-8 as encoding to the toString() method. The encoding is necessary to decode the buffer object with a specific encoding to a string

Code

const aws = require('aws-sdk');

const s3 = new aws.S3({ apiVersion: '2006-03-01' });

var getParams = {

    Bucket: 'bucket_name', 

    Key: 'filename.txt' 
}

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

    if (err) {
        return err;
    }

    let objectData = data.Body.toString('utf-8');

    console.log(objectData);
});

Output

contents of the text file

Get Response From S3 GetObject() Using Data Body with Async and Await

This section teaches you how to get the object from AWS S3 in Node JS using Async and Await.

Use this method when you want to read the contents of a large file from the AWS S3 bucket.

  • Create an async function to getObject()
  • Invoke the s3.getObject().promise() method with await
  • Access the response body using the data.Body
  • Convert the response to a string using the toString() method and pass the appropriate encoding. For example, utf-8

Code

const AWS = require('aws-sdk');

const s3 = new AWS.S3();

async function getObject () {

  try {

    var params = {

        Bucket: "bukcetName",

        Key: "filename.txt"
    }

    const data = await s3.getObject(params).promise();

    console.log(data.Body.toString('utf-8'));

  } catch (e) {

    throw new Error(`Could not retrieve file from S3: ${e.message}`);

  }
}

getObject();

Output

contents of the text file

Get Response From S3 GetObjectCommand (AWS JavaScript SDK Version V3)

The section teaches you how to get a response from S3 GetObjectCommand.

You need to use the GetObjectCommand() method to retrieve the objects from AWS S3 in the JS SDK Version V3.

  • Create a new S3 client object
  • Create a new GetObjectCommand with the bucket name and the object name
  • Send the GetObjectCommand to the S3 client
  • It will return a response
  • Access the response body using response.body and convert the response to a String using the transformToString() method
  • The body object also supports transformToByteArray() and TransformToWebStream() method. You can use either of these methods to convert the response to a Byte array. For example, to get the content of the binary files and convert it into a binary file object in your client-side program.

Code

const { GetObjectCommand, S3Client } = require('@aws-sdk/client-s3');

const client = new S3Client();

const getObject = async () => {

  const command = new GetObjectCommand({

    Bucket: "bucketName",

    Key: "filename.txt"

  });

  try {

    const response = await client.send(command);
   
    const str = await response.Body.transformToString();

    console.log(str);

  } catch (err) {

    console.error(err);

  }
};

getObject();

Output

contents of the text file

This is how you can get a response from the getObject() method while downloading a file from S3.

If you have any questions, feel free to comment below.

Leave a Comment