Documentation

File Storage (R2)

Save AI results, let users view their assets, and add reference-image uploads when needed

R2 stores generated images, videos, and other assets. Complete “Save generated results” first. Continue to reference-image uploads when users need to upload an image before generating.

Local files and online files are separate. Use a deployed test website to check the complete preview and upload flows.

Save generated results

1. Create a bucket

Open R2 in Cloudflare Dashboard, create a bucket, and note its name.

Find r2_buckets at the top level of wrangler.jsonc. Set bucket_name to your new bucket's name. Keep binding as R2:

{
  "binding": "R2",
  "bucket_name": "your-bucket-name"
}

2. Set the asset URL

In that bucket's settings, enable its public development URL or connect your own asset domain. Copy the address that serves your assets, such as https://media.example.com.

Add this to local .dev.vars and upload the same named Worker secret online:

STORAGE_PUBLIC_URL=https://media.example.com

Use the asset domain or public development URL here, not the S3 API address. It must point to the bucket from the previous step.

3. Generate and check a result

Follow the AI guide to prepare keys, models, and credits, then generate an image at /generate/. Confirm the job completes, the asset library shows a preview, and the asset link opens.

Upload reference images

Continue here when users need to upload an image before generating. You can skip this section if you only need to save generated results.

1. Get upload credentials

In Cloudflare R2 API Tokens, create credentials with object read/write access to the same bucket. Copy the Access Key ID, Secret Access Key, and account S3 API endpoint.

Add these to local .dev.vars and upload the same named Worker secrets online:

STORAGE_ACCESS_KEY_ID=your-access-key-id
STORAGE_SECRET_ACCESS_KEY=your-secret-access-key
STORAGE_REGION=auto
STORAGE_BUCKET_NAME=your-bucket-name
STORAGE_ENDPOINT=https://your-account-id.r2.cloudflarestorage.com
STORAGE_PUBLIC_URL=https://media.example.com

STORAGE_BUCKET_NAME must match the bucket name in wrangler.jsonc.

2. Allow uploads from your website

Open the bucket's Settings → CORS Policy and add the following settings. Replace the example URL with your website's address, including https:// and without a trailing slash:

[
  {
    "AllowedOrigins": ["https://app.example.com"],
    "AllowedMethods": ["GET", "HEAD", "PUT"],
    "AllowedHeaders": ["Content-Type"],
    "ExposeHeaders": ["ETag"],
    "MaxAgeSeconds": 3600
  }
]

Add any other test domains to AllowedOrigins. See Cloudflare's CORS guide for the dashboard steps.

3. Upload a reference image

  1. Confirm OPENAI_API_KEY is configured. Reference-image checks use OpenAI by default; see Admin → Settings → Content Moderation for current settings.
  2. Open /generate/ and choose a configured reference-image model.
  3. Upload a JPEG, PNG, or WebP up to 10MB. Wait for checks to pass and the reference preview to appear.
  4. Enter a description, generate, and confirm you can preview the result.

If uploads or previews fail

SymptomFirst check
Generated result does not openSTORAGE_PUBLIC_URL is accessible and belongs to the bucket storing the result
Upload reports missing configurationUpload credentials are complete and the online Worker has the required secrets
Browser reports a CORS errorAllowedOrigins includes the current website's full origin
Image check fails after uploadOpenAI Key and quota, then Admin → Moderation (/admin/moderation)

Storage fields in Admin → Settings are read-only. Change the environment settings, restart locally, or follow deployment to update the online site.

On this page