19
votes

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);
        }
      });
    });
  }
2

2 Answers

56
votes

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/

5
votes

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);
        }
    })
});