I'm trying to upload an image to my S3 bucket from a webpage using node.js. I am able to create a folder, but uploading the image file to that folder is not working for me. Here are my dependencies in my package.json
"dependencies": {
"aws-sdk": "^2.331.0",
"bcrypt-nodejs": "0.0.3",
"busboy": "^0.2.14",
"busboy-body-parser": "^0.3.2",
"connect-busboy": "0.0.2",
"ejs": "^2.6.1",
"express": "^4.16.3",
"express-handlebars": "^3.0.0",
"express-session": "^1.15.6",
"mysql": "^2.16.0",
"mysql2": "^1.6.1",
"passport": "^0.4.0",
"passport-local-sequelize": "^0.8.0",
"path": "^0.12.7",
"sequelize": "^4.39.0"
}
and this is the part of my api_routes.js file that I'm working with
const bucketName = 'socialmemedia';
const bucketRegion = 'us-east-1';
AWS.config.update({
region: bucketRegion
});
AWS.config.apiVersions = {
s3: '2006-03-01',
};
const s3 = new AWS.S3();
module.exports = function (app) {
// upload image to S3
app.post("/api/upload", function (req, res) {
const file = (req.body.imageUpload);
const busboy = new Busboy({
headers: req.headers
});
busboy.on('finish', function () {
console.log('Upload finished');
console.log(file);
uploadToS3(file);
});
req.pipe(busboy);
});
function uploadToS3(file) {
// console.log(req.body);
const folder = (req.user.username + "/")
console.log("Folder name: " + folder);
const params = {
Bucket: bucketName,
Key: folder,
ACL: 'public-read',
Body: stream
};
s3.upload(params, function (err, data) {
if (err) {
console.log("Error: ", err);
} else {
console.log("Successfully created a folder on S3");
}
});
res.redirect("/feed");
}
};
and here is the form snippet
<form action="/api/upload" method="POST" class="image-form">
<input id="image-upload" class="file-add" type="file" accept="image/*" name="imageUpload"/>
<button type="submit" id="image-upload" class="sinsup-button">Upload Image</button>
</form>
any idea on how to get the image to actually upload into the S3 bucket?
jpg . Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.
I figured it out, thanks for your help @me. I was using s3.upload instead of s3.putObject. Answering to help anyone else who may have this problem in the future.
// upload image to S3
app.post("/api/upload", function (req, res) {
const folder = (req.user.username + "/");
const file = (req.body.imageUpload);
const params = {
Bucket: bucketName,
Key: (folder + file),
ACL: 'public-read',
Body: file
};
console.log("Folder name: " + folder);
console.log("File: " + file);
s3.putObject(params, function (err, data) {
if (err) {
console.log("Error: ", err);
} else {
console.log(data);
}
});
res.redirect("/feed");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With