If you’ve been around the ol’ static website hosting block a few times, you’ve probably come across the possibility of hosting your site out of Amazon S3 and maybe sticking Cloudfront in front of it for good measure. That stack looks like the image below, assuming we use Amazon for everything.
The big three cloud providers all have their bucket solutions, which is to say they all provide a way to store files without having to worry about hard drives. Amazon’s bucket service is called “S3” (short for Simple Storage Service), Google’s is called “Cloud Storage”, and Microsoft’s is called “Blob Storage”.
In recent years a new challenger has appeared, a company called Backblaze has begun offering a service called “B2” that is wildly cheaper than the competition. I’m always rooting for the underdog, so let’s take a look at hosting a static website from B2.
Using B2 to Host a Site?
B2 does allow you to set a bucket to “public”, and then provides a url where anyone can download the files. That’s a good start, but there are three problems that stand between us and an awesome static website.
- 1: The url B2 provides isn’t exactly something that would make for a catchy website name.
- 2: Files served out of B2 don’t have the right content-type headers present. For example, if I type in the path to an index.html file hosted on B2, it prompts me to download the html as a file, rather than rendering it as a webpage.
- 3: There are some nice things that webservers tend to automatically do, like serving an index.html file if the user requests a base path like “/”
Luckily we can solve all three issues using a cdn service called Cloudflare. With that addition, our B2 static hosting stack looks like this:
Cloudflare has a bunch of different services, but there’s really only one we need for this project: Cloud Workers. Cloud Workers let you write little scripts that get called when a user requests a url. We’ll be looking at the url that the user requested, loading the specified file from B2, adding in the right content-type headers, and then serving the file back to the user. Here’s the script I put together for the cloud worker:
I’ll admit, writing a somewhat complex script was not what I had in mind when starting on the project. Sometimes the internet is just a crazy place.
- B2 Stack (Cache miss): 700ms
- B2 Stack (Cache hit): 239ms
- S3 Stack (Cache miss): 498ms
- S3 Stack (Cache hit): 230ms
In case you’re wondering, “Cache miss” means that the file was not found at the CDN layer. That’s Cloudfront for the S3 stack or Cloudflare for the B2 stack. Since the file is not found in the CDN, the CDN will load the file from the origin and then serve it, usually with some kind of header indicating a “cache miss”. If the CDN has the file it can provide it right away, resulting in a faster “cache hit”.
B2 is considerably cheaper than S3, but it’s kind of like saying 0.00001 dollars is ten times cheaper than 0.0001 dollars. Storage is so cheap that you probably won’t care, unless you’re hosting large video files or something that will take up hundreds of gigs of storage.
As much as I would like to pull for the underdog, the S3 stack is considerably easier to set up and use. S3’s UI is nicer to use. S3 can serve your files directly without needing to write a script that runs on every request. The B2 stack is cheaper, but for small to medium sites the difference will be negligible.