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.