B2 Caching Control
Reading Time: 2 min
Backblaze B2 storage and Cloudflare have a partnership that allows free egress from B2 storage through the Cloudflare CDN. What this means in effect is free bandwidth for serving your images and videos. I wanted to set up this system for my Hugo based blog but I ran into an undocumented step that I wanted to detail here.
The basic idea of free egress through Cloudflare is to use your own domain to serve your B2 bucket contents over http. So in this instance I created a photos.mattstenson.com cname with an alias to f001.backblazeb2.com. You can check out the documentation on setting this up here: https://help.backblaze.com/hc/en-us/articles/217666928-Using-Backblaze-B2-with-the-Cloudflare-CDN
After getting this setup however, I discovered that none of my images where being served through the Cloudflare CDN, in fact they weren’t being cached at all. I discovered that all uploads and buckets by default in b2 have nocache directives on them. Digging into the documentation it wasn’t immediately clear how to modify this. Thankfully the solution was stupid simple if a bit opaque to change from the web interface.
- Click on the “Settings” option for the bucket in question
- In the textbox called “Bucket Info” you can add a json parameter for cache control:
- Save your changes and b2 will start serving cache controlled headers within 10 minutes
Hopefully this saves someone else some digging.