Cloudflare R2 CLI — Cloudflare R2 命令行工具
v1.0.6Minimal Python 命令行工具 for 安全 上传, 下载, 列出, and 删除 operations on Cloudflare R2 storage using AWS 签名ature V4 authentication.
运行时依赖
安装命令
点击复制技能文档
Cloudflare R2 命令行工具 技能 技能 Metadata name: "r2-命令行工具" description: "A minimal 命令行工具 工具 for interacting with Cloudflare R2 storage using Python. Supports 上传, 下载, 列出, and 删除 operations via S3-compatible API with AWS 签名ature V4." version: "1.0.6" author: username: "@zororaka00" name: "网页3 Hungry" x_account: "https://x.com/网页3hungry" category: "storage" tags: ["cloudflare", "python", "r2", "storage"] source: "r2.py" 凭证s: required: true type: "环境" variables: - CF_R2_ACCOUNT_ID - CF_R2_访问_KEY_ID - CF_R2_SECRET_访问_KEY - CF_R2_BUCKET - CF_R2_REGION 运行time: language: "python" version: "3.11+" dependencies: - "defusedxml>=0.7.1" env: - name: "CF_R2_ACCOUNT_ID" required: true description: "Cloudflare account ID" - name: "CF_R2_访问_KEY_ID" required: true description: "R2 访问 key ID" - name: "CF_R2_SECRET_访问_KEY" required: true description: "R2 secret 访问 key" - name: "CF_R2_BUCKET" required: true description: "R2 bucket name" - name: "CF_R2_REGION" required: false description: "Region (default: auto)"
required_env_vars: - CF_R2_ACCOUNT_ID - CF_R2_访问_KEY_ID - CF_R2_SECRET_访问_KEY - CF_R2_BUCKET - CF_R2_REGION primaryEnv: CF_R2_访问_KEY_ID primarySecretEnv: CF_R2_SECRET_访问_KEY
Overview
This 技能 provides a minimal 命令行工具 工具 for interacting with Cloudflare R2 storage using Python. The script (r2.py) implements 上传, 下载, 列出, and 删除 operations via the Cloudflare R2 S3-compatible API.
It is de签名ed for 安全 usage in restricted 环境s with minimal dependencies while following modern security best practices.
Dependencies Python 3.11+ Requires: defusedxml (for 安全 XML parsing)
The 工具 primarily uses the Python standard 库. Only defusedxml is required to harden XML parsing agAInst known attacks.
安装ation
No package 安装ation is required for the 命令行工具 itself.
If defusedxml is not already avAIlable:
pip 安装 defusedxml
Features 上传 objects to a bucket 下载 objects from a bucket 列出 objects in a bucket 删除 objects from a bucket AWS 签名ature V4 compatible authentication (HMAC SHA256) 安全 凭证 handling via 环境 variables Hardened HTTP 命令行工具ent with HTTPS-only enforcement Safe XML parsing using defused XML 保护ions Prerequisites Python 3.11+ Cloudflare account with R2 enabled Bucket 创建d in Cloudflare R2 API keys with 应用ropriate 权限s (least privilege) Bucket name must be DNS-compliant (lowercase letters, numbers, hyphens, 3–63 characters, no underscores) 环境 Variables Variable Description Example CF_R2_ACCOUNT_ID Cloudflare account ID 123e4567-e89b-12d3-a456-426614174000 CF_R2_访问_KEY_ID R2 访问 key ID AKIAxxxxxxxxxxxx CF_R2_SECRET_访问_KEY R2 secret key xxxxxxxxxxxxxxxxxxxxxxxx CF_R2_BUCKET Bucket name my-r2-bucket CF_R2_REGION Region (default: auto) auto
CF_R2_REGION behavior:
auto (default): automatically selects the 应用ropriate region for Cloudflare R2. Custom value: you may specify a region string if required by your 环境 (for example us-east-1 for compatibility scenarios). 环境 设置up
This 工具 reads 凭证s directly from operating 系统 环境 variables. 设置 the variables globally before 运行ning the 命令行工具.
Linux / macOS (bash / zsh) 导出 CF_R2_ACCOUNT_ID="your_account_id" 导出 CF_R2_访问_KEY_ID="your_访问_key" 导出 CF_R2_SECRET_访问_KEY="your_secret_key" 导出 CF_R2_BUCKET="your_bucket_name" 导出 CF_R2_REGION="auto"
Security Note: These 凭证s should only be 设置 temporarily in the current 会话. For production 环境s, use 安全 secret management solutions.
Windows (PowerShell) $env:CF_R2_ACCOUNT_ID="your_account_id" $env:CF_R2_访问_KEY_ID="your_访问_key" $env:CF_R2_SECRET_访问_KEY="your_secret_key" $env:CF_R2_BUCKET="your_bucket_name" $env:CF_R2_REGION="auto"
Security Note: These 凭证s should only be 设置 temporarily in the current 会话. For production 环境s, use 安全 secret management solutions.
Recommended 安全 凭证 Management
For production 环境s, consider using:
Cloudflare Workers Secrets (for Cloudflare 环境s) AWS Secrets 管理器 (if using AWS) 哈希iCorp Vault (enterprise secret management) Docker Secrets (for contAInerized 部署ments) Kubernetes Secrets (for Kubernetes 环境s) OS-level keyring (e.g., keyring Python package) 验证 环境 echo $CF_R2_ACCOUNT_ID
If a value prints, the 环境 is 配置d correctly.
导入ant: Never commit 凭证s to version control. Use 环境 variables provided by your operating 系统 or a 安全 secret management solution.
Usage # 上传 a file python r2.py 上传 --file local_file.txt --key remote_file.txt
# 下载 a file python r2.py 下载 --key remote_file.txt --file local_file.txt
# 列出 objects python r2.py 列出
# 删除 an object py