Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EC2 to resize images stored on S3 on demand

We need to serve the same image in a number of possible sizes in our app. The library consists of 10's of thousands of images which will be stored on S3, so storing the same image in all it's possible sizes does not seem ideal. I have seen a few mentions on Google that EC2 could be used to resize S3 images on the fly, but I am struggling to find more information. Could anyone please point me in the direction of some more info or ideally, some code samples?

Tip

It was not obvious to us at first, but never serve images to an app or website directly from S3, it is highly recommended to use CloudFront. There are 3 reasons:

  1. Cost - CloudFront is cheaper
  2. Performance - CloudFront is faster
  3. Reliability - S3 will occasionally not serve a resource when queried frequently i.e. more than 10-20 times a second. This took us ages to debug as resources would randomly not be available.

The above are not necessarily failings of S3 as it's meant to be a storage and not a content delivery service.

like image 805
RunLoop Avatar asked Jan 29 '13 05:01

RunLoop


1 Answers

Why not store all image sizes, assuming you aren't talking about hundreds of different possible sizes? Storage cost is minimal. You would also then be able to serve your images up through Cloudfront (or directly from S3) such that you don't have to use your application server to resize images on the fly. If you serve a lot of these images, the amount of processing cost you save (i.e. CPU cycles, memory requirements, etc.) by not having to dynamically resize images and process image requests in your web server would likely easily offset the storage cost.

like image 74
Mike Brant Avatar answered Oct 10 '22 11:10

Mike Brant