Tuesday, August 18, 2015

Adding Cache-Control Headers to Existing Files in a S3 Bucket

Around 6 months ago I started working on a MEAN based Blogging Platform and last month it reached a point where I could use it create a new blog from scratch. The blog is called wisdomtoinspire.com and it's been picking up pretty quick since it's launch.

As the Blogging platform I built is a very generic manner (i.e. can be used to host any type of blog and content type), I opted to using AWS S3 for storage of all the assets. It's mainly used to store Images and content which I upload via my Blogging CMS.

I use S3 for Storage. It's very affordable...

After around one month of feeding in assets into the S3 bucket, I realised that I had forgot to set the all important "Cache-Control" headers for each image I uploaded into S3 (This was slowing down my blog load times as all assets were NOT being cached by the user browsers). I then quickly discovered that there was no way to set Cache-Control headers on a bucket level, so I was left with 2 options:

  1. Go to the S3 Console Panel and select each one of my 600 plus images one by one and manually had the Cache-Control header for each. 
  2. Write a script that will use the AWS SDK and automate this.

Being a coding freak obsessed with automation and scripting, I opted for option 2. I also didn't have much time to work out how to do this, but I went over the AWS S3 SDK documentation and wrote a NodeJs script built on top of the aws-sdk NPM package that let's you enter your S3 bucket and specify the Cache-Control you need and it loops through each Object in the S3 Bucket and update the Cache-Control header.

At a high level, how I do this is to copy the Object into the same Bucket with the same name and specify a "Replace" which effectively updates the object. This seems to be the only way to do this based on my reading around, but please comment below if you disagree.

The Script is called s3 bucket cache control and you can get it from here https://github.com/newbreedofgeek/s3-bucket-cache-control 

Hope this helps...

No comments:

Post a Comment

Fork me on GitHub