Node js - Configuring aws s3 images on upload
This is the best source to answer my question, although I didn't originally find my answer here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
I found my answer here: http://blog.katworksgames.com/2014/01/26/nodejs-deploying-files-to-aws-s3/
You can simply add:
ContentType: file.mimetype, ACL: 'public-read'
to the params, turning:
var params = {Key: file.name, Body: ''};
into
var params = {Key: file.name, Body: '', ContentType: file.mimetype, ACL: 'public-read'};
EDIT:
Rather than supplying the file's mimetype, you could only allow particular mimetypes such as: 'image/jpg', 'image/jpeg', 'image/png', 'image/gif', etc...
EDIT #2: Original question is about configuring on upload, but this is also probably of relevance for some of the folks viewing this:
https://aws.amazon.com/premiumsupport/knowledge-center/s3-allow-certain-file-types/
I have added ContentType as below to fix this issue. For png or jpg, even without ContentType it will work. But if for uploading svg, you have to send ContentType property to S3 Bucket.
router.post(uploadImagePath, function (request, response) {
var form = new formidable.IncomingForm();
form.parse(request, (err, fields, files) => {
try {
let ImageName = files.file.name;
fs.readFile(files.file.path, function (err, data) {
let params = {
Bucket: 'Bucket-name',
Key: Date.now() + "_" + ImageName,
Body: data,
ACL: 'public-read',
ContentType: files.file.type
};
s3.upload(params, function (err, data) {
response.send(200, { key: data.key })
});
});
}
catch (e) {
response.send(500);
}
})
});