Github图床同步到cloudflare R2
cloudflare R2提供了兼容 AWS S3协议的对象存储能力,可以通过 s3browser rclone awscli 等工具进行管理配置,当前除了一些老图片,新图片都放在了该处。出于数据备份和减少A/B类操作的目的,通常我会先把对应的图片上传至 github 进行编辑验证,再定期同步到 R2,同时更新对应文档里的图片信息。这里记录下通过github action自动同步的操作。
先登录 Cloudflare R2 界面,通过 API tokens 界面创建 token 信息,可以获取到对应的 AK/SK、 Endpoint信息,保存这些信息后面在github的配置界面会用到。
在 .github/workflows
目录下创建一个yaml文件,比如 main.yaml 内容如下:
1name: Upload to Cloudflare R2
2on:
3 push:
4 branches:
5 - main
6 workflow_dispatch:
7jobs:
8 upload:
9 runs-on: ubuntu-latest
10 steps:
11 - name: Checkout repository
12 uses: actions/checkout@v4
13 - name: Install AWS CLI
14 run: |
15 curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"
16 unzip awscli-bundle.zip
17 sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
18 - name: Configure AWS CLI for Cloudflare R2
19 run: |
20 aws configure set aws_access_key_id ${{ secrets.CLOUDFLARE_ACCESS_KEY_ID }}
21 aws configure set aws_secret_access_key ${{ secrets.CLOUDFLARE_SECRET_ACCESS_KEY }}
22 env:
23 CLOUDFLARE_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_ACCESS_KEY_ID }}
24 CLOUDFLARE_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_SECRET_ACCESS_KEY }}
25 - name: Upload to Cloudflare R2
26 run: |
27 echo ${{ secrets.R2_BUCKET_NAME }} is bucket name
28 # aws s3 sync . s3://${{ secrets.R2_BUCKET_NAME }}/ --endpoint-url=${{ secrets.R2_ENDPOINT_URL }} --exclude ".git/*" --exclude ".github/*" --exclude "awscli-bundle/*" --exclude "awscli-bundle.zip" --exact-timestamps --delete
29 aws s3 sync . s3://${{ secrets.R2_BUCKET_NAME }}/ --endpoint-url=${{ secrets.R2_ENDPOINT_URL }} --exclude ".git/*" --exclude ".github/*" --exclude "awscli-bundle/*" --exclude "awscli-bundle.zip" --exact-timestamps
30 env:
31 R2_BUCKET_NAME: ${{ secrets.R2_BUCKET_NAME }}
32 R2_ENDPOINT_URL: ${{ secrets.R2_ENDPOINT_URL }}
Settings — Secrets and variables — Actions 增加对应的变量信息,github action 可以通过 env 变量获取。
这样后面再在 github 上传就会自动往 cloudflare 同步。验证完成后,如果担心同步会消耗过多的A/B类操作,我们可以修改过yaml文件为基于 crontab 定时同步或者暂时关闭,在需要的时候再开启进行同步。
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/2024/08/github-sync-r2.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.