19

I have a .json file stored in a S3 Bucket, now i Want to download the json File with nodejs. I wrote following Code:

const bucket = "s3bucketname";

const AWS = require('aws-sdk');
const S3= new AWS.S3();
exports.handler = async (event, context, callback) => {
    var transcript = await download();
    console.log(transcript);
}

async function download(){
  try {
    // Converted it to async/await syntax just to simplify.
    const data = await S3.getObject(
    {   Bucket: bucket, 
        Key: "Test.json",
        //ResponseContentType: 'application/json'
    }).promise();

    console.log(data);
    return {
      statusCode: 200,
      body: JSON.stringify(data)
    }
  }
  catch (err) {
    return {
      statusCode: err.statusCode || 400,
      body: err.message || JSON.stringify(err.message)
    }
  }
}

My Response is like this: AcceptRanges: 'bytes', LastModified: 2020-02-07T08:04:25.000Z, ContentLength: 12723, ETag: '"ea7de645f93c45b3jkj4e7ffjdsf"', ContentType: 'application/octet-stream', Metadata: {}, Body: Buffer 7b 0d ...

In the body i get a Buffer from my JSON, if I convert it via tools like: https://onlineutf8tools.com/convert-bytes-to-utf8

I get my JSON string like i want it. How can i do that in Javascript/nodejs ? I don't need the Buffer, I need the JSON in String. I tried different ways, but it didnt work yet.

aw123
  • 243
  • 1
  • 2
  • 12

2 Answers2

38

I don't have the reputation to add comment for @Ashish Modi's answer. The data.Body.toString('utf-8') will convert it to plain string not to json object.
If you want to have the json object you can convert the string to json object by JSON.parse(obj.Body.toString('utf-8'))

user1497277
  • 439
  • 1
  • 5
  • 8
  • Confirming this works for me too, this will also parse if you are returning an array of json objects from a json file if that makes sense, it will parse it in as an array. – perustaja Jun 21 '21 at 19:53
  • now u do have the reputation :-) cheers! – OhadR Apr 13 '22 at 09:43
18

You need to do data.Body.toString('utf-8') to get the proper json (string version) from buffer.

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
  • 17
    if i change body: JSON.stringify(data.toString()), I get body: ' " [object Object]" ' how can i access to the string version of the json ? – aw123 Feb 11 '20 at 11:44
  • If you have the "[object Object]" value, you may use the new version of the sdk and need to update your code to download the data as chunks > https://stackoverflow.com/a/36944450/4912329 – callmemath May 16 '23 at 15:46