SUEN

JSONs Gateway Manual (Cloudflare R2 + Workers)

Version: v1.5.0 (per‑file ACL + web editor + admin list)

This guide documents a secure workflow to store multiple JSON files in Cloudflare R2 and access them via a Cloudflare Worker using token-based permissions.


1) System Overview

You have three parts:

  1. R2 bucket named jsons that stores objects like x.json.

  2. Worker that:

  1. Custom domain route to the Worker (not documented here by name).

2) Placeholders Used in This Manual


3) Permission Model (RO / RW / Admin)

The Worker supports three identity types:

3.1 Admin

3.2 Read‑Only token (RO)

3.3 Read+Write token (RW)

Important: Tokens are not interchangeable unless you intentionally assign the same value.


4) One‑Time Setup (Cloudflare UI)

4.1 Create the R2 bucket

  1. Cloudflare Dashboard → R2
  2. Create bucket
  3. Bucket name: y
  4. Create

4.2 Create / deploy the Worker

  1. Cloudflare Dashboard → Workers & Pages
  2. Create Worker
  3. Paste the full Worker code (v1.5.0)
  4. Deploy

4.3 Bind the bucket to the Worker

  1. Worker → SettingsBindings
  2. Add binding:
    • Type: R2 bucket
    • Variable name: CONFIG
    • Bucket: jsons
  3. Save

4.4 Configure secrets (tokens)

  1. Worker → SettingsVariables and Secrets
  2. Add Secrets:
    • A
    • X_RO, X_RW
    • ,

Secrets are available immediately after saving; no extra build step is required.


5) Upload / Add JSON Files to the Bucket

5.1 File names must match your ACL list

You should upload JSON files with these exact names (case‑sensitive):

5.2 Upload in Cloudflare UI

  1. Cloudflare Dashboard → R2 → bucket jsons
  2. Upload
  3. Select a local JSON file
  4. Rename it to the correct key if needed
  5. Save

6) How to Read JSON (API)

Use:

text
<BASE_URL>/<KEY>?token=<TOKEN>

Examples (placeholders):

7) Online Editing (Web UI)

Editing is only allowed for RW tokens of the same file, or Admin.

7.1 Open the editor

text
<BASE_URL>/_edit?key=<KEY>&token=<TOKEN>

Example:

text
<BASE_URL>/_edit?key=x.json&token=<TOKEN_x_RW>

7.2 Save rules


8) Admin Panel: List All Keys + One‑Click View/Edit

The Admin panel is Admin‑only.

8.1 Open the panel

text
<BASE_URL>/_list?token=<TOKEN_ADMIN>

8.2 What it shows

Each row includes:


9) Rotate / Replace a Token

If a token is leaked or you want to rotate it:

  1. Worker → SettingsVariables and Secrets
  2. Find the target secret (example: TOKEN_x_RW)
  3. Use Rotate or manually replace the value
  4. Save

✅ The new value takes effect immediately.

After rotation, any old links containing the old token will stop working.


10) Add a New JSON File Later (Safe Checklist)

When you want to add a new file (example: f.json):

10.1 Add new secrets

Add two secrets:

10.2 Update the Worker code

In the FILES array, append:

js
{ key: "f6.json", secretNameRO: "TOKEN_f6_RO", secretNameRW: "TOKEN_f6_RW" },

10.3 Upload the new JSON

Upload f6.json into the R2 bucket jsons.