Using Cloudflare R2 object storage with Nextcloud

By Swee

Don’t have enough storage on your nextcloud box? You can use Object Storage providers like Cloudflare R2, I will how you how.

Why I recommend Cloudflare R2

Cloudflare offers cheap object storage, and Nextcloud has decent performance with R2.

1. Storage capacity

  • 10GB free each month, then ~$0.02 each GB exceeded

This means you can have infinite storage for free, but you will be charged a small amount for the month if you exceed 10GB in that month.

2. Low costs for I/O

  • 1 Million Write operations for free each month, then $4.50 USD for the other millions
  • 10 Million Read operations for free each month, then $0.40 USD for the other millions

This means you will have 1M write operations and 10M read operations completely for free, If you host a personal Nextcloud instance (like I do), then it is very unlikely for your instance to exceed this in a matter of a month.

If you want to check more on Cloudflare’s pricing, check out their R2 pricing calculator.

Configuring Nextcloud

To do this, you must have Nextcloud extracted but not installed via the web UI yet, here’s a tutorial on installing nextcloud so you can get started.

Now, assuming Nextcloud is in /var/www/nextcloud we are going to create a new configuration file while we create our storage bucket.

nano /var/www/nextcloud/config/s3.config.php

On another browser window, go to the Cloudflare Dashboard and go to R2 > Overview

In that page, create a new bucket, doesn’t matter what the name is.

Then, go to your bucket’s Settings, and copy the “S3 API” url.

After this, go back to the R2 overview page and create an API token

This should be the configuration for the API token

For extra security, you can restrict what IP can use the API token

When you click create, do not copy the Token Value, copy the Access Key ID and Secret Access Key.

Now you are ready to configure Nextcloud pre-install.

Go back to your terminal, and write a configuration like this:

<?php
$CONFIG = array (
  'objectstore' => [
       'class' => '\\OC\\Files\\ObjectStore\\S3',
       'arguments' => [
               'bucket' => 'nextcloud-tutorial',
               'hostname' => 'xxxxx.r2.cloudflarestorage.com',
               'key' => 'access_key_id',
               'secret' => 'secret_access_key',
               'use_path_style' => true,
               'region' => "auto",
               'use_ssl' => true,
               'use_path_style' => true,
               'autocreate' => true,
               'verify_bucket_exists' => true,
               'sse_c_key' => 'your_encryption_key_here',
       ],
],
);

Here’s what you need to change:

  • Change nextcloud-tutorial to your R2 bucket’s name.
  • Change xxxxx.r2.cloudflarestorage.com to the S3 API URL you copied, remove the bucket name from the URL.
  • Change access_key_id to the Access Key ID you copied.
  • Change secret_access_key to the Secret Access Key you copied.
  • Change your_encryption_key_here to an encryption key, use openssl rand --base64 32 to generate one, this is optional, if you don’t want to encrypt, delete the line.

Then, save your file by pressing CTRL-S then exiting nano by pressing CTRL-X

After that, you can access your Nextcloud URL and install Nextcloud like normal. Instead of files being stored in internal storage, your files are now in an R2 bucket, enjoy the free space!

Comments