Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active August 4, 2024 16:04
Show Gist options
  • Save balupton/036833ab9cef4af8778e46d159c3569d to your computer and use it in GitHub Desktop.
Save balupton/036833ab9cef4af8778e46d159c3569d to your computer and use it in GitHub Desktop.
Upload a File to Cloudflare R2 Instructions

Upload a File to Cloudflare R2 Instructions

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:

INSTALLING RCLONE

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.

CREATING THE BUCKET

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.

CREATING THE R2 TOKEN

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

VERIFYING OUR RCLONE CONFIGURATION

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

UPLOADING OUR FILE

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment