这套目录只迁移公开随机图访问能力,不替代现有 Express 后台。后台上传、图库管理、Docker 部署仍按项目根目录的说明运行;Cloudflare 侧负责读取 R2 中的图片和 manifest.json,对外提供随机图 API。
| 文件 | 说明 |
|---|---|
worker.js |
Cloudflare Worker 入口 |
wrangler.toml |
Wrangler 示例配置,不包含任何密钥 |
scripts/generate-manifest.js |
从现有 public/images 目录生成 R2 索引 |
manifest.example.json |
manifest 结构示例 |
默认保持 /image 子路径:
| 路径 | 说明 |
|---|---|
GET /image/api/random |
随机图片,默认 302 跳转 |
GET /image/api/random?gallery=anime&device=pc&type=json |
指定图库、设备类型并返回 JSON |
GET /image/api/random?type=pc |
兼容旧主题写法,按 pc 设备随机并跳转 |
GET /image/api/:gallery |
图库快捷随机接口 |
GET /image/api/galleries |
图库统计列表 |
GET /image/api/list?gallery=anime&device=mobile&limit=100 |
图片列表 |
GET /image/api/stats |
全局统计 |
GET /image/images/{gallery}/{device}/{filename} |
从 R2 读取图片文件 |
GET /image/health |
Worker 健康检查 |
参数语义与 Express 版本保持一致:device 支持 pc、mobile、all,type 支持 redirect、json、image,并兼容 type=pc、type=mobile。
建议把 R2 bucket 整理成下面这样:
manifest.json
images/
anime/
pc/
001.webp
mobile/
001.webp
manifest.json 是 Worker 每次随机选择时读取的索引。不要让 Worker 每次请求扫描 R2;图片增删后重新生成并上传 manifest 即可。
可以在 Cloudflare 控制台创建,也可以用 Wrangler:
npx wrangler r2 bucket create nyaovo-random-imagesbucket 名可以自定义。创建后修改 cloudflare/wrangler.toml 里的:
[[r2_buckets]]
binding = "IMAGE_BUCKET"
bucket_name = "你的 bucket 名"binding 建议保持 IMAGE_BUCKET,Worker 代码会用这个名字读取 R2。
在项目根目录运行:
npm run cloudflare:manifest等价于:
node cloudflare/scripts/generate-manifest.js --root public/images --output cloudflare/manifest.json --r2-prefix images常用参数:
| 参数 | 默认值 | 说明 |
|---|---|---|
--root |
public/images |
本地图片根目录 |
--output |
cloudflare/manifest.json |
输出文件 |
--r2-prefix |
images |
R2 中图片对象 key 前缀 |
--base-path |
/image |
Worker 对外 API 子路径 |
脚本会读取 public/images/{gallery}/pc 和 public/images/{gallery}/mobile,并尽量写入图片尺寸、大小、图库统计和 R2 key。
先把图片上传到 R2,保持 key 与 manifest 中的 key 一致,例如:
npx wrangler r2 object put nyaovo-random-images/images/anime/pc/001.webp --file public/images/anime/pc/001.webp --content-type image/webp --cache-control "public, max-age=31536000, immutable"图片较多时,建议使用 Cloudflare 控制台、Wrangler 脚本、rclone 或任意兼容 S3 的工具批量上传。关键是保持路径为:
images/{gallery}/pc/{filename}
images/{gallery}/mobile/{filename}
然后上传 manifest:
npx wrangler r2 object put nyaovo-random-images/manifest.json --file cloudflare/manifest.json --content-type application/json --cache-control "public, max-age=60"cloudflare/wrangler.toml 已包含基础配置:
[vars]
BASE_PATH = "/image"
MANIFEST_KEY = "manifest.json"
IMAGE_KEY_PREFIX = "images"
MANIFEST_CACHE_SECONDS = "60"
STATIC_CACHE_SECONDS = "31536000"
# PUBLIC_BASE_URL = "https://img.example.com"变量说明:
| 变量 | 说明 |
|---|---|
BASE_PATH |
对外挂载路径,默认 /image |
MANIFEST_KEY |
R2 中 manifest 对象 key |
IMAGE_KEY_PREFIX |
R2 图片 key 前缀,需要与生成 manifest 时一致 |
MANIFEST_CACHE_SECONDS |
Worker 内存中缓存 manifest 的秒数 |
STATIC_CACHE_SECONDS |
/image/images/... 图片响应缓存时间 |
PUBLIC_BASE_URL |
JSON 和 redirect 返回 URL 的域名;不填则使用当前请求域名 |
不要把 Cloudflare API Token、账号 ID 或其他密钥写进仓库。需要登录 Wrangler 时使用:
npx wrangler login进入 cloudflare 目录:
cd cloudflare
npx wrangler dev部署:
cd cloudflare
npx wrangler deploy部署后可以访问:
https://你的域名/image/api/random
https://你的域名/image/api/random?gallery=anime&device=mobile&type=json
https://你的域名/image/api/galleries
- 继续用现有 Express 后台管理本地图片,或直接整理本地
public/images。 - 把新增、删除或移动后的图片同步到 R2。
- 重新运行
npm run cloudflare:manifest。 - 上传新的
manifest.json。 - 等待
MANIFEST_CACHE_SECONDS过期,或临时把它调小后重新部署。
Manifest not found in R2:检查 MANIFEST_KEY 是否等于上传的对象 key,例如 manifest.json。
图片文件不存在于 R2:manifest 中有这张图,但 R2 对应 key 没上传,或者 IMAGE_KEY_PREFIX 与生成时的 --r2-prefix 不一致。
非法图库名:图库名需要保持小写字母、数字、短横线或下划线。
随机结果看起来短时间内重复:这是随机选择的正常现象。API 响应默认 no-store,图片文件本身长缓存,不会把随机入口缓存死。