Picbed Migration: from Aliyun OSS to cloudflare R2

导言

Aliyun charge me 10RMB per month, and the fee grows 2RMB per month. So i decide to move to cloudflare R2 which not charge the egress bandwidth.

STEP1: Batch download my Pic-set From Aliyun OSS using ossutil64

??? failure annotate “Click Download button trigers stupid individual web download”

Goto the [bucket page](https://oss.console.aliyun.com/bucket/oss-cn-hangzhou/shaojiemike/object?path=img%2F) and download all pictures(1) in `img` directory.
  1. 1000 files about 350MB

??? example annotate “ossutil64”

  1. 根据windows系统版本[下载ossutil安装包](https://www.alibabacloud.com/help/zh/oss/developer-reference/install-ossutil#4c30f1a48ce9y),解压使用,双击`ossutil.bat`跳出命令行窗口
  2. config access authority(1) `ossutil64.exe config -e oss-cn-hangzhou.aliyuncs.com -i <accessKeyId> -k <accessKeySecret>`
  3. 本地批量上传至OSS:`ossutil64.exe cp <本地目录> oss://<bucketName>[/<上传路径>/] -r`
  4. OSS批量下载至本地:[^1] `ossutil64.exe cp oss://<bucketName>/[<上传路径>/] <本地目录> -r`,e.g, `ossutil64.exe cp oss://shaojiemike/img . -r`
  1. 创建RAM用户的AccessKey and oss-cn-hangzhou must be the location of bucket

STEP2: Setup R2

1. support 银联 and fill CVC(1) and 有效期(2)[^2]。Need to recharge 1 USD(US dollar) 1. 但是我没有自己的信用卡,申请了中行的一个校园数字信用卡(3), 中国银行APP申请然后APP激活,完全不需要去线下网点。 2. Click `Create a bucket` and named it `shaojiemike` and select loaction `APAC` 3. CLick `Settings` and `connect domain` to add `pic.shaojiemike.top` 4. Click `active access` in `R2.dev subdomain`
  1. The Card Verification Code, or CVC*, is an extra code printed on your debit or credit card. With most cards (Visa, MasterCard, bank cards, etc.) it is the final three digits of the number printed on the signature strip on the reverse of your card
  2. 信用卡有效期是指从信用卡发卡时间至停止使用的时间,有效期涉及用卡安全请勿随意透露,目前仅能通过信用卡正面的卡号下方查看。 有效期的格式是MM/YY,在信用卡正面下方四位数,前两位代表月份,后两位代表年份,如04/21,代表有效期到2021年4月30日。
  3. 数字信用卡没有实体卡,不用担心“薄薄”的一张卡片丢失被盗刷或者忘记密码,数字信用卡进一步提高了用户在消费使用时的安全性和便捷性。

STEP3: upload your Pic-Set to R2

您最多可以从仪表板上传 100 个文件。请使用 Wrangler CLI 或 API 上传更多文件。

test upload using Wrangler

  1. install sudo npm install wrangler --save-dev -g[^3]
  2. config authority, when you first time using wrangler will triger a url web forward to authority. Maybe you need ssh -L 8976:127.0.0.1:8976 -vN -f -l shaojiemike snode6.acsalab.com to forword the response.
    1
    2
    3
    4
    5
    # shaojiemike @ snode6 in ~/Download [18:04:34] C:1
    $ wrangler kv:namespace list
    Attempting to login via OAuth...
    Opening a link in your default browser: https://dash.cloudflare.com/oauth2/auth?response_type=xxx
    Successfully logged in.
  3. and test upload one file
    1
    2
    3
    4
    5
    6
    # shaojiemike @ snode6 in ~/Download [18:14:16] C:1
    $ wrangler r2 object put shaojiemike/img/test.png --file=./img/1212111.png
    ⛅️ wrangler 3.15.0
    -------------------
    Creating object "img/test.png" in bucket "shaojiemike".
    Upload complete.

batch upload using Wrangler

you can use the script to upload files in directory one by one, and check the upload.log for failed ones

title
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash

# Set the directory where the images are located
img_dir="img"
bucket="shaojiemike"

# Loop through each file in the "img" directory
for file in "$img_dir"/*; do
if [ -f "$file" ]; then
# Extract the filename (without the path)
filename=$(basename "$file")

# Build and execute the wrangler command
wrangler_command="wrangler r2 object put $bucket/$img_dir/$filename --file=$file"
echo "Uploading $filename... in $file"
$wrangler_command

# Check the exit status of the wrangler command
if [ $? -eq 0 ]; then
echo "Uploaded $filename successfully" |tee -a upload.log
else
echo "Failed to upload $filename" |tee -a upload.log
fi
fi
done

And you can access pic through url like pic.shaojiemike.top/img/20220705171352.png

STEP4: Change the picture url in your markdown

Just global replace shaojiemike.oss-cn-hangzhou.aliyuncs.com/img to pic.shaojiemike.top/img

STEP5: Setting picgo

  1. R2 Clike Manage R2 API Tokens[^2]
  2. Picgo install S3 plug-in and setting like[^2]{ align=right }

需要进一步的研究学习

Vercel

参考文献

[^1]: ossutils download

[^2]: 通用技术 白嫖 CloudFlare R2 搭建个人图床

[^3]: cloudflare Upload objects

Author

Shaojie Tan

Posted on

2023-10-31

Updated on

2025-01-30

Licensed under