# File storage

An app with its own server can keep files people upload, or files it makes, in Toupas file storage. It works with any S3 client. Files count towards your account's storage.

## Getting it
If your app already uses an S3 client, `toupas live` creates its storage the first time. If the app saves uploads in a folder next to its code (`uploads/`, `media/`, `public/uploads/`), `toupas live` stops: live apps can only write to `/tmp`, which is emptied on every restart and update. Switch the app to S3 first; the files already in that folder are copied across the first time. To add storage later: `toupas storage create` (`--public` for public files too).

If the app already uses its own S3 storage, Toupas asks whether to look after storage instead (`--use-toupas-storage`) or keep yours (`--keep-own-storage`).

## The settings the app gets
| Setting | What it is |
|---|---|
| `S3_ENDPOINT` | The S3 endpoint |
| `S3_REGION` | Always `auto` |
| `S3_BUCKET` | The bucket |
| `S3_PREFIX` | The app's folder in the bucket. Start every file name with it |
| `S3_ACCESS_KEY_ID`, `S3_SECRET_ACCESS_KEY`, `S3_SESSION_TOKEN` | A temporary key that reaches only the app's folder |

Pass all three parts of the key to the S3 client, the session token too. Keys are renewed while the app runs; don't copy them anywhere else.

```js
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';

const s3 = new S3Client({
  endpoint: process.env.S3_ENDPOINT,
  region: process.env.S3_REGION,
  credentials: {
    accessKeyId: process.env.S3_ACCESS_KEY_ID,
    secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
    sessionToken: process.env.S3_SESSION_TOKEN,
  },
});

await s3.send(new PutObjectCommand({ Bucket: process.env.S3_BUCKET, Key: `${process.env.S3_PREFIX}photos/cat.jpg`, Body: data }));
```

If the app already reads the AWS names (`AWS_ACCESS_KEY_ID`, `AWS_ENDPOINT_URL_S3` and so on), `toupas storage names aws` gives it those instead.

## Public files
Files that anyone may open, such as product photos, go in a separate public bucket. With public files on (`toupas storage public enable`, or `--public` when creating), the app also gets `S3_PUBLIC_BUCKET`, `S3_PUBLIC_URL` and `S3_PUBLIC_ACCESS_KEY_ID`, `S3_PUBLIC_SECRET_ACCESS_KEY`, `S3_PUBLIC_SESSION_TOKEN`. A public file's link is `S3_PUBLIC_URL` followed by its name. Everything in the private bucket stays private: the app hands it out itself.

## On your computer
```
toupas dev storage
```
Starts S3 storage on your computer, like the live one (in Docker), and writes its settings to `.env`. Its files stay on your computer and are never uploaded. `--stop` stops it and `--reset` empties it.

## Looking at live files
| Command | What it does |
|---|---|
| `toupas storage info` | The app's storage, its size and settings |
| `toupas storage ls [path]` | List files |
| `toupas storage upload <file-or-folder> [path]` | Upload, up to 5 GB a file |
| `toupas storage download <path>` | Download a file |
| `toupas storage rm <path> --yes` | Delete files |
| `toupas storage credentials` | A key that works for an hour, for rclone or an S3 app on your computer |

Like the database, opening the live files from your computer is off at first: an account owner or admin turns it on in the Toupas panel.

## When storage is full
Your account's storage covers databases and files together. When it's full, apps can still read their files but can't save new ones after a grace period, until you delete some or add storage. `toupas usage` shows what's used, and [Pricing](/pricing) what each plan includes.
