【白嫖】使用 CloudFlare R2 搭建个人图床

Last updated on March 19, 2024 pm

优点介绍

  1. 免费额度足够个人使用
  2. 无需额外配置 CDN
  3. 自定义域名比较优雅
Free Paid - Rates
存储 10 GB / month $0.015 / GB-month
A 类操作 1 million requests / month $4.50 / million requests
B 类操作 10 million requests / month $0.36 / million requests

第一步 创建 R2 存储桶

第二步 绑定域名

第三步 创建 S3 令牌


第四步 安装 lsky 图床

1
2
mkdir -p ~/app/Lsky && cd ~/app/Lsky && nano docker-compose.yml
docker-compose up -d
1
2
3
4
5
6
7
8
9
10
11
12
version: '3.3'
services:
lsky-pro:
restart: always
volumes:
- './lsky-pro-data:/var/www/html'
image: 'dko0/lsky-pro'

networks:
default:
external: true
name: ngpm

第五步 图床添加 R2 储存桶

附加 图床备份

1
2
sudo -v ; curl https://rclone.org/install.sh | sudo bash -s beta
nano ~/.config/rclone/rclone.conf
1
2
3
4
5
6
7
[img]
type = s3
provider = Cloudflare
access_key_id = xxx
secret_access_key = xxxxxx
endpoint = https://<accountid>.r2.cloudflarestorage.com
acl = private
1
2
3
4
5
rclone lsd img:limour-img
git clone https://oauth2:ghp_xxx@github.com/limour-blog/img-bed.git
rclone copy --ignore-existing --progress --ignore-errors img:limour-img ~/img-bed
cd ~/img-bed
git add . && git commit -m 'backup' && git push -u origin main

附加 图片预处理

1
2
3
4
5
6
D:\ImageMagick-7.1.1-Q16\magick.exe convert -resize 512x512^ -gravity North -extent 512x512 -quality 50 -define WebP:lossless=false F:\temp\randImg\*.jpg 26.webp
# 顶部 -gravity North
# 底部 -gravity South
# 中间 -gravity center
# 右侧 -gravity East
# 左侧 -gravity West

附加 旧图片归档

1
2
3
4
5
6
7
8
9
10
mkdir ~/img-bed
apt install imagemagick
convert -version
# 6.9.11
# 将 ~/app/Lsky/lsky-pro-data/storage/app/uploads 目录及其所有的子目录的 png 后缀的图片转换成 webp
# 在保持目录结构不变的前提下,保存到 ~/img-bed 目录下
# 非 png 的文件则直接复制
find ~/app/Lsky/lsky-pro-data/storage/app/uploads -type f -name "*.png" -exec sh -c 'mkdir -p ~/img-bed/$(dirname {}); convert {} -quality 30 -define webp:lossless=false ~/img-bed/$(dirname {})/$(basename {} .png).webp' \;
find ~/app/Lsky/lsky-pro-data/storage/app/uploads -type f ! -name "*.png" -exec sh -c 'mkdir -p ~/img-bed/$(dirname {}); cp {} ~/img-bed/$(dirname {})/$(basename {})' \;
mv ~/img-bed/root/app/Lsky/lsky-pro-data/storage/app/* ~/img-bed & rm -rf ~/img-bed/root & mv ~/img-bed/uploads ~/img-bed/archives_2023
1
2
rclone lsd img:limour-img
rclone copy --ignore-existing --progress --ignore-errors ~/img-bed img:limour-img
  • 修改原文章图片地址 _posts 目录
1
2
3
sed -i 's|https://img-cdn.limour.top/i/|https://img.limour.top/archives_2023/|g' *.md
sed -i 's|https://img-cdn.limour.top/|https://img.limour.top/archives_2023/|g' *.md
sed -i 's#https://img.limour.top/archives_2023/\(.*\)\.png#https://img.limour.top/archives_2023/\1.webp#g' *.md

【白嫖】使用 CloudFlare R2 搭建个人图床
https://hexo.limour.top/-bai-piao--shi-yong-CloudFlare-R2-da-jian-ge-ren-tu-chuang
Author
Limour
Posted on
August 27, 2023
Updated on
March 19, 2024
Licensed under