Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read JSON file stored in aws S3 with Nodejs

I managed to read a JSON file stored in my S3 bucket but I'm having to do a lot of transformation that I dont fully understand.

If we log the data as it comes we get an output of Buffer data

s3.getObject(objParam, (err, data) => {
  if (err) throw err;
  
  console.log("🚀 ~ file: reader.js ~ line 31 ~ rs ~ data", data.Body)

});

// outputs  data <Buffer 7b 0a 20 20 22 74 79 70 65 22 3a 20 22 42 75 66 66 ...

If we convert the above buffer to string with toString('utf-8') I get this:

data.Body.toString('utf8')

// outputs:
{
  "type": "Buffer",
  "data": [
    123,
    34,
    99,
    115,
    112,
    45,
    114,
    101,
    112,
    111,
    114,
....

The only way I managed to see my actual original JSON was converting the above to JSON, accessing the data property, creating a new buffer, then converting back again to toString('utf-8').

s3.getObject(objParam, (err, data) => {
  if (err) throw err;

  const jsonBytes = JSON.parse(data.Body.toString('utf-8'));
  const buffer = Buffer.from(jsonBytes.data);
  const bufferBackToString = buffer.toString('utf-8');
  console.log("🚀 ~ file: reader.js ~ line 21 ~ s3.getObject ~ bufferBackToString", bufferBackToString);

});

// output: Logs my original JSON!
{
"myProperty": "myData"
} // or whatever...

If it was already a buffer, why did I have to convert to one and convert back again to string? Could the buffer have different encoding?

like image 766
JBird Avatar asked Oct 19 '25 15:10

JBird


2 Answers

const jsonBytes = JSON.parse(data.Body.toString('utf-8'));
console.log(jsonBytes);

worked for me , I did see in forums , that s3.getObject returns json directly not string

like image 177
Abderrahmen BRINIS Avatar answered Oct 21 '25 05:10

Abderrahmen BRINIS


const getJsonFromS3 = async (filename, bucketName) => {
  const s3Output = await s3.getObject({
    Key: filename,
    Bucket: bucketName
  })

  try {
    const jsonString = await s3Output.Body?.transformToString()
    const json = JSON.parse(jsonString ?? '')
    return json
  } catch (error) {
    console.error('error parsing json', error)
    return null
  }
}

all suggestions with data.toString('utf-8') not working for me. but using transformToString() worked for me

like image 37
SemyenP Avatar answered Oct 21 '25 05:10

SemyenP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!