2025-03-22cloudflare

Use the AWS CLI with Cloudflare R2

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.