Should my bucket be public for my usecase and how should I avoid bad practice?

Viewed 210

I'm new to AWS tools and although I have tried to search thoroughly for an answer I wasn't able to fixate on a solution.

My usecase is this: I have a bucket where I need to store images, upload them via my server however I need to display them on my website.

Should my bucket be public? If not, what should I do to allow everyone to read those images but not be able to mass upload on it from origins who are not my server?

2 Answers

If you want the images to be publicly accessible for your website, then the objects need to be public.

This can be done by creating a Bucket Policy that makes the whole bucket, or part of the bucket, publicly accessible.

Alternatively, when uploading the images, you can use ACL='public-read', which makes the individual objects public even if the bucket isn't public. This way, you can have more fine-grained control over what content in the bucket is public.

Both of these options require you to turn off portions of S3 Block Public Access to allow the Bucket Policy or ACLs.

When your server uploads to S3, it should be using Amazon S3 API calls using a set of AWS credentials (Access Key, Secret Key) from an IAM User. Grant the IAM User permission to put objects in the bucket. This way, that software can upload to the bucket totally independently to whether the bucket is public. (Never make a bucket publicly writable/uploadable, otherwise people can store anything in there without your control.)

Related