r2-upload
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseR2 Upload
R2 上传
Upload files to R2/S3-compatible storage and return a URL.
将文件上传至R2/S3兼容的存储服务并返回URL。
Use when
适用场景
- Upload images, documents, or other assets to object storage
- Generate public URLs for web/CDN use
- Generate presigned URLs for temporary access
- Provide upload helpers to other skills (like tech-news)
- 将图片、文档或其他资源上传至对象存储
- 生成用于网页/CDN的公开URL
- 生成用于临时访问的预签名URL
- 为其他技能(如tech-news)提供上传辅助工具
Prerequisites
前置条件
- Python 3.8+ available as
python3 - PyYAML ()
python3 -m pip install pyyaml - Config at (or set
~/.r2-upload.yml)R2_UPLOAD_CONFIG - Decide the bucket, object key/path, and visibility (public vs presigned)
- If you skip /
--key, the default is--key-prefixYYYY/MM/DD/<filename>
- 已安装Python 3.8+,且可通过调用
python3 - 已安装PyYAML(执行安装)
python3 -m pip install pyyaml - 配置文件位于(或设置环境变量
~/.r2-upload.yml指定路径)R2_UPLOAD_CONFIG - 确定存储桶、对象键/路径,以及可见性(公开或预签名)
- 如果省略/
--key参数,默认路径为--key-prefixYYYY/MM/DD/<filename>
Recommended workflow
推荐工作流程
- Confirm bucket/key and whether the URL should be public or presigned (avoid overwrites unless asked).
- Verify config and bucket exist.
- Upload with the CLI (recommended) or Python helper.
- Return the URL and key; note whether it is public or temporary and the expiration.
- 确认存储桶/键以及URL应为公开还是预签名(除非明确要求,否则避免覆盖现有文件)。
- 验证配置文件和存储桶是否存在。
- 使用CLI(推荐方式)或Python辅助工具执行上传。
- 返回URL和键;同时注明该URL是公开还是临时的,以及过期时间(如果是预签名URL)。
Quick commands
快速命令示例
bash
python3 scripts/r2-upload.py ./photo.jpg --public
python3 scripts/r2-upload.py ./photo.jpg --key images/YYYY/MM/DD/cover.jpg --public
python3 scripts/r2-upload.py ./report.pdf --key reports/YYYY/MM/DD/report.pdf
python3 scripts/r2-upload.py ./image.png --key-prefix images/YYYY/MM/DD --public
python3 scripts/r2-upload.py ./file.zip --expires 600bash
python3 scripts/r2-upload.py ./photo.jpg --public
python3 scripts/r2-upload.py ./photo.jpg --key images/YYYY/MM/DD/cover.jpg --public
python3 scripts/r2-upload.py ./report.pdf --key reports/YYYY/MM/DD/report.pdf
python3 scripts/r2-upload.py ./image.png --key-prefix images/YYYY/MM/DD --public
python3 scripts/r2-upload.py ./file.zip --expires 600Key options
主要参数选项
- : override the default bucket in config
--bucket <name> - : set the object key/path
--key <path> - : prepend prefix to the local filename
--key-prefix <prefix> - : return a public URL instead of a presigned URL
--public - : presigned URL expiration (1-604800)
--expires <seconds> - : use a custom config file
--config <path> - : network timeout
--timeout <seconds> - : override content type
--content-type <mime> - : set Cache-Control header
--cache-control <value> - : set Content-Disposition header
--content-disposition <value>
- :覆盖配置文件中的默认存储桶
--bucket <name> - :设置对象键/路径
--key <path> - :在本地文件名前添加前缀
--key-prefix <prefix> - :返回公开URL而非预签名URL
--public - :预签名URL的过期时间(取值范围1-604800秒)
--expires <seconds> - :使用自定义配置文件
--config <path> - :网络超时时间
--timeout <seconds> - :覆盖默认的内容类型
--content-type <mime> - :设置Cache-Control请求头
--cache-control <value> - :设置Content-Disposition请求头
--content-disposition <value>
Behavior notes
行为说明
- Default behavior returns a presigned URL (temporary access).
- If no key is provided, the default key is .
YYYY/MM/DD/<filename> - returns a public URL. For private buckets, use presigned URLs instead.
--public - Presigned URLs always use the storage endpoint (custom CDN domains are for public URLs).
- 默认行为是返回预签名URL(临时访问权限)。
- 如果未指定对象键,默认键为。
YYYY/MM/DD/<filename> - 参数会返回公开URL。对于私有存储桶,请使用预签名URL。
--public - 预签名URL始终使用存储服务的端点(自定义CDN域名仅适用于公开URL)。
Programmatic usage
程序化调用示例
python
import sys
from pathlib import Path
r2_dir = Path("/path/to/r2-upload") # update to your local path
sys.path.insert(0, str(r2_dir / "scripts"))
from upload import upload_file, batch_upload, fetch_and_upload
url = upload_file(
local_path="./image.jpg",
key="images/YYYY/MM/DD/image.jpg",
make_public=True
)python
import sys
from pathlib import Path
r2_dir = Path("/path/to/r2-upload") # 更新为你的本地路径
sys.path.insert(0, str(r2_dir / "scripts"))
from upload import upload_file, batch_upload, fetch_and_upload
url = upload_file(
local_path="./image.jpg",
key="images/YYYY/MM/DD/image.jpg",
make_public=True
)Scripts
脚本说明
- : CLI upload tool
scripts/r2-upload.py - : Python helpers (
scripts/upload.py,upload_file,batch_upload)fetch_and_upload
- :CLI上传工具
scripts/r2-upload.py - :Python辅助函数库(包含
scripts/upload.py、upload_file、batch_upload函数)fetch_and_upload
References
参考文档
- (provider config examples)
references/CONFIGURATION.md - (image workflow)
references/IMAGES.md - (common errors)
references/TROUBLESHOOTING.md
- (云服务商配置示例)
references/CONFIGURATION.md - (图片处理工作流程)
references/IMAGES.md - (常见错误排查)
references/TROUBLESHOOTING.md