Node js - Configuring aws s3 images on upload
Asked Answered
C

2

21

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);
        }
      });
    });
  }
Conditioned answered 14/11, 2014 at 21:22 Comment(0)
C
62

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/

Conditioned answered 14/11, 2014 at 22:22 Comment(6)
Be careful of the ContentType property! I was tearing my hair out until I noticed that I was using Content-Type as the property name.Ury
Wouldn't this solution allow for someone to upload any type of file? This doesn't restrict it to images as far as I can tell.Meshed
Yeah I think you're right, looking back at this now... I'll update the ContentTypeConditioned
This answer just saved meMaize
even tho it appears as a Metadata in a lot of places, it DOES NOT work if you PUT it as {"Key":key, "Body": body, "Metadata": {"Content-Type": "mime/type"} }Yemen
In my case mimetype was in file.typeMillymilman
B
6

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);
        }
    })
});
Binghi answered 18/11, 2020 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.