Hello,
I am trying to upload an audio file to S3 through a Netlify Function. The problem is that I pass a playable Blob of type audio/mpeg
to the Function, but when the Function receives the request, it gets an un-playable string of type text/plain
.
Here is the code of making the request to the Netlify Function:
const uploadAudio = async audioBlob => {
// "audioBlob" is a Blob of type "audio/mpeg" that I tested that I can play as mp3
try {
const response = await fetch("/.netlify/functions/upload-audio", {
method: "POST",
body: audioBlob
});
if (!response) {
return null;
}
const JSONresp = await response.json();
return JSONresp.data;
} catch (error) {
console.log("Error uploading audio", error);
}
};
This is the Function itself, when it gets there, the former “Blob” is only a string now:
const AWS = require("aws-sdk");
const s3 = new AWS.S3({
accessKeyId: process.env.MY_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.MY_AWS_SECRET_ACCESS_KEY
});
export async function handler(event) {
try {
// At this point, event.body is just a long string, not an audio Blob anymore
const audioBlob = event.body;
const params = {
Bucket: "audio",
Key: "uploads/usertest/filetest.mp3",
Body: audioBlob,
ACL: "public-read",
ContentType: "audio/mpeg",
ContentDisposition: "inline"
};
let putObjectPromise = await s3.upload(params).promise();
let location = putObjectPromise.Location;
return {
statusCode: 200,
body: JSON.stringify({ data: location })
};
} catch (error) {
return {
statusCode: 400,
body: JSON.stringify(error)
};
}
}
How do I get the Blob from the client and send to AWS S3 as a proper Blob of type “audio/mpeg”?
Thanks!