Pular para o conteúdo

CLI

Atualizado em Ver como Markdown

Manage R2 buckets and objects directly from your terminal. Use CLI tools to automate tasks and manage objects.

Tool Best for
Wrangler Single object operations and managing bucket settings with minimal setup
rclone Bulk object operations, migrations, and syncing directories
AWS CLI Existing AWS workflows or familiarity with AWS CLI

1. Create a bucket

A bucket stores your objects in R2. To create a new R2 bucket:

  1. Log in to your Cloudflare account:

    npx wrangler login
  2. Create a bucket named my-bucket:

    npx wrangler r2 bucket create my-bucket

    If prompted, select the account you want to create the bucket in.

  3. Verify the bucket was created:

    npx wrangler r2 bucket list
  1. In the Cloudflare Dashboard, go to R2 object storage.

    Go to Overview ↗
  2. Select Create bucket.

  3. Enter a name for your bucket.

  4. Select a location for your bucket and a default storage class.

  5. Select Create bucket.

2. Generate API credentials

CLI tools that use the S3 API (AWS CLI, rclone) require an Access Key ID and Secret Access Key. If you are using Wrangler, you can skip this step.

  1. In the Cloudflare dashboard, go to R2.
  2. Select Manage R2 API tokens.
  3. Select Create API token.
  4. Choose Object Read & Write permission and select the buckets you want to access.
  5. Select Create API Token.
  6. Copy the Access Key ID and Secret Access Key. Store these securely — you cannot view the secret again.

3. Set up a CLI tool

Wrangler is the Cloudflare Workers CLI. It authenticates with your Cloudflare account directly, so no API credentials needed.

  1. Install Wrangler:

    
    								
    									
    									npm
    									 i -D wrangler
    								
    							
  2. Log in to your Cloudflare account:

    wrangler login

rclone is ideal for bulk uploads, migrations, and syncing directories.

  1. Install rclone (version 1.59 or later).

  2. Configure a new remote:

    rclone config
  3. Create new remote by selecting n.

  4. Name your remote r2

  5. Select Amazon S3 Compliant Storage Providers as the storage type.

  6. Select Cloudflare R2 as the provider.

  7. Select whether you would like to enter AWS credentials manually, or get it from the runtime environment.

  8. Enter your Access Key ID and Secret Access Key when prompted.

  9. Select the region to connect to (optional).

  10. Provide your S3 API endpoint.

The AWS CLI works with R2 by specifying a custom endpoint.

  1. Install the AWS CLI for your operating system.

  2. Configure your credentials:

    aws configure
  3. When prompted, enter:

    • AWS Access Key ID: Your R2 Access Key ID
    • AWS Secret Access Key: Your R2 Secret Access Key
    • Default region name: auto
    • Default output format: json (or press Enter to skip)

4. Upload and download objects

(Optional) Create a test file to upload. Run this command in the directory where you plan to run the CLI commands:

echo 'Hello, R2!' > myfile.txt
# Upload myfile.txt to my-bucket
wrangler r2 object put my-bucket/myfile.txt --file ./myfile.txt

# Download myfile.txt and save it as downloaded.txt
wrangler r2 object get my-bucket/myfile.txt --file ./downloaded.txt

Refer to the Wrangler R2 commands for all available operations.

# Upload myfile.txt to my-bucket
rclone copy myfile.txt r2:my-bucket/

# Download myfile.txt from my-bucket to the current directory
rclone copy r2:my-bucket/myfile.txt .

Refer to the rclone documentation for more configuration options.

# Upload myfile.txt to my-bucket
aws s3 cp myfile.txt s3://my-bucket/ --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com

# Download myfile.txt from my-bucket to current directory
aws s3 cp s3://my-bucket/myfile.txt ./ --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com

# List all objects in my-bucket
aws s3 ls s3://my-bucket/ --endpoint-url https://<ACCOUNT_ID>.r2.cloudflarestorage.com

Refer to the AWS CLI documentation for more examples.

Next steps

CORS

Configure CORS for browser-based uploads.