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

287
Views
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
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!