介绍
# Send Me My Files - R2 Upload with Short Lived Signed URLs
将文件上传到 Cloudflare R2 或任何兼容 S3 的存储,并生成预签名下载链接。
## 功能特性
- 上传文件到 R2/S3 存储桶 - 生成预签名下载 URL(可配置过期时间) - 支持任何兼容 S3 的存储(R2, AWS S3, MinIO 等) - 多存储桶配置 - 自动检测 content-type
## 配置
创建 `~/.r2-upload.yml`(或设置 `R2_UPLOAD_CONFIG` 环境变量):
```yaml # Default bucket (used when no bucket specified) default: my-bucket
# Bucket configurations buckets: my-bucket: endpoint: https://abc123.r2.cloudflarestorage.com access_key_id: your_access_key secret_access_key: your_secret_key bucket_name: my-bucket public_url: https://files.example.com # Optional: custom domain region: auto # For R2, use "auto" # Additional buckets personal: endpoint: https://xyz789.r2.cloudflarestorage.com access_key_id: ... secret_access_key: ... bucket_name: personal-files region: auto ```
### Cloudflare R2 设置
1. 进入 Cloudflare Dashboard → R2 2. 创建一个存储桶 3. 进入 R2 API Tokens: `https://dash.cloudflare.com/<ACCOUNT_ID>/r2/api-tokens` 4. 创建新的 API token - **重要:** 应用于特定存储桶(选择你的存储桶) - 权限:Object Read & Write 5. 复制 Access Key ID 和 Secret Access Key 6. 使用 endpoint 格式:`https://<account_id>.r2.cloudflarestorage.com` 7. 设置 `region: auto`
### AWS S3 设置
```yaml aws-bucket: endpoint: https://s3.us-east-1.amazonaws.com access_key_id: ... secret_access_key: ... bucket_name: my-aws-bucket region: us-east-1 ```
## 使用方法
### 上传文件
```bash r2-upload /path/to/file.pdf # Returns: https://files.example.com/abc123/file.pdf?signature=... ```
### 使用自定义路径上传
```bash r2-upload /path/to/file.pdf --key uploads/2026/file.pdf ```
### 上传到指定存储桶
```bash r2-upload /path/to/file.pdf --bucket personal ```
### 自定义过期时间(默认:5 分钟)
```bash r2-upload /path/to/file.pdf --expires 24h r2-upload /path/to/file.pdf --expires 1d r2-upload /path/to/file.pdf --expires 300 # seconds ```
### 公共 URL(无签名)
```bash r2-upload /path/to/file.pdf --public ```
## 工具
- `r2_upload` - 上传文件并获取预签名 URL - `r2_list` - 列出最近的上传 - `r2_delete` - 删除文件
## 环境变量
- `R2_UPLOAD_CONFIG` - 配置文件路径(默认:`~/.r2-upload.yml`) - `R2_DEFAULT_BUCKET` - 覆盖默认存储桶 - `R2_DEFAULT_EXPIRES` - 默认过期时间(秒)(默认:300 = 5 分钟)
## 注意事项
- 上传的文件以其原始文件名存储,除非指定了 `--key` - 自动添加 UUID 前缀以防止冲突(例如 `abc123/file.pdf`) - 根据文件扩展名自动检测 Content-Type - 预签名 URL 在配置的时长后过期