π¦ Cloudflare R2 β Using the S3-Compatible API
This guide shows how to configure and upload files to Cloudflare R2 using the AWS CLI and S3-compatible API.
π 1. Configure AWS CLI for Cloudflare R2
Use the following command to set up a new AWS CLI profile for R2:
aws configure --profile r2
Provide the following when prompted:
- Access Key: (Get this from R2 β Manage API Tokens)
- Secret Key: (Also from the same place)
- Region:
auto
or any valid AWS region likeus-east-1
- Output format:
json
(or your preferred format)
π€ 2. Upload a single file to R2
aws s3 --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com \
cp myfile.txt s3://my-r2-bucket/ --profile r2
β Replace:
<ACCOUNT_ID>
with your actual Cloudflare Account IDmyfile.txt
with your local file namemy-r2-bucket
with your R2 bucket name
π 3. Upload all files in current folder to R2
To recursively upload everything in the current directory:
aws s3 sync . s3://my-r2-bucket \
--endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com \
--profile r2
This command:
- Recursively uploads files from the current folder
- Skips files that haven't changed (based on timestamp and size)
π Notes
- R2 is S3-compatible, so standard S3 tools and SDKs will work.
- Zero egress fees β great for serving content publicly.
- You can also use tools like
s3cmd
,rclone
, or SDKs likeboto3
for programmatic access.