I have a scenario where I need to upload a .csv file from a react frontend, using axios, to an AWS Lambda function written in Node.js, through AWS API Gateway. From there I will process the lines of the csv file and save them to a database.
So far I've managed to get the data into the lambda function using the code below:
React Snippet
...
export const upload = async (file: any) => {
await axios({
method: "post",
url: url,
data: file,
headers: { "Content-Type": "multipart/form-data" },
})
.then(function (response) {
//handle success
console.log(response);
return response;
})
.catch(function (response) {
//handle error
console.log(response);
return response;
});
const upload = async () => {
setIsLoadng(true);
if (selectedFile) {
debugger;
const formData = new FormData();
formData.append("File", selectedFile);
await upload(formData);
setIsLoadng(false);
} else {
setIsLoadng(false);
alert("Please choose a file to upload.");
}
};
...
but it arrives encoded in base64. At this point this is how I extract the data from the lambda function:
AWS Lambda Function
exports.handler = async (event) => {
try {
console.log(`Event: ${JSON.stringify(event)}`);
// Decoding the base64 encoded body from AWS API Gateway
let buff = Buffer.from(event.body, "base64");
console.log(`Buffer data decoded: ${buff}`);
console.log(`Type: ${typeof buff}`);
const response = {
statusCode: 200,
body: JSON.stringify("Some data here..."),
};
return response;
} catch (err) {
console.log(err);
const response = {
statusCode: 200,
body: JSON.stringify(err),
};
return response;
}
};
This is all fine and well, however, after decoding it I cant seem to parse it easily to JSON format for processing because the data is surrounded by a webkit form boundray:
'------WebKitFormBoundary0YYQOz5kaQyRTGk4
Content-Disposition: form-data; name="File"; filename="file001.csv"
Content-Type: application/vnd.ms-excel
...
the actual .csv data
...
------WebKitFormBoundary0YYQOz5kaQyRTGk4--
'
I think that axios has formatted my data and sent the request correctly based on content-type: multipart/form-data. I also believe that AWS API Gateway encodes data like this into base64 which is what I receive in the lambda function. However, once I decode the data I get this string that includes all the header information and the "------WebKitFormBoundary0YYQOz5kaQyRTGk4" top and bottom bits.
Is there a better way to handle the .csv data so that I can have a nicely parsed JSON object in my lambda function to then process. So far I've been trying to hack together a string "find and concat" solution but this feels so wrong.
Any help or guidance is sincerely appreciated!
B