The instructions are multifold, before we begin, consider what you will call the bucket for your files. E.g. my-storage-bucket
.
Cloudflare R2 is free for the first 10GB of storage, then is $0.015/GB-month of storage. R2 uniquely does not charge for transfers, for uploading and downloading.
References used to construct this guide:
- https://developers.cloudflare.com/r2/pricing/#class-a-operations
- https://developers.cloudflare.com/r2/api/s3/tokens/
- https://developers.cloudflare.com/r2/examples/rclone/
- https://rclone.org/downloads/
- https://rclone.org/commands/rclone_copy/
- https://forum.rclone.org/t/copy-progress-help/5323/3
Unfortunately for uploading files larger than 300MB, Cloudflare R2 requires special tooling. We will use Rclone for this, it is a terminal application.
You can install Rclone on your system via: https://rclone.org/install/
Once installed, open a Terminal and run the following:
rclone config file
It will tell you where the configuration file is location, e.g. /Users/username/.config/rclone/rclone.conf
Open the configuration file in your editor of choice, and copy and paste the following into it:
[my-storage-bucket]
type = s3
provider = Cloudflare
access_key_id = ...
secret_access_key = ...
endpoint = ...
acl = private
Keep this editor open.
Create a R2 bucket if you haven't already, do this by the left sidebar, navigate to R2
=> Overview
, then clicking Create bucket
.
Set the Bucket name
to whatever you want, e.g. my-storage-bucket
.
Keep the Location
as Automatic
.
Click Create bucket
.
Once the bucket is created, you will see three tabs: Overview
, Metrics
, Settings
.
Click Settings
.
Under R2.dev subdomain
click the Allow Access
button, and confirm.
Copy the S3 API
url, and insert it as the endpoint
value in our rclone config editor:
endpoint = ...
Also make sure the [my-storage-bucket]
line matches what you called the bucket, keeping the square brackets.
In the left sidebar, navigate to R2
=> Overview
.
In the right sidebar, navigate to Manage R2 API Tokens
Click Create API token
.
Name the token whatever you want, e.g. my-storage-token
.
Set the permissions to Object Read & Write
.
Set the Specify bucket(s)
to Apply to specific buckets only
and select the bucket you created earlier.
Click Create API Token
.
Copy the Access Key ID
and insert it as the access_key_id
value in our rclone config editor:
access_key_id = ...
Copy the Secret Access Key
and insert it as the secret_access_key
value in our rclone config editor:
secret_access_key = ...
Our rclone configuration should now have the correct bucket name, and have each ...
replaced with the correct value, looking like:
[my-storage-bucket]
type = s3
provider = Cloudflare
access_key_id = adsasdase123123qasd
secret_access_key = asdasd123123qsdasd
endpoint = https://asdasd123123.r2.cloudflarestorage.com/my-storage-bucket
acl = private
Save the configuration file, keep the editor open for now.
We can verify the configuration is correct by running the following in our terminal, replacing my-storage-bucket
with our bucket name, keeping the trailing colon :
rclone tree my-storage-bucket:
It should output:
/
0 directories, 0 files
Open a terminal and navigate to the directory of our files:
cd the-directory-our-files-are-located
We can then upload a file with progress updates every minute via, replacing my-storage-bucket
with our bucket name, and keeping or changing uploads
(this is the name of the directory we create inside the bucket to store our uploaded files, it can be called anything, but in this example we use uploads
)
rclone copy -v ./the-file-we-want-to-upload my-storage-bucket:uploads/
Once the file has uploaded, go back to Cloudflare.
In the left sidebar, navigate to R2
=> Overview
.
Click the bucket we uploaded our file to.
We should now see in the Objects
table, the directory we created during upload, click it.
We should now see our file, click it.
We should now see a Public Bucket
link, this is the link we can share with people.