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

293
Visualizações
How to read and upload image to AWS S3 with Nodejs

I have put together upload.js script that reads the image jpg file from a local drive and uploads it to AWS S3 bucket.

var fs = require('fs');
var AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-1'});


const BUCKET_NAME = 'my-bucket-name';


let filepath = '/home/user/test-image.jpeg';

const content = fs.readFileSync(filepath, {encoding: 'base64'});

let params = {
  params: {
    Bucket: BUCKET_NAME,
    Key: 'test.jpeg',
    Body: content
  }
};

var upload = new AWS.S3.ManagedUpload(params);

var promise = upload.promise();

promise.then(
  function(data) {
    console.log("Successfully uploaded photo.");
  },
  function(err) {
    console.error("There was an error uploading: ", err.message);
  }
);

When I run it with node upload.js the image is uploaded. But when I donwload it back the downloaded image is corrupted and cannot be opened with image viewer. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add the ContentType: image/jpeg to your params object and Loose the base64 encoding, this configuration will work for you as well.

var fs = require('fs');
const AWS = require('aws-sdk');
// const s3 = new AWS.S3();
AWS.config.update({ region: 'us-east-1' });
const mime = require('mime');

const BUCKET_NAME = 'my-bucket-name';

let filepath = '/home/user/test-image.jpeg';

const content = fs.readFileSync(filepath);
console.log(mime.getType(filepath));

let params = {
  params: {
    Bucket: BUCKET_NAME,
    Key: 'cancel.jpeg',
    Body: content,
    ContentType: mime.getType(filepath),
  },
};

var upload = new AWS.S3.ManagedUpload(params);

var promise = upload.promise();

promise.then(
  function (data) {
    console.log('Successfully uploaded photo.');
  },
  function (err) {
    console.error('There was an error uploading: ', err.message);
  }
);

about 4 years ago · Juan Pablo Isaza 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