Reputation: 1201
Hi I am currently looking for library that uses AWS Lambda Proxy Integration
service from amazon that would help me parse formdata
and send the data using https
library using nodejs. A sample code will do as well on how to get the body and the file from the formdata
request.
Please someone let me know if there is one. Your help would be much appreciated.
Upvotes: 0
Views: 1329
Reputation: 1538
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!');
});
Upvotes: 1