# Storage
Source: https://sovaria.in/docs/storage-overview

Store files in buckets, control who can read and write them, and serve them with public or signed URLs.

Files live in **buckets**. A bucket has a size limit per file, an optional list of allowed content types, and access rules that decide who can upload, download and delete. Files are addressed by a path such as `avatars/<user-id>/photo.png`.

(Diagram: Buckets, rules and URLs)

## Create a bucket

In **Storage → New bucket**, pick a name, whether it is **public**, and who can access it. The defaults are the safe ones: signed-in users manage files in their **own folder** (`<user id>/…`) and project admins can do everything.

| Access rule (as shown in the dialog) | What it allows |
|---|---|
| Signed-in users manage files in their own folder | Upload, download and delete only under `<their user id>/`. Other paths are refused. |
| Any signed-in user can download any file | Read access for every signed-in user. |
| Any signed-in user can upload and replace any file | Write access for every signed-in user. |
| Any signed-in user can delete any file | Delete access for every signed-in user. |
| Project admins can do everything | App users with the `admin` role (and the service role key). |
| Nobody can delete | Overrides the rules above — good for receipts and audit trails. |
| Public bucket (switch) | Anyone with the link can download, without signing in. |

Visitors who are not signed in (requests with only the anon key) get nothing from a private bucket unless you add a rule for them.

CLI:
```bash
sov storage create-bucket avatars
sov storage buckets
```

JavaScript:
```js
await sovaria.storage.createBucket({
  name: 'avatars',
  public: false,
  file_size_limit: 5 * 1024 * 1024,           // 5 MB
  allowed_mime_types: ['image/png', 'image/jpeg', 'image/webp'],
})
```

## Upload and download

JavaScript:
```js
const me = await sovaria.auth.me()
const path = `${me.id}/avatar.png`

await sovaria.storage.upload('avatars', path, file, { upsert: true, contentType: 'image/png' })
const blob = await sovaria.storage.download('avatars', path)
await sovaria.storage.remove('avatars', path)
```

CLI:
```bash
sov storage upload avatars ./avatar.png --name <user-id>/avatar.png
sov storage ls avatars
sov storage download avatars <user-id>/avatar.png -o avatar.png
```

cURL:
```bash
curl -s -X POST https://<project-ref>.sovarias.in/api/v1/store/objects/avatars/<user-id>/avatar.png \
  -H "Authorization: Bearer $ACCESS_TOKEN" -H 'X-Sovaria-Project-ID: <project-id>' \
  -F file=@avatar.png
```

## Names, paths and overwrites

- Bucket names are lowercase letters, digits, `.`, `_` and `-`, start with a letter or digit, and are at most 128 characters. Creating a bucket that exists returns `409`.
- Object paths may contain `/` to form folders. With the own-folder rule, the first folder must be the user's id: `<user id>/avatar.png`.
- Uploading to a path that already has a file returns `409 Object already exists`. Pass `upsert: true` (or `?upsert=true`) to replace it.
- Each bucket has a per-file size limit (default 50 MB) and an optional list of allowed content types. Your plan caps both.
- List a folder with `sovaria.storage.listObjects(bucket, { prefix: '<user id>/' })`; rename or move a file with `moveObject({ from_path: 'avatars/<user id>/old.png', to_path: 'avatars/<user id>/new.png' })` (paths include the bucket). Moving needs write access to both paths and never replaces an existing file.

## Public and signed URLs

- **Public buckets**: `sovaria.storage.publicUrl(bucket, path)` returns a permanent URL anyone can open.
- **Private buckets**: `sovaria.storage.signDownload(bucket, path, seconds)` returns `{url, expires_at}` — a URL that works without signing in until it expires (default 1 hour, at most 24 hours). `signUpload` does the same for uploads, so a browser can upload directly without your server in the middle.
- Downloads of files that browsers could render as pages (HTML, SVG, XML, scripts) are served as sandboxed attachments on every route that returns a file — object downloads, public and signed links, S3 GetObject and presigned URLs, image transforms — so an uploaded file can't run scripts on your domain.

## Limits

File size is capped by the bucket's limit and your plan. Total storage and bandwidth depend on your plan; when a limit is reached uploads return `PLAN_LIMIT_REACHED` and owners are emailed.
