---
title: Media storage
description: The S3-compatible store where a self-hosted Peal keeps images and videos, and the upload rule browsers need.
sidebar:
  icon: hard-drive
---

Any S3-compatible store works. Browsers upload to the bucket directly, so besides the server's settings, you put an upload rule on the bucket.

## Connect the store [#media-storage]

The server reads these settings:

| Setting                                                                                    | Required | What it does                                                                                  |
| ------------------------------------------------------------------------------------------ | -------- | --------------------------------------------------------------------------------------------- |
| `PEAL_S3_ENDPOINT`, `PEAL_S3_BUCKET`, `PEAL_S3_ACCESS_KEY_ID`, `PEAL_S3_SECRET_ACCESS_KEY` | Yes      | The store, the bucket and the key. The server's key only needs to read and write objects.     |
| `PEAL_MEDIA_PUBLIC_BASE_URL`                                                               | No       | The bucket's public address, if it's also served publicly (see [Public links](#public-links)) |

[Environment variables](/self-hosting/environment-variables#media) lists the other media settings, with their defaults.

### On Cloudflare R2 in the EU [#r2-eu]

To keep media in the EU on Cloudflare R2, create the bucket in R2's EU jurisdiction, in the dashboard or with Wrangler:

```sh
wrangler r2 bucket create <name> --jurisdiction eu
```

Then put the `.eu.` endpoint in `PEAL_S3_ENDPOINT`: `https://<account>.eu.r2.cloudflarestorage.com`. Wrangler's other bucket commands need `--jurisdiction eu` too. The same goes for a backup bucket, whose endpoint is a host without a scheme.

> **A bucket's jurisdiction can't be changed later**
>
> You can't change a bucket's jurisdiction later.

## Add the upload rule [#cors]

Browsers send media straight to the bucket, with requests the server signs. The bucket must let the web app's origin send them, with a CORS rule that allows `PUT` from that origin with the `Content-Type` and `If-None-Match` headers. The same rule allows `GET` with a `Range` header, because the video editor's preview draws library files on a canvas, which only reads files that CORS allows. If you set the rule up before the editor existed, run the command again.

Set the server's variables, and make sure `PUBLIC_URL` and `TRUSTED_ORIGINS` name the web app's origins. Then this command puts the rule on the bucket and keeps the bucket's other rules:

```sh
bun nx run peal-server:media-cors
```

It also prints the same policy for Cloudflare R2's dashboard (R2 › the bucket › Settings › CORS policy). Running it again changes nothing. Its access key must be allowed to change the bucket's settings.

## Clean up unfinished uploads [#multipart]

Large videos go up in parts. An upload left unfinished keeps its parts in the store until something aborts it.

The server sweeps the media folder every hour, on one server at a time. Each sweep handles one kind of leftover:

| Leftover                                                                                                    | What the sweep does                                    | Setting                          |
| ----------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | -------------------------------- |
| An upload never completed                                                                                   | Deletes its media and its file, a day after it started | `PEAL_MEDIA_SWEEP_PENDING_AFTER` |
| A multipart upload left open                                                                                | Aborts it a day after it started                       | `PEAL_MEDIA_SWEEP_UPLOADS_AFTER` |
| A file under `media/` that no media holds, such as one sent with a link signed before its media was deleted | Deletes it once it's a day old                         | `PEAL_MEDIA_SWEEP_ORPHANS_AFTER` |
| The files of a deleted workspace that the store didn't delete at once                                       | Deletes them again until none is left                  |                                  |

Each run deletes 500 of each at most (`PEAL_MEDIA_SWEEP_BATCH`) and leaves the rest to the next. A file the store refuses to delete is counted and left.

The sweeps touch only keys under `media/` that Peal writes, below `PEAL_S3_KEY_PREFIX`. They never touch the erasure ledger or another instance's prefix. A sweep that keeps failing raises the `peal-media-sweep-failing` alert.

The sweeps run in production only, unless `PEAL_MEDIA_SWEEPS` says otherwise. `off` stops them all.

### Which database the folder belongs to [#media-owner]

The pending and orphan sweeps delete what the database doesn't know of, so the database must be the bucket's newest truth. The folder's owner marker, `media/.owner`, says which database that is. It holds:

- The database's random id.
- The Postgres cluster's system identifier and timeline.
- An epoch that only these sweeps advance, one more at each run, in the database and the marker together.

The first run writes the marker. From then on, the two sweeps delete nothing, and raise the `peal-media-sweep-refused` alert, when the marker disagrees with the database:

| Reason     | What the marker shows                                                                                          |
| ---------- | -------------------------------------------------------------------------------------------------------------- |
| `foreign`  | It names another database.                                                                                     |
| `unmarked` | It's gone, after this database had written it.                                                                 |
| `restored` | It names another Postgres cluster or timeline.                                                                 |
| `rewound`  | It counts more runs than the database, so the database went back in time, as a disk snapshot rolled back does. |

Every restore from pgBackRest ends on a new timeline, and a database loaded from a dump runs on a new cluster, so both read as `restored`. The timeline is read from the write-ahead log being written, so a failover to a replica is seen at once. `pg_upgrade` gives a new cluster. The sweeps stop after either, until you hand the folder over.

A snapshot taken after the last run the marker counts reads as sound. That window is up to half an hour while the store answers, longer when the store failed the runs' marker writes, and as long as the sweeps were off, since only their runs move the epoch on. Keep the sweeps off across a snapshot's roll-back too.

> **Turn the sweeps off before you start a server on a restored database**
>
> Set `PEAL_MEDIA_SWEEPS=off`. The restored database doesn't know the files
> uploaded since the backup, and the sweeps would delete them.

As a last guard, two breakers catch a large gap:

- The orphan sweep deletes nothing on a page of the listing with more files the database doesn't know of than `PEAL_MEDIA_SWEEP_ORPHANS_LIMIT` (100), or than a fifth of 50 old files or more.
- The pending sweep deletes nothing when uploads never completed are found in more workspaces than `PEAL_MEDIA_SWEEP_PENDING_LIMIT` (100). One workspace's backlog never trips it.

Handing the folder over doesn't clear a breaker. Once you know the database is sound, raise the limit for a run (and restart the server), then set it back.

### Hand the folder over [#claim-media-folder]

Hand the folder over once the database is meant to own it: a restore confirmed final, a failover or an upgrade done, a database loaded from a dump, or a marker lost. The command prints the database, the bucket and prefix, the marker there now and what it would write. It writes only with `--yes`:

```sh
docker compose … run --rm --no-deps server packages/peal-server/src/claim-media-folder.ts
docker compose … run --rm --no-deps server packages/peal-server/src/claim-media-folder.ts --yes
```

It changes the marker and nothing else.

### Add a lifecycle rule [#lifecycle]

As a second guard, add a lifecycle rule that aborts incomplete multipart uploads, on the media folder alone. When you set `PEAL_S3_KEY_PREFIX`, put it before `media/` in the rule's prefix, such as `staging/media/`.

**Cloudflare R2**

R2's buckets also abort them after seven days by default. Add `--jurisdiction eu` for a bucket in the EU jurisdiction:

```sh
wrangler r2 bucket lifecycle add <bucket> abort-unfinished-uploads media/ --abort-multipart-days 2
```

**S3 API**

Where the store supports lifecycle rules, save the rule as `lifecycle.json`:

```json
{
  "Rules": [
    {
      "ID": "abort-unfinished-uploads",
      "Status": "Enabled",
      "Filter": { "Prefix": "media/" },
      "AbortIncompleteMultipartUpload": { "DaysAfterInitiation": 2 }
    }
  ]
}
```

Then put it on the bucket. Add `--endpoint-url` for a store other than AWS:

```sh
aws s3api put-bucket-lifecycle-configuration --bucket <bucket> --lifecycle-configuration file://lifecycle.json
```

> **One configuration per bucket**
>
> On an S3 API, this replaces every lifecycle rule the bucket had, the erasure
> ledger's included, so put them all in the same file. Never add an expiration
> to `media/`, because it would delete media in use.

## How the server reads the files [#probe]

Once a file is stored, the server reads it. It reads an image's dimensions with sharp, and a video's dimensions, duration and codec with ffprobe, which the server's image ships.

A video uploaded to a server without ffprobe stays uploaded, marked as unread, until it's completed again on a server that has it.

## Public links [#public-links]

Pages show media through short-lived signed links. If the bucket is also served publicly, as through an R2 custom domain, set `PEAL_MEDIA_PUBLIC_BASE_URL` to that address, and links to media use it instead.
