Home > Back-end >  Architecture for serving images from S3 bucket securely
Architecture for serving images from S3 bucket securely

Time:12-16

I'm building a small website that involves users uploading images that will be displayed later. The images are stored in an S3 bucket.

Sometimes, I need to display a lot of these images at once, and I'm not sure how best to accommodate that, without allowing public access to S3.

Currently, when there's a request to the server, the server downloads the object from S3, and then returns the file to the client- This is understandably slow. I would love to just be able to return the S3 URL and have the client load from there (So the traffic doesn't have to pass through my server and I don't have to wait for the image to download from S3->Server->Client, but I also don't want S3 bucket urls that are just unsecured and that anyone can go to.

What is the best architecture to solve this? Is there a way of giving people very brief temporary permission to a bucket? Is it possible to scope that to a specific url?

I looked around on stackoverflow and github for similar questions, but most of them seem to have to do with how the files are uploaded and not accessing them securely.

CodePudding user response:

To serve images from an S3 bucket securely, you should use Amazon CloudFront. CloudFront is a content delivery network (CDN) that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, and security. It integrates with S3 to securely serve images from the bucket. You can also use signed URLs or signed cookies to control access to the images.

CodePudding user response:

As suggested by @jarmod, you can pre-sign your objects' URL.

In this case, once you need to share an image, you need to create a pre-sign URL for the object and share this URL.

Your server will only provide the URL. The user will access the image directly, without your server in the middle of the request.

The AWS site explains how to use pre-sign URLs:

  • Related