Since Mastodon is compatible with Amazon S3, it automatically is also compatible with Cloudflare R2. Why? Because R2 uses the same API endpoints. So, what works with Amazon S3 works with Clouflare R2 as well.
Sounds pretty straight forward, in installing my Mastodon instance with Cloudflare R2 storage, I ran into a few small niggles that made it not working in the first place.
Time needed: 5 minutes
Here’s how to fix that:
- Log into your Mastodon server
Log into your Mastodon server and grant yourself root access with
sudo su
. - Navigate to where Mastodon environment file is located
You’ll find this file located in the following structure (if you’ve installed on Ubuntu, that is):
/home/mastodon/live/
- Edit the configuration file
I use
nano
but you can use whatever editor you prefer. Fornano
it looks like this:nano env.production
If you’ve not configured it to serve from S3 there will be very little info, but if you have, you will have to delete all AWS and S3 lines from that config file. - Use this template as a basis for your configuration
Copy this to a text editor and change the values according to your situation:
S3_ENABLED=true
S3_BUCKET=NAME_OF_R2_BUCKET
AWS_ACCESS_KEY_ID=XYXYXYXYXYXYXYXXY
AWS_SECRET_ACCESS_KEY=XYXYXYXYXYXYXYXY
S3_REGION=auto
S3_PROTOCOL=https
S3_HOSTNAME=XYZ.r2.cloudflarestorage.com
S3_ENDPOINT=https://XYZ.r2.cloudflarestorage.com
S3_ALIAS_HOST=pub-ZYZ.r2.dev
This is ignored by R2, but it needs to be set to something valid
S3_PERMISSION=private
TheAWS Access Key
andAWS Secret Access Key
are your R2 keys, of course. Please take note of when to use and not to usehttps
in front of the values. - Copy and paste this inside your terminal
Copy and paste your version of the above listed template inside your
.env.production
file and save the file. - That should do it.
If you’re moving over from S3 or an existing instance with local storage, you will have to move over the files to the R2 bucket.
But other than that, that should do it.
You know have a fully scalable Mastodon server where you don’t have to worry about storage exceeding your server’s allocation. And cheaply.
Leave a Reply