Cloudflare R2 CLI
简介
Cloudflare R2 CLI
原始描述
Minimal Python CLI for secure upload, download, list, and delete operations on Cloudflare R2 storage using AWS Signature V4 authentication.
通过你的 Agent 添加
安装目录:cloudflare-r2-cli/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
技能正文
Cloudflare R2 CLI Skill
Skill Metadata
name: "cloudflare-r2-cli"
description: "A minimal CLI tool for interacting with Cloudflare R2 storage using Python. Supports upload, download, list, and delete operations via S3-compatible API with AWS Signature V4."
version: "1.0.0"
author:
username: "@zororaka00"
name: "Web3 Hungry"
x_account: "[https://x.com/web3hungry](https://x.com/web3hungry)"
category: "storage"
tags: ["cloudflare", "python", "r2", "storage"]
runtime:
language: "python"
version: "3.11+"
dependencies:
- "defusedxml>=0.7.1"
env:
- "CF_R2_ACCOUNT_ID"
- "CF_R2_ACCESS_KEY_ID"
- "CF_R2_SECRET_ACCESS_KEY"
- "CF_R2_BUCKET"
- "CF_R2_REGION"
Overview
This skill provides a minimal CLI tool for interacting with Cloudflare R2 storage using Python.
The script (r2.py) implements upload, download, list, and delete operations via the Cloudflare R2 S3-compatible API.
It is designed for secure usage in restricted environments with minimal dependencies while following modern security best practices.
Dependencies
- Python 3.11+
- Requires:
defusedxml (for secure XML parsing)
The tool primarily uses the Python standard library. Only defusedxml is required to harden XML parsing against known attacks.
Installation
defusedxml (for secure XML parsing)The tool primarily uses the Python standard library. Only defusedxml is required to harden XML parsing against known attacks.
No package installation is required for the CLI itself.
If defusedxml is not already available:
pip install defusedxml
Features
- Upload objects to a bucket
- Download objects from a bucket
- List objects in a bucket
- Delete objects from a bucket
- AWS Signature V4 compatible authentication (HMAC SHA256)
- Secure credential handling via environment variables
- Hardened HTTP client with HTTPS-only enforcement
- Safe XML parsing using defused XML protections
Prerequisites
- Python 3.11+
- Cloudflare account with R2 enabled
- Bucket created in Cloudflare R2
- API keys with appropriate permissions (least privilege)
- Bucket name must be DNS-compliant (lowercase letters, numbers, hyphens, 3–63 characters, no underscores)
Environment Variables
Variable
Description
Example
CF_R2_ACCOUNT_ID
Cloudflare account ID
123e4567-e89b-12d3-a456-426614174000
CF_R2_ACCESS_KEY_ID
R2 access key ID
AKIAxxxxxxxxxxxx
CF_R2_SECRET_ACCESS_KEY
R2 secret key
xxxxxxxxxxxxxxxxxxxxxxxx
CF_R2_BUCKET
Bucket name
my-r2-bucket
CF_R2_REGION
Region (default: auto)
auto
- Python 3.11+
- Cloudflare account with R2 enabled
- Bucket created in Cloudflare R2
- API keys with appropriate permissions (least privilege)
- Bucket name must be DNS-compliant (lowercase letters, numbers, hyphens, 3–63 characters, no underscores)
Environment Variables
Variable
Description
Example
CF_R2_ACCOUNT_ID
Cloudflare account ID
123e4567-e89b-12d3-a456-426614174000
CF_R2_ACCESS_KEY_ID
R2 access key ID
AKIAxxxxxxxxxxxx
CF_R2_SECRET_ACCESS_KEY
R2 secret key
xxxxxxxxxxxxxxxxxxxxxxxx
CF_R2_BUCKET
Bucket name
my-r2-bucket
CF_R2_REGION
Region (default: auto)
auto
| Variable | Description | Example |
|---|---|---|
CF_R2_ACCOUNT_ID |
Cloudflare account ID | 123e4567-e89b-12d3-a456-426614174000 |
CF_R2_ACCESS_KEY_ID |
R2 access key ID | AKIAxxxxxxxxxxxx |
CF_R2_SECRET_ACCESS_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 appropriate region for Cloudflare R2.- Custom value: you may specify a region string if required by your environment (for example
us-east-1for compatibility scenarios).
Environment Setup
This tool reads credentials directly from operating system environment variables. Set the variables globally before running the CLI.
Linux / macOS (bash / zsh)
export CF_R2_ACCOUNT_ID="your_account_id"
export CF_R2_ACCESS_KEY_ID="your_access_key"
export CF_R2_SECRET_ACCESS_KEY="your_secret_key"
export CF_R2_BUCKET="your_bucket_name"
export CF_R2_REGION="auto"
export CF_R2_ACCOUNT_ID="your_account_id"
export CF_R2_ACCESS_KEY_ID="your_access_key"
export CF_R2_SECRET_ACCESS_KEY="your_secret_key"
export CF_R2_BUCKET="your_bucket_name"
export CF_R2_REGION="auto"
To make them persistent across sessions, add the same lines to your shell profile:
~/.bashrc
# or
~/.zshrc
Then reload:
source ~/.bashrc
Windows (PowerShell)
setx CF_R2_ACCOUNT_ID "your_account_id"
setx CF_R2_ACCESS_KEY_ID "your_access_key"
setx CF_R2_SECRET_ACCESS_KEY "your_secret_key"
setx CF_R2_BUCKET "your_bucket_name"
setx CF_R2_REGION "auto"
setx CF_R2_ACCOUNT_ID "your_account_id"
setx CF_R2_ACCESS_KEY_ID "your_access_key"
setx CF_R2_SECRET_ACCESS_KEY "your_secret_key"
setx CF_R2_BUCKET "your_bucket_name"
setx CF_R2_REGION "auto"
Restart the terminal after setting variables.
Verify Environment
echo $CF_R2_ACCOUNT_ID
echo $CF_R2_ACCOUNT_ID
If a value prints, the environment is configured correctly.
For local development convenience only, you may use a
.envfile with a loader tool, but this is not recommended for production environments.
Usage
# Upload a file
python r2.py upload --file local_file.txt --key remote_file.txt
# Download a file
python r2.py download --key remote_file.txt --file local_file.txt
# List objects
python r2.py list
# Delete an object
python r2.py delete --key remote_file.txt
Security Best Practices
- Never commit credentials to version control.
- Use environment variables managed by your operating system, container runtime, or secret manager.
- Apply least-privilege access when creating API keys.
- Rotate keys regularly and revoke compromised keys.
- All credentials are loaded at runtime from environment variables.
- HTTPS is strictly enforced and hostnames are validated to prevent unexpected network access.
- XML responses are parsed using the defusedxml library to prevent XXE and entity expansion attacks.
- Errors are handled without exposing sensitive information such as credentials or secrets.
Credential Security
# Upload a file
python r2.py upload --file local_file.txt --key remote_file.txt
# Download a file
python r2.py download --key remote_file.txt --file local_file.txt
# List objects
python r2.py list
# Delete an object
python r2.py delete --key remote_file.txt
- Never commit credentials to version control.
- Use environment variables managed by your operating system, container runtime, or secret manager.
- Apply least-privilege access when creating API keys.
- Rotate keys regularly and revoke compromised keys.
- All credentials are loaded at runtime from environment variables.
- HTTPS is strictly enforced and hostnames are validated to prevent unexpected network access.
- XML responses are parsed using the defusedxml library to prevent XXE and entity expansion attacks.
- Errors are handled without exposing sensitive information such as credentials or secrets.
Credential Security
Never store credentials inside source code or configuration files committed to version control. Use environment variables provided by your operating system or a secure secret management solution.
Future Improvements
- Multipart uploads for large objects
- Retry with exponential backoff
- Progress indicators
- Bucket management commands
- Streaming upload/download to reduce memory usage
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。