I’m starting this topic as an ongoing discussion about using Cloudflare’s R2 service with Active Storage. The objective is to figure which features of Active Storage are supported, which aren’t, and help others configure their apps for use with Cloudflare.
About R2:
R2 is Cloudflare’s object storage solution. It includes “full” S3 compatibility while being much cheaper. Check the announcement below for the full explanation.
Useful links:
- Announcement: https://blog.cloudflare.com/introducing-r2-object-storage
- Pricing: Pricing · Cloudflare R2 docs
- Cost Analysis vs S3: The Compelling Economics of Cloudflare R2 - Last Week in AWS Blog
- S3 Compatibility: S3 API Compatibility · Cloudflare R2 docs
- Creating R2 Tokens: Generate an S3 Auth token · Cloudflare R2 docs
- Finding your ACCOUNT_ID: Find zone and account IDs · Cloudflare Fundamentals docs
Minimal configuration
cloudflare:
service: S3
endpoint: https://ACCOUNT_ID.r2.cloudflarestorage.com
access_key_id: YOUR_KEY
secret_access_key: YOUR_SECRET
region: auto
bucket: BUCKET_NAME
For the region
attribute, an empty value or us-east-1
are alias to auto
.
Active Storage Supported Features
- Attach:
- Download:
- Display in
img
tag: - Direct Upload:
- Public files:
(missing ACL support for
PutObject
operation)
Progressive Migration
In their annoucement, Cloudflare cited the “progressive migration” feature, to make it easier for existing apps to migration away from S3:
Migrations are designed to be dead simple. After specifying an existing storage bucket, R2 will serve requests for objects from the existing bucket, egressing the object only once before copying and serving from R2.
I’ve checked their documentation and everywhere in the R2 dashboard, but found no mention of this feature, so it’s possible it is not available yet.
In the interim, this can be implemented via workers. An explanation can be found below: