Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

294
Views
Cómo leer y cargar imágenes en AWS S3 con Nodejs

He creado el script upload.js que lee el archivo jpg de la imagen desde una unidad local y lo carga en el depósito de AWS S3.

 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); } );

Cuando lo ejecuto con el node upload.js la imagen se carga. Pero cuando lo vuelvo a descargar, la imagen descargada está dañada y no se puede abrir con el visor de imágenes. ¿Qué estoy haciendo mal?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Agregue ContentType: image/jpeg a su objeto params y suelte la base64 encoding , esta configuración también funcionará para usted.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!