Skip to main content

CLI

还没翻译译自英文(developers.cloudflare.com)本页 39 段都还没译

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

ToolBest for
WranglerSingle object operations and managing bucket settings with minimal setup
rcloneBulk object operations, migrations, and syncing directories
AWS CLIExisting AWS workflows or familiarity with AWS CLI

1. Create a bucket

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

Wrangler CLIDashboard

  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

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

WranglerrcloneAWS CLI

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

  1. Install Wrangler:

    npmyarnpnpmbun

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

    wrangler login

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

WranglerrcloneAWS CLI

# 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.

Next steps

Presigned URLs

Generate temporary URLs for private object access.

Public buckets

Serve files directly over HTTP with a public bucket.

CORS

Configure CORS for browser-based uploads.

Object lifecycles

Set up lifecycle rules to automatically delete old objects.