Serving Static Assets out of Cloudflare R2

Created: by Pradeep Gowda Updated: Nov 04, 2023 Tagged: cloudflare · static-sitegen

Cloudflare offers r2, an S3 compatible cloud storage with zero egress costs.

I wanted someone other than this site to host static assets (files that do not change once created, like - images, PDFs etc; and are not suitable to be stored in a git repo with rest of the content), and R2 was a good solution after considering a few others.

I have a directory caleld files.btbytes.com on my $HOME and I sync it up to the r2 bucket using a shell script using rclone

#!/bin/bash
# sync.sh
rclone -vv sync . btfiles:/btbytes-static-assets/

The btfiles configuration comes from $HOME/.config/rclone/rclone.conf:

[btfiles]
type = s3
provider = Cloudflare
access_key_id = <your access key id>
secret_access_key = <your secret access key>
endpoint = https://<your endpoint>.r2.cloudflarestorage.com
acl = private

I copy the files over - like these image files and run ./sync.sh to copy them to r2.

I then use the Cloudfront dashboard to serve the contents of this bucket as a website, in this case - files.btbytes.com.

As far as I know, R2 does not support automatic index files at the root of the domain/directory. so visiting files.btbytes.com itself will show a 404.