Use the AWS CLI with Cloudflare R2

2025-03-22

aws --profile <PROFILE> \
  s3api list-objects-v2 \
  --bucket <BUCKET> \
  --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com

Three important steps to get this working:

  1. Create API keys for R2
  2. Use the AWS CLI to create a profile for your Cloudflare account
  3. Use the --endpoint-url to point to your account on Cloudflare, and the R2 service

Create a profile for S3 usage. The easiest way to do this is with aws configure. Or, just edit the files in ~/.aws/config and ~/.aws/credentials.

Other calls I've made with the CLI to R2.

# use s3 ls instead of the s3api above
aws --profile <PROFILE> \
  s3 ls s3://<PATH_ON_CLOUDFLARE> \
  --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com

# uploading files
aws --profile <PROFILE> \
  s3 sync \
  <LOCAL_DIRECTORY_PATH> \
  s3://<PATH_ON_CLOUDFLARE> \
  --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com

I like to include the profile directly after the aws for better shell history search completion.

About

Home

Contact Me

mitchell@gritts.me

This Site

Built by me. Writing and images are my original work unless otherwise noted. Please attribute this site when sharing.

This content served from this URL (mitchell.gritts.net) is licensed with the Unlicense. Content served from other URLs may have other licenses. The repo is currently private while I actively build out the site.