I am creating a simple function that uploads a single file which is less than 5 GB to AWS S3 bucket. The function neither returns any data nor error. It just returns an empty response.
The code is as follows,
const csvUploadParams = {
Bucket: Bucketname,
Key: fileName + ".csv",
Body: "",
ContentType: "text/csv"
};
let response = new Promise(async (resolve, reject) => {
console.log("Inside the response block"); //gets invoked
try{
console.log("Inside the try block of response"); // gets invoked
const csvParam = new PutObjectCommand(csvUploadParams);
await s3.send(csvParam,function(err,data){
if(err){
console.log("Error in uploding file", err); // not invoked
reject(err);
} else{
console.log("Response obtained", JSON.stringify(data));//not invoked
resolve(data);
}
});
} catch(err){
console.log("Error in uploading to S3 bucket", err); // not invoked
throw err;
}
});
console.log("CSV Upload params : ", csvUploadParams); // this line prints empty response
Please provide any guidance on why empty response is provided.
You are doing a strange mix of callbacks, promises and even use an async
function as argument to new Promise
which never makes sense.
s3.send
already returns a promise (which is probably why the code doesn't work - I don't know if it would even accept a callback, but if it does, it may not invoke it if it sees it's being await
ed (which internally calls .then
)).
Consider this simplified code:
const csvUploadParams = {
Bucket: Bucketname,
Key: fileName + ".csv",
Body: "",
ContentType: "text/csv"
};
try {
const csvParam = new PutObjectCommand(csvUploadParams);
const data = await s3.send(csvParam);
console.log("Response obtained", data);
} catch (err) {
console.error("Error in uploading to S3 bucket", err);
throw err;
}