Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

288
Vistas
Why is the s3.PutObject not called in this snippet

This node script when ran does not execute the upload, but simply goes straight to the thrown error. Any ideas why?

const AWS = require('aws-sdk')
const fs = require('fs')
const s3 = new AWS.S3()

const filePathName="/tmp/readme.txt"
var numberOfErrors=1

fs.readFile(filePathName, (err,data) => {
    const fileDate = new Date(Date.now()).toISOString()
    const bucket = 'test-bucket';

    console.log('Preparing file...')

    var fileData = new Buffer.from(data);

    const params = {
        Bucket: bucket,
        Key: `readme.txt`,
        ContentType: 'text/html',
        Body: fileData
    }
    
    if (numberOfErrors > 0) { 
        s3.putObject(params, (err) => {
            if (err) { throw err; }
            console.log("File uploaded to S3 " + bucket)                                     
        })           
        throw new Error('There were errors') 
    } else {
        s3.putObject(params, (err) => {
            if (err) { throw err; }
            console.log("File uploaded to S3 " + bucket)                                     
        })
        console.log('There were no errors')
    }    

})

Output is as follows

Preparing file...
sample_test.js:28
        throw new Error('There were errors') 
        ^

Error: There were errors
    at sample_test.js:28:15
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)

Node.js v17.4.0

if I comment out the line where I throw the error it uploads successfully. Any help much appreciated thanks.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda