> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gatojs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# File storage in production

> Choose where the GatoJS uploads module stores files in production: a Railway Bucket, a Railway volume, or local disk or S3-compatible storage on your own server.

If your project uses the [`uploads` module](https://docs.gatojs.com/modules/uploads), you need to decide where files live in production **before the first upload**. The module doesn't move files between providers, so switching later means migrating them yourself.

The module stores files wherever `STORAGE_PROVIDER` points:

* `local` (default): a directory on the server's disk;
* `s3`: any S3-compatible object storage.

Either way, files are always served through the app at `GET /api/uploads/<id>`, never directly from storage, so the bucket can stay private.

## Choose a location

| Location | Variables | Pros | Limits |
| - | - | - | - |
| **Railway Bucket** | `STORAGE_PROVIDER=s3` plus references to the bucket's variables | Allows replicas, no redeploy downtime, no need to run as root. | Always private; Railway provides no automatic backups or versioning for buckets. |
| **Railway volume** | `STORAGE_PROVIDER` unset or `local`, no `STORAGE_LOCAL_PATH` | Simple for a single instance; Railway offers manual and automatic volume backups. | One volume per service, no replicas, a short pause on every redeploy, and possible write-permission issues for the non-root user. |
| **Your own server** | `local` with an absolute `STORAGE_LOCAL_PATH` on a persistent volume, or `s3` with any provider | No platform lock-in. | With `local`: single instance only, and backups are up to the server admin. |

> [!TIP]
>
> **Recommendation:** on Railway, use a **Railway Bucket**. Use a volume only for a simple single-instance deploy. On your own infrastructure, use S3-compatible storage if you might ever run more than one instance.

## What production enforces

With `NODE_ENV=production` (always true in the Docker image), the startup env check refuses configurations that would lose files:

| Situation | Startup error |
| - | - |
| `local` on Railway without a volume | `STORAGE_PROVIDER: na Railway bez wolumenu pliki zginą przy redeployu: podepnij wolumen albo ustaw STORAGE_PROVIDER=s3` ("on Railway without a volume files are lost on redeploy: attach a volume or set `STORAGE_PROVIDER=s3`") |
| `local` on Railway with `STORAGE_LOCAL_PATH` outside the volume | `STORAGE_LOCAL_PATH: musi leżeć wewnątrz wolumenu Railway (...)` ("must be inside the Railway volume") |
| `local` outside Railway without `STORAGE_LOCAL_PATH` | `STORAGE_LOCAL_PATH: wymagana w produkcji przy STORAGE_PROVIDER=local: wskaż trwały katalog` ("required in production: point to a persistent directory") |
| `local` outside Railway with a relative path, including `./volumes/uploads` from `.env.example` | `STORAGE_LOCAL_PATH: w produkcji poza Railway musi być ścieżką bezwzględną` ("must be an absolute path") |
| `s3` with a missing `S3_ENDPOINT`, `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY` or `S3_BUCKET` | One line per missing variable |
| Any other `STORAGE_PROVIDER` value | `STORAGE_PROVIDER: dozwolone wartości: local, s3` ("allowed values") |

"On Railway" means `RAILWAY_ENVIRONMENT_ID` is set; Railway sets it for you. Any of these errors makes the server exit with code 1 at startup. The kit can't check whether a directory actually survives a redeploy; that is up to you.

## Railway Bucket

**1. Create the bucket**

Add a bucket to your Railway project and pick its region. You can't change the region later. The real bucket name is the display name plus a short hash, for example `my-bucket-jdhhd8oe18xi`.

**2. Reference its variables from the app service**

Replace `<bucket>` with the bucket's name in the project:

```bash theme={null}
STORAGE_PROVIDER=s3
S3_BUCKET=${{<bucket>.BUCKET}}
S3_ENDPOINT=${{<bucket>.ENDPOINT}}
S3_REGION=${{<bucket>.REGION}}
S3_ACCESS_KEY_ID=${{<bucket>.ACCESS_KEY_ID}}
S3_SECRET_ACCESS_KEY=${{<bucket>.SECRET_ACCESS_KEY}}
```

**3. Leave path-style off**

Don't set `S3_FORCE_PATH_STYLE`. Railway Buckets use virtual-hosted addressing (bucket name in the hostname). Only buckets created before Railway switched to this style may need `S3_FORCE_PATH_STYLE=true`.

## Railway volume

**1. Attach a volume**

Attach a volume to the app service with mount path `/app/volumes/uploads`. The image creates that directory for the `nextjs` user.

**2. Leave the path unset**

Don't set `STORAGE_LOCAL_PATH`, and leave `STORAGE_PROVIDER` unset or `local`. Railway sets `RAILWAY_VOLUME_MOUNT_PATH` on a service with a volume, and the module uses it as the storage directory. If you do set `STORAGE_LOCAL_PATH`, it must point inside the volume.

**3. Grant write access**

Set `RAILWAY_RUN_UID=0`. Railway documents permission issues for images that run as a non-root user (here `nextjs`, UID 1001) on mounted volumes; this variable runs the process as root.

> [!WARNING]
>
> A Railway service can have only one volume, a service with a volume can't have replicas, and each redeploy causes a short downtime. If you expect to scale out, use a bucket.

## Your own server

### Local disk

The image sets `NODE_ENV=production`, so `local` outside Railway needs an absolute `STORAGE_LOCAL_PATH`. Mount a persistent volume at the directory the image prepares:

```bash theme={null}
docker run -d --env-file prod.env \
  -v uploads:/app/volumes/uploads \
  -e STORAGE_LOCAL_PATH=/app/volumes/uploads \
  -p 3000:3000 my-app
```

Permissions matter because the process runs as `nextjs` (UID 1001):

* A **new, empty named volume** mounted at `/app/volumes/uploads` inherits the directory's ownership from the image, so `nextjs` can write to it.
* A named volume mounted at a path the image **doesn't** prepare fails with `Permission denied`.
* A **reused volume** whose files belong to root keeps its owner, and the first upload fails with `500 storage_failed`. Fix the ownership with `chown -R 1001:1001`.
* A **host directory** (`-v /srv/uploads:/app/volumes/uploads`) must be writable by UID 1001.

Back up the directory yourself. Running more than one instance requires `s3`.

### S3-compatible storage

Any S3-compatible service works: AWS S3, Cloudflare R2, MinIO, Backblaze B2 and others.

| Variable | Required | Notes |
| - | - | - |
| `STORAGE_PROVIDER` | Yes | `s3` |
| `S3_ENDPOINT` | Yes | Full `https://` URL of the provider's S3 API. |
| `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY` | Yes | Credentials with read and write access to the bucket. |
| `S3_BUCKET` | Yes | The bucket must **exist before the first deploy**; the app doesn't create it. |
| `S3_REGION` | No | Default `auto` (correct for R2). Set it for providers with regions. |
| `S3_FORCE_PATH_STYLE` | No | `true` for `endpoint/bucket/key` addressing instead of `bucket.endpoint`. Needed by MinIO and some self-hosted providers. Default `false`. |

> [!WARNING]
>
> Take credentials from your provider, not from `.env.example`. The sample values (`minioadmin`, `http://localhost:9000`) are for the local MinIO, and validation doesn't reject them. A region or addressing style that's well-formed but wrong for your provider only fails on the first upload, with `500 storage_failed`.

## Local development

Locally, `.env.example` uses `local` with `./volumes/uploads`. To try the S3 path, start MinIO with the `s3` Compose profile; it creates the `<slug>-uploads` bucket for you:

```bash theme={null}
docker compose --profile s3 up -d
```

Then set `STORAGE_PROVIDER=s3` in `.env`. MinIO is for development and CI only; it isn't part of the production image.

## Related

> [card] [Uploads module](https://docs.gatojs.com/modules/uploads.md)
>
> What the module does and how to use it in features.

> [card] [Deploy on Railway](https://docs.gatojs.com/deployment/railway.md)
>
> The full Railway setup.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.