Node js - Setting up aws s3 images on boot

What works so far:

Using this function, I take images that are uploaded to my server, sending them to the aws S3 bucket and then deleting them from my machine. This all works great.

Problem:

How to configure an image so that amazon serves it as public and with the appropriate type of content (image / jpeg or image / png)? Currently, the default is private and (application / octet-stream).

Is this something I can configure in node? or do i need to do this in my aws console?

function sendFileToAmazon(file) { var s3bucket = new AWS.S3({ params: {Bucket: 'BUCKET NAME'} }); var params = {Key: file.name, Body: ''}; fs.readFile(file.path, function(err, data) { if (err) throw err; params.Body = data; s3bucket.putObject(params, function(errBucket, dataBucket) { if (errBucket) { console.log("Error uploading data: ", errBucket); } else { console.log(dataBucket); deleteFileFromTmp(file); } }); }); } 
+9
source share
1 answer

This is the best source for answering my question, although I did not initially 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 pairs by turning:

 var params = {Key: file.name, Body: ''}; 

in

 var params = {Key: file.name, Body: '', ContentType: file.mimetype, ACL: 'public-read'}; 

EDIT:

Instead of specifying the mimetype file type, you can only allow specific mimety types, such as: 'image / jpg', 'image / jpeg', 'image / png', 'image / gif', etc.


EDIT # 2: The original setup issue on boot, but this also probably relates to some of the people looking at this:

https://aws.amazon.com/premiumsupport/knowledge-center/s3-allow-certain-file-types/

+29
source

Source: https://habr.com/ru/post/1206914/


All Articles