# Cloudflare R2 & S3 Media Storage

> Step-by-step guide to storing OvertCMS media in Cloudflare R2, Amazon S3, MinIO, or DigitalOcean Spaces—including API tokens, CORS, public CDN URLs, and upload size limits.

By default, OvertCMS stores uploaded files on local disk (`./.data/media`). For production deployments or multi-node setups, switch `OVERTCMS_MEDIA_DRIVER=s3` to store media in **Cloudflare R2**, **Amazon S3**, **MinIO**, or any S3-compatible object store.

OvertCMS implements pure-Go AWS Signature Version 4 signing directly—no heavy cloud SDKs or sidecars required.

---

## Step-by-Step: Cloudflare R2 Setup

<Steps>
  <Step title="Create a Bucket in Cloudflare R2">
    In the Cloudflare Dashboard, open **R2 Object Storage** and create a bucket (for example, `my-cms-media`).
    On the bucket's **Settings** tab, note:
    - **Bucket Name** (e.g., `my-cms-media`)
    - **S3 API Endpoint**: Copy **only the origin without the bucket name at the end**:
      `https://<account-id>.r2.cloudflarestorage.com`
      *(If the UI shows `https://<account-id>.r2.cloudflarestorage.com/my-cms-media`, strip `/my-cms-media` so the bucket name isn't duplicated!)*
  </Step>

  <Step title="Create an R2 S3 API Token">
    Go back to the **R2 Overview** page and click **Manage R2 API Tokens → Create API token**:
    - **Permissions**: **Object Read & Write**
    - **Specify bucket(s)**: Choose your bucket (recommended)
    - Click **Create API Token** and copy:
      - **Access Key ID** → `OVERTCMS_S3_ACCESS_KEY_ID`
      - **Secret Access Key** → `OVERTCMS_S3_SECRET_ACCESS_KEY`
    *(You do not need the top "Token value" — OvertCMS uses the S3 Access Key ID and Secret Access Key.)*
  </Step>

  <Step title="Add CORS Policy (Required for Large Browser Uploads)">
    In your bucket's **Settings → CORS Policy**, add the following JSON so direct presigned uploads from the Admin UI succeed in the browser:

    ```json
    [
      {
        "AllowedOrigins": [
          "https://cms.yourdomain.com",
          "http://localhost:5173"
        ],
        "AllowedMethods": ["PUT", "GET", "HEAD"],
        "AllowedHeaders": ["Content-Type"],
        "ExposeHeaders": ["ETag"],
        "MaxAgeSeconds": 3600
      }
    ]
    ```
  </Step>

  <Step title="Configure Environment Variables & Restart Core">
    Add these variables to your `.env.local` (for local dev) or container environment:

    ```bash
    OVERTCMS_MEDIA_DRIVER=s3
    OVERTCMS_S3_ENDPOINT=https://<account-id>.r2.cloudflarestorage.com
    OVERTCMS_S3_REGION=auto
    OVERTCMS_S3_BUCKET=my-cms-media
    OVERTCMS_S3_ACCESS_KEY_ID=your-r2-access-key-id
    OVERTCMS_S3_SECRET_ACCESS_KEY=your-r2-secret-access-key
    OVERTCMS_S3_FORCE_PATH_STYLE=true

    # Optional: serve media directly from your R2 Custom Domain or CDN
    # OVERTCMS_MEDIA_CDN_URL_TEMPLATE=https://media.yourdomain.com/{key}
    ```

    <Warning>
      Always set **`OVERTCMS_S3_FORCE_PATH_STYLE=true`** for Cloudflare R2 and MinIO. Without it, requests use virtual-hosted bucket subdomains (`https://bucket.<account-id>.r2.cloudflarestorage.com`) which fail TLS/routing on R2.
    </Warning>
  </Step>

  <Step title="Verify in Admin Settings">
    Restart `overtcms`, open **Settings → Storage & Infrastructure** in the Admin UI, and confirm the storage badge shows **S3 / R2** with your bucket name and endpoint. Upload a test image in **Media Library** and verify it appears in your R2 bucket.
  </Step>
</Steps>

---

## Public Media URLs vs Proxied Delivery

You have two ways to serve uploaded files to your website visitors:

| Mode | Configuration | How Media URLs Work |
| :--- | :--- | :--- |
| **Built-in Delivery Proxy (Default)** | Leave `OVERTCMS_MEDIA_CDN_URL_TEMPLATE` empty | Files serve from `/delivery/v1/media/{id}` with `Cache-Control: public, max-age=31536000, immutable` and `ETag`. Works even when your R2/S3 bucket is completely private. |
| **Direct CDN / R2 Custom Domain** | Set `OVERTCMS_MEDIA_CDN_URL_TEMPLATE=https://cdn.example.com/{key}` | OvertCMS emits direct CDN URLs in API responses, taking media bandwidth completely off your CMS server. Supported placeholders: `{key}`, `{id}`, `{siteId}`, `{ext}`. |

---

## Tuning Maximum Upload Sizes

Both upload paths are configurable via environment variables or CLI flags:

| Environment Variable | Default | Controls |
| :--- | :--- | :--- |
| `OVERTCMS_MEDIA_MAX_DIRECT_BYTES` | `26214400` (**25 MiB**) | Direct multipart uploads (`POST /media`), inline rich-text uploads, and remote URL sideloads. |
| `OVERTCMS_MEDIA_MAX_PRESIGNED_BYTES` | `524288000` (**500 MiB**) | Large browser/client uploads sent directly to S3/R2 via presigned upload intents (`POST /media/upload-intent`). |

For example, to allow **100 MiB** direct uploads and **2 GiB** presigned uploads:

```bash
OVERTCMS_MEDIA_MAX_DIRECT_BYTES=104857600
OVERTCMS_MEDIA_MAX_PRESIGNED_BYTES=2147483648
```

Restart `overtcms` after changing these values; the new limits appear automatically in **Settings → Storage & Infrastructure**.
