A Cloudflare Worker that fetches images from a remote URL and stores them directly into an R2 bucket, streaming the response body without buffering.
- Receives a
POSTrequest with a JSON body containing a source URL and a destination path - Checks if the file already exists in R2 — skips if so
- Fetches the image from the source URL (with optional
Refererheader) - Streams the image body directly into R2 without loading it into memory
Endpoint: POST https://woodtableguy888.imageuploads.workers.dev
Request body:
{
"url": "https://example.com/image.jpg",
"path": "folder/filename.jpg",
"referer": "https://example.com/"
}| Field | Type | Required | Description |
|---|---|---|---|
url |
string | Yes | The source URL to fetch the image from |
path |
string | Yes | The destination key/path in the R2 bucket |
referer |
string | No | Referer header to send with the fetch request |
Responses:
| Status | Body | Meaning |
|---|---|---|
| 200 | {"status":"stored","path":"..."} |
Image fetched and saved to R2 |
| 200 | {"status":"skipped","path":"..."} |
File already exists in R2 |
| 400 | "Missing fields" |
url or path not provided |
| 400 | "Invalid JSON" |
Request body is not valid JSON |
| 405 | "POST only" |
Non-POST request received |
| 500 | {"error":"...","status":...} |
Fetch or R2 error |
Example:
curl -X POST https://woodtableguy888.imageuploads.workers.dev \
-H "Content-Type: application/json" \
-d '{
"url": "https://photo.yupoo.com/woodtableguy888/da5ccd80/580a0ca1.jpg",
"path": "test/01.jpg",
"referer": "https://woodtableguy888.x.yupoo.com/"
}'- Node.js
- A Cloudflare account with an R2 bucket named
woodtableguy888
npm installnpm run devnpm run deploynpm run cf-typegenBindings are defined in wrangler.jsonc:
| Binding | Type | Resource |
|---|---|---|
MEDIA_BUCKET |
R2 Bucket | woodtableguy888 |