#nginx


How to cache S3 stored pictures to local disk and serving them from your web server

How to cache S3 stored pictures to local disk and serving them from your web server

The problem

Recently I worked on upgrading a legacy Laravel v4.2 to a newer version of Laravel. The site stored user uploaded pictures, which where stored locally on the app's public folder.

Now, I didn't like the fact that those pictures where stored locally because if the server went down the picture files would go down as well. Therefore deploying to a new server would require the hassle of manually restoring the pictures from a backup.

I was thinking to upload all images to S3 and serve them from there, but on the other hand I didn't want to get additionally charged from AWS S3 for the pictures bandwidth. 

How could I get a local cache of those pictures? Here is the solution that worked for my case.