白嫖worker 自建短链服务 Url-Shorten-Worker 变身文件保管站 File Storage 保存到 R2对象存储 搭建手册


变身文件保管站 File Storage 保存到 R2对象存储 

先上演示视频


申请Cloudflare账号,略。

* 以下操作过程中, 标颜色的信息, 你最好另外单独记好. 这样操作流程比较顺畅.


创建R2


记住这个R2的名字,以 r2test 为例

设置公开链接


记住这个R2公开链接, https://pub-*****.r2.dev

设置 CORS 策略

[
  {
    "AllowedOrigins": [
      "*"
    ],
    "AllowedMethods": [
      "PUT",
      "GET",
      "HEAD",
      "DELETE"
    ],
    "AllowedHeaders": [
      "*"
    ]
  }
]
* 注, 说实话这里有一点点粗暴, 但是问题不大. 

创建R2 API

回到R2 overview页面, 记住这个 Account ID

点击 Manage 按钮

点击 "Create User API token" 按钮

选择 "Object Read & Write" - "Specify bucket(s)" - 选择你刚刚创建的 R2  r2test 

记住生成的 Access Key ID,   Secret Access Key

创建KV

记住这个KV的名字,以 kvtest 为例


创建Worker服务




设置绑定KV




变量名称必须设置为 LINKS, KV的名字选刚刚创建的 kvtest

* LINKS 是在脚本中要引用的,所以要设置这个。

编辑Worker的脚本内容


把原有的内容全部删掉


换成:https://github.com/crazypeace/Url-Shorten-Worker/blob/main/worker.js 的内容

变身文件保管站 lite

修改:
  password: "suibianshezhishenme",  // 你随便想设什么密码
  theme: "theme/file-r2-lite",  // 变身文件保管站 lite


设置5个变量:

R2_ACCOUNT_ID        = "Account ID" // - R2 账户 ID (明文变量)
R2_ACCESS_KEY_ID     = "Access Key ID" // - S3 API Access Key ID (建议设为加密 Secret)
R2_SECRET_ACCESS_KEY = "Secret Access Key" // - S3 API Secret Access Key (建议设为加密 Secret)
R2_BUCKET_NAME       = "r2test" // - R2 存储桶名称 (明文变量)
R2_PUBLIC_URL        = "https://pub-*****.r2.dev" // - R2 公开访问 URL, 如 https://pub-xxxx.r2.dev (明文变量)
 

保存并部署

右上角

开始使用 文件保管站 lite

用浏览器访问你的 worker域名  或 点击 visit 链接

显示 404 这是正常的功能,  设置了密码不对外公开使用.

在 worker 域名后 添加 密码作为 path , 如  
https://divine-voice-e752.crazypeace.workers.dev/suibianshezhishenme
这时界面就正常了

具体操作可以看演示视频

* 考虑到文件保管站存储的文件有可能相当大, 所以没有默认显示图片, 需要点预览按钮看图片.

变身文件保管站 (完全体)

考虑到R2中的文件有可能体积大, 而我们在管理面板只希望显示小尺寸的缩略图.
那么我们可以白嫖 cloudflare 的 Image Transformation 功能.

首先, 你需要有一个托管在cloudflare的域名.

打开 Image Transformation 功能


给 R2 设置自定义域名

还记得这个页面吗?

输入一个你的域名的子域名. 如, r2test.xxxx.xxxx
过一会儿, 会变成这个样子, 说明生效了.

修改 worker 脚本

修改 R2_PUBLIC_URL 的值

R2_PUBLIC_URL = "https://r2test.xxxx.xxxx"

修改
theme: "theme/file-r2",  // 文件保管站 (完全体)

 

保存并部署

右上角

开始使用 文件保管站 (完全体)

用浏览器访问你的 worker域名/你的密码, 如  
https://divine-voice-e752.crazypeace.workers.dev/suibianshezhishenme
你会发现自动显示了图片缩略图

======

后记

点击一次 "上传R2" 按钮, 再点击一次 "保存KV" 按钮, 确实太啰嗦.

但, 考虑到 Url-Shorten-Worker 整个系列的"传承", 我没有做进一步的优化.

未来我会新开一个 repo, 实现进一步的优化和增强开发.


======

配置参数的作用

const config = {
  password: "", // 管理面板使用密码 
  result_page: false, // 是否用特定的result页面来显示value 
  theme: "", // 管理面板的主题 
  cors: true, // 是否允许CORS使用API 
  unique_link: false, // 一个长链是否只有唯一的短链(会增加写入的使用量)
  custom_link: true, // 允许自定义短链
  overwrite_kv: false, // 允许覆盖已存在的key
  snapchat_mode: false, // 短链只能访问一次(访问后就删除了)
  visit_count: true, // 使用记数(会大大增加写入的使用量, 多人共用不推荐打开)
  load_kv: false, // 从KV加载全部数据(自用推荐打开, 多人共用会看到别人的数据)
  system_type: "shorturl", // 系统的功能定义 // shorturl, imghost, other types {pastebin, journal}
}

======

如果要当网络记事本 PasteBin

演示站 https://pastebin.crazypeace.workers.dev/tieludasiliqiuweiyue

演示视频 https://www.youtube.com/watch?v=toBeb3DNpZc


======

如果要当图床 Image Hosting

演示站 https://imghost.crazypeace.workers.dev/imghostimghost

演示视频 https://www.youtube.com/watch?v=B7wH4tB1-O8



======

如果要当网络日记本, 支持MarkDown

演示站 https://journal.crazypeace.workers.dev/journaljournal

演示视频 https://www.youtube.com/watch?v=DZcXqyIm_R0

https://www.youtube.com/watch?v=Ra6SXZFeGM8



======

如果要做一次性二维码

演示站 https://snapchat.crazypeace.workers.dev/huiyingebielianaiputi


======

开发记录

直接访问域名返回404。在KV中设置一个entry,保存秘密path,只有访问这个path才显示使用页面。

https://zelikk.blogspot.com/2022/07/url-shorten-worker-hide-tutorial.html

支持自定义短链

https://zelikk.blogspot.com/2022/07/url-shorten-worker-custom.html

API 不公开服务

https://zelikk.blogspot.com/2022/07/url-shorten-worker-api-password.html

页面缓存设置过的短链

https://zelikk.blogspot.com/2022/08/url-shorten-worker-localstorage.html

长链接文本框预搜索localStorage

https://zelikk.blogspot.com/2022/08/url-shorten-worker-bootstrap-list-group-oninput.html

增加删除某条短链的按钮

https://zelikk.blogspot.com/2022/08/url-shorten-worker-delete-kv-localstorage.html

访问计数功能 可查询短链 成为功能完整的短链API系统

https://zelikk.blogspot.com/2023/11/url-shorten-worker-visit-count-api-api.html

阅后即焚功能, 可制作一次性二维码

https://zelikk.blogspot.com/2023/11/url-shorten-worker-snapchat-mode.html

增加读取 KV 中全部记录的功能

https://zelikk.blogspot.com/2024/01/url-shorten-worker-load-cloudflare-kv.html

变身网络记事本 Pastebin

https://zelikk.blogspot.com/2024/01/url-shorten-worker-pastebin.html

变身图床 Image Hosting

https://zelikk.blogspot.com/2024/01/url-shorten-worker-image-hosting-base64.html

变身日记本 NetJournal 支持Markdown

https://zelikk.blogspot.com/2024/02/url-shorten-worker-netjournal.html

https://zelikk.blogspot.com/2024/02/url-shorten-worker-netjournal-markdown.html

https://zelikk.blogspot.com/2024/04/url-shorten-worker-netjournal-markdown.html


评论

The Hot3 in Last 7 Days

白嫖worker 自建短链服务 Url-Shorten-Worker 显示短链的二维码QRcode

酒馆SillyTavern 玩英文角色卡 也能以中文输出 设置世界书Lorebooks