Sign up Log in
n/wulin · 1mo ago · Edited · 846 views
u/zhzywSilver
n/wulinu/zhzyw · 1mo ago

Cloudflare R2 + TG image hosting tutorial: full-stack deploy and config in 10 minutes

I've got my own site, but paid image hosting felt a bit pricey, so I tried rolling my own. Messed around with a bunch of stuff online and none of it felt great, so I hacked together a Cloudflare R2 + TG dual-active (backup) setup with AI, and it turned out decent enough to share.

Grab it and tinker with it yourself if you need it.

Baidu link: https://pan.baidu.com/s/1jz7-33xO2F062s5IF0DosA?pwd=jjrg
Quark link: https://pan.quark.cn/s/26856336f9fd

You don't need any dev environment or command line on your computer at all — just click around in GitHub and the Cloudflare Pages dashboard, and you can get "ZYW image host" fully deployed in 10 minutes.

Step 1: Upload the source to your GitHub repo
● Unzip the source archive: unzip zyw_tuchuang_v2.5_opensource.zip on your computer, open the zyw-tuchuang folder inside, and you'll see public, functions, package.json and so on.
● Create a new private repo: log in and, following GitHub's create-a-repo guide, click the + in the top right and choose New repository, name it zyw-tuchuang, tick Private (safer that way), and click Create repository at the bottom.
● Drag-and-drop upload in the browser: on the new empty repo page, click uploading an existing file (see GitHub's web upload docs), and drag all the files and subfolders from inside the zyw-tuchuang folder into the box (note: drag public, functions etc. in directly — don't wrap them in an extra folder layer).
● Commit the code: once all the progress bars finish, just click the green Commit changes button at the bottom of the page.

Step 2: Create the database and bucket in Cloudflare
After logging into the Cloudflare dashboard, you first need to create a KV namespace for the image index and an R2 bucket for the original image files:
● Create the KV index space: in the left menu go to Storage & Databases → KV (see Cloudflare's Workers KV getting-started guide), click Create a namespace (or Create instance), enter zyw-image-index as the name, and click Add to finish.
● Create the R2 image bucket: in the left menu go to R2 Object Storage → Overview (see Cloudflare's R2 bucket creation guide), click Create bucket, enter zyw-images as the name, leave the location on default auto, and click Create bucket (R2 gives you 10GB free per month with no egress fees).
● Set up a Telegram bot (optional dual backup): if you want "R2 + Telegram dual-active backup", open the official @BotFather in Telegram and send /newbot to get a TG_BOT_TOKEN, then add the bot to your private channel as an admin to get the channel ID (usually starts with -100); skip this if you're only using R2.

Step 3: Connect the repo to Cloudflare Pages and deploy for the first time
● Go to the Pages creation page: in the Cloudflare left menu click Compute (Workers) → Workers & Pages, click Create in the top right, then switch to the Pages tab at the top and choose Connect to Git (see Cloudflare's Pages Git integration docs).
● Authorize and pick the repo: click to connect your GitHub account, select the zyw-tuchuang repo you just created from the list, and click Begin setup.
● Fill in the build config:
○ Project name: keep zyw-tuchuang (or use your own).
○ Build command: leave it empty — this source has no bloated bundler, so nothing goes here.
○ Build output directory: enter public (required, points to where the frontend lives).
● Run the first deploy: click Save and Deploy at the bottom and wait about 20 seconds for the success message.

Step 4: Bind storage and environment variables (the most important step)
Once the first deploy succeeds, click Continue to project to open the project page, then go through Settings at the top to configure the following two things (see Cloudflare's Pages bindings docs):

Where to configure | Binding type / Variable name | Value or selection | What it does
Settings → Bindings → Add → KV namespace | IMAGE_INDEX | Select the zyw-image-index created in step 2 | Stores the image short-link mapping and login brute-force counters (required, name must be all caps)
Settings → Bindings → Add → R2 bucket | ZYW_R2_BUCKET | Select the zyw-images created in step 2 | Stores uploaded HD images and video files (required, name must be all caps)
Settings → Environment variables → Add | UPLOAD_AUTH_PASSWORD | e.g. zyw888888 (pick your own strong password) | Sets the password gate for access and uploads; if you don't add this, the whole site is open for passwordless uploads
Settings → Environment variables → Add | SESSION_SECRET | any long random string (e.g. zyw_2026_abcdefg) | Encrypts the login cookie and prevents forged sessions
Settings → Environment variables → Add | STORAGE_DRIVER | r2 (R2 only) or dual (R2 + TG dual-active) | r2 is the simplest if you only use R2; use dual to turn on the backup
Settings → Environment variables → Add | TG_BOT_TOKEN | the token generated by @BotFather | Only needed when STORAGE_DRIVER is set to dual or telegram
Settings → Environment variables → Add | TG_CHAT_ID | your Telegram channel ID (e.g. -100123456) | Only needed when the Telegram storage channel is enabled

Binding R2: Cloudflare Dashboard → R2 → Create bucket (e.g. zyw-images) → back to the Pages project → Settings → Bindings → Add → R2 bucket, variable name ZYW_R2_BUCKET

Binding KV (not mandatory). Pages → Settings → Bindings → Add → KV namespace, variable name IMAGE_INDEX. The free quota is more than enough; the main point is to get login rate limiting working, otherwise someone can brute-force your upload password forever.

Step 5: Re-trigger the deploy so the config takes effect
● Re-run the latest deployment: since bindings and environment variables only take effect after a reload, go to the Deployments tab at the top of the project, find the three-dot (...) button to the right of the first deployment record, and click Retry deployment.
● Bind your own domain (strongly recommended): once the retried deployment turns green/Active, you can use the *.pages.dev URL Cloudflare assigned you right away; if you're worried about the free domain changing and breaking your hotlinks, bind your own domain under Custom domains at the top of the project, then change "same-domain direct link domain" in the top right of the upload page to your domain — after that you can bulk upload in one click and copy BBCode, HTML, Markdown and direct links.
If you can't find a button at some step or hit an error message, send me a screenshot or the error text and I'll walk you through it step by step.

Translated by AI from 简体中文

17
22
Log into join the discussion.

22 replies

Sort order
  1. GeorgeXie 💬 Master Never leave, never forsake 1mo ago

    Pretty nice

    Translated by AI from 简体中文

    4
  2. alay Platinum 3 YEARS 1mo ago

    Nice, support this!

    Translated by AI from 简体中文

    3
  3. zhzyw Silver OP 1mo ago

    Updated at 15:01 — testing revealed a bug in the previous version, now fixed.

    Translated by AI from 简体中文

    0
  4. zhzyw Silver OP 1mo ago

    Thanks bro

    Translated by AI from 简体中文

    0
  5. tufei 1mo ago

    Really detailed tutorial, thanks for sharing.

    Translated by AI from 简体中文

    2
  6. jhon25 Gold 1mo ago

    This approach boils the deployment down to pure web-based operations, which really lowers the barrier to setting up an image host.

    Translated by AI from 简体中文

    2
  7. Jade Gold 1mo ago

    Thanks for sharing

    Translated by AI from 简体中文

    1
  8. catloe9fb7 Diamond 3 YEARS 1mo ago

    Thanks for sharing.
    After uploading, does it automatically go to both sides?

    Translated by AI from 简体中文

    1
  9. zhzyw Silver OP 1mo ago

    You can just pick R2, or select both.

    Translated by AI from 简体中文

    0
  10. Kingjames 💘 Gold 3 YEARS 1mo ago

    Like first, read later.

    Translated by AI from 简体中文

    1
  11. Er4dicate Silver 1mo ago

    Thank you

    1
  12. nahear Diamond 1mo ago

    Nice, nice

    Translated by AI from 简体中文

    1
  13. stg Gold 3 YEARS 1mo ago

    酷

    1
  14. weiweiya Gold 1mo ago

    Like first, then read.

    Translated by AI from 简体中文

    1
  15. user68 💬 Platinum 3 YEARS 1mo ago

    Nice, support this! :xhj001:

    Translated by AI from 简体中文

    1
  16. calimer Silver Never leave, never forsake 1mo ago

    Bookmarked for later, this is really useful — thanks for sharing!

    Translated by AI from 简体中文

    1
  17. unethical8117 Bronze 1mo ago

    Nice, you've got my support.

    Translated by AI from 简体中文

    1
  18. trial Master Diamond member 1mo ago

    The tutorial works for me.

    Translated by AI from 简体中文

    1
  19. musemeta Silver 1mo ago

    Thanks for sharing, saving this for later!

    Translated by AI from 简体中文

    1
  20. fuax Silver 1mo ago

    What kind of limits does R2 have? Just asking since I don't know.

    Translated by AI from 简体中文

    0
  21. zhzyw Silver OP 1mo ago

    Just a 10G limit?

    Translated by AI from 简体中文

    0
  22. 2mm Silver 1mo ago

    Having to install an app just to download a few things is such a hassle

    Translated by AI from 简体中文

    0