How To Use Mastodon With Cloudflare R2

Home » Notes » How To Use Mastodon With Cloudflare R2

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:

  1. Log into your Mastodon server

    Log into your Mastodon server and grant yourself root access with sudo su.

  2. 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/

  3. Edit the configuration file

    I use nano but you can use whatever editor you prefer. For nano 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.

  4. 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

    The AWS Access Key and AWS Secret Access Key are your R2 keys, of course. Please take note of when to use and not to use https in front of the values.

  5. 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.

  6. 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.

First name
This field is for validation purposes and should be left unchanged.

Automatically sent to your inbox 🪄 

Leave a Reply

Your email address will not be published. Required fields are marked *