Try this code, complete the form element as per your need.
HTML form
<form id="hfrm" method="POST" action="/fileUpload" enctype="multipart/form-data">
</form>
Use AJAX to submit the code
var form = $('#hfrm').get(0);
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open("POST", "URL");
xhr.send(formData);
The server side code to handle the request, please note there are few places where you must use credentials and names like AWS Region, S3 bucket name etc.
var xpress = require('express')
const fileUpload = require('express-fileupload');
const app = xpress();
app.use(fileUpload());
var AWS = require('aws-sdk');
app.post('/fileUpload', async (req, res) => {
AWS.config.update({
accessKeyId: "ACCESS-KEY",
secretAccesskey: "SECRET-ACCESS-KEY",
region: "AWS REGION"
})
const s3 = new AWS.S3();
const fileContent = Buffer.from(req.files.uploadedFileName.data, 'binary');
const params = {
Bucket: 'S3 BUKET-NAME',
Key: "Name of the File",
Body: fileContent
};
s3.upload(params, function(err, data) {
if (err) {
throw err;
}
res.send({
"response_code": 200,
"response_message": "Success message",
"response_data": data
});
});
})
app.listen(3000, function () {
console.log('Sample app listening on port 3000!');
});