上次在 玩转云服务(7):自建随机图片 API 做了一个基于服务器的随机图片 API。然而,有几点让它不能成为完美的方案:
- 服务器和域名需要自备,自备需要花钱(虽然有免费的羊毛,但是分分钟跑路)
- API 托管在自己的服务器上,就需要自己管理服务器,这对于不想在运维上花心思的人来说不友好
经过今天的研究,发现了下面这一套几乎完美的 Cloudflare 工具链:
- Cloudflare R2 对象存储作为图床
- Cloudflare D1 作为数据库
- CLoudflare Workers 部署 API 代码
- 以上都有免费域名分配,不需要自己买
这套方案有如下优点:
- 完 全 免 费!虽然是有免费额度的(无限免费你在想 peach),但是个人甚至一大坨人一起用很难触及上限
- 三个都是无服务器(Serverless)服务,开箱即用,不需要兼任运维
- 部署和配置更简单
警告
由于 .workers.dev
域名在境内处于无法访问的状态,因此若想在境内使用 API 仍然需要购买自己的域名。一毛不拔的“铁公鸡”请参见:玩转云服务(10):使用 Cloudflare Functions + D1 + R2 搭建免费的随机图片 API
免费额度
截止至 2023 年 9 月 25 日,每个账户的免费额度为
R2:10 GB存储,每月(30 天计) 1 百万次 A 类请求(上传文件,列出所有文件等),1 千万次 B 类请求(获取文件等)。官方文档
Workers:每天 10 万次请求。官方文档
D1:每天 5 百万行读,10 万行写,1 GB 存储。 官方文档(D1 仍处于公开测试阶段,正式发布后定价可能有更改)
此外,Cloudflare KV 键值对存储的免费额度为每天 10 万次读请求,1 千次写,1 千次删除,1 千次列出和 1GB 存储。KV 曾被我考虑用来存储图片信息,不过因为免费写的额度太少,加上 D1 作为关系型数据库更加符合我的要求,因此本项目没有采用 KV。
架构图

配置 R2 对象存储
按照 玩转云服务(8):使用 Cloudflare R2 对象存储 配置 R2。需要将 CORS 配置为允许全部源,以便于 Sakurairo 预载封面图。
有多少图集就开多少个 R2 桶。并且往其中分别上传你的图片。比如我开了以下桶:

配置 D1 数据库
建立数据库
在网页端建立一个数据库,用来存放图片的信息和在 R2 上的 URL。名字随意,比如我的是:pictures-cdn-db

添加数据
首先打开在线工具:随机图片 API:SQL 生成
然后,对于每一个图集(也就是每一个 R2 桶里的图片的本地那份),把他们拖到框框里;打开“删除并重建表格”;填写被插入的表名(这个随意,但是最好和图集的英文名相同,后续 API 会用到),填这个图集对应的 R2 桶的域名,然后生成并下载 SQL。

复制下载好的 SQL,粘贴到 D1 数据库的控制台执行成功即可。

对你的所有图集(建立的 R2 桶)都做上述导入 SQL 的操作。我的如下:

随便点进去一个看是这样的:

配置 Workers API
配置 Pro 版本 API
Pro 版本可以使用 SQL 进行更加强大的查询。
首先新建一个 Worker。


名字随便取,比如我的是 random-picture-api-pro
(因为我已经有了一个,所以这个名字不能用了)然后创建,点击下面的 配置 Workers

点击编辑变量(我这里已经设置过了,你这里应该是空的):

点击“添加变量”来添加变量。

对于 Pro 版本,有以下环境变量:
DEFAULT_TABLES
(必需):如果用户提供的 URL 没有指定任何表(比如就只是直接的https://rpicpro-api.eterance.com
),默认随机 D1 数据库里的什么表(图集),多个图集用英文逗号隔开。比如我设置为默认pixiv, imas
两张表。ENABLE_COUNT
(可选):用户是否可以使用count
参数查询 SQL 语句所选择的随机图片范围的图片数量,比如https://rpicpro-api.eterance.com?count&pixiv
。设置为true
就是允许;false
和不设置就是不允许。ENABLE_DEBUG
(可选):用户是否可以使用debug
参数查询自己构建的 SQL 语句最终长什么样,比如https://rpicpro-api.eterance.com?debug&pixiv
。设置为true
就是允许;false
和不设置就是不允许。
按需设置保存。
然后滑动到下面,给 Worker 绑定 D1 数据库:
名称是 PIC_DB
,数据库选之前建的,保存。

然后滚回顶部,点击编辑:

在左边代码编辑器里删除所有原有的代码,然后将rpicpro.js的内容复制粘贴,点击右上的保存并部署。
这时,你的 Pro 版 API 就完成了。
也可以绑上自己的域名:


配置普通版本 API
步骤和上面类似,比如说我的名字是 random-picture-api
。
环境变量选:
DEFAULT_TABLE
(必需):如果用户提供的 URL 没有指定任何表(比如就只是直接的https://rpic-api.eterance.com
),默认随机 D1 数据库里的什么表(图集)。比如我设置为默认pixiv
表。

代码使用rpic.js里的。保存并部署即可。
如何使用以及文档网站
我不打算在这篇博客中花费大量笔墨解释这个 API 如何使用。请参见这个 API 文档网站:
https://eterance.github.io/docusaurus-api-docs/
是的,我连 API 文档都帮你写好了,而且你还能把它搞成你自己的。
源码地址:https://github.com/Eterance/docusaurus-api-docs
如何部署自己的文档网站见:玩转云服务(6):使用 Docusaurus 创建项目文档
Comments 6 条评论
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
没反应
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
@123 你这什么问题都没说啊
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
api的使用能把后面那个/pics/rpic简化吗
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
@1834336693 可以啊,把图片目录放到网址根目录,然后改一下代码就行了,我偷懒把url写死了
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
大佬的网站加载好快啊,用的是阿里云的服务器?带宽是多少呢,图片是cf的这个r2存储吗
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 367
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
Warning: Trying to access array offset on value of type null in /www/wwwroot/wordpress/wp-content/themes/Sakurairo-main/functions.php on line 368
对,图片是r2,网站是阿里云,主机放到大陆解析和加载比在海外快,另外我还做了静态处理和cdn