Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

323
Visualizações
nodejs how to get JSON instead of Buffer from aws s3 bucket

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.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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'))

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda