Cloudflare R2 S3 端点设置(2026):CLI、SDK 与 Mac GUI
**简短回答:**Cloudflare R2 的 S3 API 端点是 https://<account-id>.r2.cloudflarestorage.com。使用 R2 API 令牌的 Access Key ID 和 Secret Access Key 作为凭证,并将区域设置为 auto。任何支持自定义端点的 S3 客户端都可以使用这三个值。
本指南先说明在哪里找到每个值,然后给出适用于 AWS CLI、Python boto3、JavaScript SDK v3、s5cmd、rclone、Cyberduck 和 Mac S3 GUI 的可直接复制的设置。
更喜欢 GUI?下载 BucketMate 或打开 Web 应用。同一个 R2 连接也可在 iPhone 上使用。完整的 GUI 教程:macOS 上的 Cloudflare R2 GUI。客户端对比:macOS 最佳 S3 GUI。
1)获取你的 R2 连接凭证
你需要 3 样东西:
- 端点 URL(例如
https://<account-id>.r2.cloudflarestorage.com) - Access Key ID
- Secret Access Key
在哪里可以找到它们:
- 在 Cloudflare 控制台中,进入 R2。
- 在概览页面复制你的 Account ID。那么 S3 API 端点 URL 就是
https://<account-id>.r2.cloudflarestorage.com。 - 点击 Manage R2 API Tokens → Create API Token,并复制 Access Key ID 和 Secret(Secret 只会显示一次)。
注意:R2 同时支持虚拟主机风格(
my-bucket.<account-id>.r2.cloudflarestorage.com)和路径风格(<account-id>.r2.cloudflarestorage.com/my-bucket)。任选其一;如果某个客户端出现异常,请在其设置中强制使用路径风格。
2)在 BucketMate 中连接 R2(Mac GUI)
BucketMate 是一款面向 macOS、iOS 和 Web 的原生 S3 GUI。使用你刚刚复制的同一个端点和密钥,可在 Mac 上、iPhone 的 iOS 应用中,或在浏览器里使用。
- 打开 BucketMate → Add connection → Cloudflare R2。
- 输入你的端点 URL、Access Key ID 和 Secret。区域为
auto。 - 点击 Test Connection 验证你的凭证。
- 验证通过后,点击 Connect 立即浏览你的存储桶。
需要进一步了解 GUI、控制台和 CLI 的区别?请阅读 macOS 上的 Cloudflare R2 GUI。在 macOS 最佳 S3 GUI 中比较各客户端。
请保留下面的 CLI 和 SDK 代码片段,用于脚本、CI 和批量传输。
3)AWS CLI:快速设置
使用一个全新的终端会话:
export AWS_ACCESS_KEY_ID="<your-access-key-id>"
export AWS_SECRET_ACCESS_KEY="<your-secret-access-key>"
export AWS_REGION="auto"
# Newer AWS CLI also supports service-specific env vars:
export AWS_ENDPOINT_URL_S3="https://<account-id>.r2.cloudflarestorage.com"
列出存储桶并复制一个对象:
aws s3 ls --endpoint-url "https://<account-id>.r2.cloudflarestorage.com"
aws s3 cp ./logo.png s3://my-r2-bucket/assets/logo.png \
--endpoint-url "https://<account-id>.r2.cloudflarestorage.com"
更喜欢使用配置文件?你可以继续配合 --profile r2 使用 --endpoint-url ...,或者在 shell 配置文件中设置 AWS_ENDPOINT_URL_S3,让 CLI 自动读取。
4)JavaScript(AWS SDK v3)
使用 @aws-sdk/client-s3 的最简上传:
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({
region: "auto",
endpoint: "https://<account-id>.r2.cloudflarestorage.com",
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
// If a client or library assumes AWS-only virtual hosting, un-comment:
// forcePathStyle: true,
});
await s3.send(new PutObjectCommand({
Bucket: "my-r2-bucket",
Key: "welcome.txt",
Body: "Hello from Cloudflare R2!",
ContentType: "text/plain",
}));
关键设置:
- R2 必须设置
region: "auto"。 - 使用上面所示的账户级端点。
- 只有当你的技术栈坚持使用虚拟主机式的存储桶子域名时,才切换
forcePathStyle。
5)Python(boto3)
import boto3
s3 = boto3.client(
"s3",
endpoint_url="https://<account-id>.r2.cloudflarestorage.com",
aws_access_key_id="<your-access-key-id>",
aws_secret_access_key="<your-secret-access-key>",
region_name="auto",
)
s3.upload_file("./logo.png", "my-r2-bucket", "assets/logo.png")
for obj in s3.list_objects_v2(Bucket="my-r2-bucket").get("Contents", []):
print(obj["Key"], obj["Size"])
endpoint_url 和 region_name="auto" 是仅有的 R2 专属设置。其他一切都是标准的 boto3 用法。
6)s5cmd 与 rclone(快速传输)
s5cmd
# Install: macOS → brew install peak/tap/s5cmd
AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... AWS_REGION=auto \
s5cmd --endpoint-url https://<account-id>.r2.cloudflarestorage.com \
cp ./big.iso s3://my-r2-bucket/
rclone(预设服务商)
# Guided setup
rclone config
# Choose: "Amazon S3 Compliant Storage Providers" → "Cloudflare R2 storage"
# Enter access key, secret, region=auto, endpoint=https://<account-id>.r2.cloudflarestorage.com
# Then:
rclone ls r2:my-r2-bucket
rclone copy ./folder r2:my-r2-bucket/folder
7)Cyberduck
- 打开 Cyberduck → Open Connection。
- 选择 Amazon S3 作为连接类型。
- 将 Server 设置为
<account-id>.r2.cloudflarestorage.com,Port 设置为443。 - 输入你的 R2 API 令牌中的 Access Key ID 和 Secret Access Key。
- 点击 Connect。如果列出失败,可能是令牌限定了特定存储桶,请将存储桶名称添加到路径中。
关于 Cyberduck 与原生 Mac 客户端的并排对比,请参阅 BucketMate 与 Cyberduck 对比。
相关指南
- macOS 上的 Cloudflare R2 GUI:像文件管理器一样浏览 R2
- 连接 Cloudflare R2 文档:BucketMate 连接的分步说明和常见错误
- Cloudflare R2 与 AWS S3 对比:成本、出站流量,以及何时同时使用两者
- macOS 最佳 S3 GUI:Cyberduck、Transmit、控制台、CLI、BucketMate
- 2026 年 Cloudflare R2 定价:存储、A/B 类操作、免费额度、零出站流量
故障排查
- AccessDenied:重新创建 API 令牌,并为你的存储桶授予 Edit 或适当的权限。
- 403/HeadBucket 问题:在你的客户端中设置路径风格寻址(例如在 SDK 中使用
forcePathStyle: true)。 - NoSuchBucket:存储桶名称必须遵循 S3 规则(小写字母、数字、连字符),请核对拼写。
- 签名不匹配:确保你的机器时钟正确,SigV4 对时间敏感。
- HTTP 被拒绝:所有端点都请使用
https://。