Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node js - Configuring aws s3 images on upload

What's working so far:

Using this function, I'm taking images that are uploaded to my server, sending them to an aws S3 bucket and then deleting them from my machine. That's all working great.

The problem:

How do I configure the image so that amazon serves it as public and with the proper Content-Type(image/jpeg or image/png)? Right now it defaults to private and (application/octet-stream).

Is this something that I can configure in node? or do I need to do that 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);
        }
      });
    });
  }
like image 749
heckascript Avatar asked Nov 14 '14 21:11

heckascript


Video Answer


1 Answers

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/

like image 76
heckascript Avatar answered Sep 19 '22 08:09

heckascript