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

# Files

> Store bytes as labelled files, accept uploads on a route and read files inside a transaction.

A file is a fact of the host's `sys.files` table plus its bytes. The bytes
live outside the log; the fact is transactional and carries the file's
label. A fact of your own keeps the `File` point in a `t.file` field, next to
the filename and any other business data.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, t, table } from "@bijectionhq/bijection";

export const finance = label();

export const receipts = table(
  { file: t.file, owner: t.principal, size: t.number, contentType: t.maybe(t.string) },
  { label: finance, index: [["owner"]] },
);
```

A file carries the label of the frame that stored it. Reading its metadata
or its bytes needs a frame that clears that label, so a file is as
confidential as the data it came from.

| Act | How |
| - | - |
| Accept an upload | A [`route.post(path, verifier)` reaction](https://docs.bijection.com/files/upload): the host stores the request's bytes and hands the reaction `request.file` |
| Store generated bytes | [`ctx.store({ bytes, contentType })`](https://docs.bijection.com/files/store) in a mutation returns the `File` point |
| Read metadata | [`ctx.get(file)`](https://docs.bijection.com/files/serve): `{ id, sha256, size, contentType }`, or `null` once erased |
| Read bytes | [`ctx.get(file.bytes)`](https://docs.bijection.com/files/serve): the bytes within the frame's clearance |
| Link a public file | [`ctx.get(file.url)`](https://docs.bijection.com/files/serve#serve-a-public-file-at-a-url): the URL the installation serves a public file at |
| Erase a file | [`ctx.erase(file)`](https://docs.bijection.com/files/delete), or keep it in no committed fact |

Use `act.store()` and `act.erase` to guard file acts. The
[system table](https://docs.bijection.com/database/system-tables.md#files) serves inventory reads.

## Files in the console

The console's file storage page lists the installation's stored files with
their keys, sizes and content types. An operator can download a file there.

![The Files page of the console](https://mintcdn.com/bijection-95ba84d3/MXGsXPKmgvoqOVRN/screenshots/pages_project_deployment_file_storage.png?fit=max\&auto=format\&n=MXGsXPKmgvoqOVRN\&q=85\&s=0330cc9539de123518cd085c0daf5ffa)

## Where bytes and keys live

The bytes of every file live in the host's `platform.files` slot, and the
root that wraps the installation's data keys lives in `platform.keys`. Both
are [slots](https://docs.bijection.com/operate/placement.md): unbound, Bijection's managed storage and key
ring serve them. To keep the bytes in your own bucket or the root in your own
KMS key, name it as the managed slot's `storage` or `root` in the
installation's bindings file. Bijection's driver still operates it; the bucket
or key is yours:

```ts bindings.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { placement } from "@bijectionhq/bijection";
const { bind, bindings, platform } = placement;

export default bindings([
  bind(platform.files, placement.managed({ storage: placement.aws.s3Bucket({ account: "111122223333", name: "acme-files", region: "eu-west-1" }) })),
  bind(platform.keys, placement.managed({ root: placement.aws.kms({ arn: "arn:aws:kms:eu-west-1:111122223333:key/9f0c…" }) })),
]);
```

`bijection deploy --bindings bindings.ts --trial` shows the trust
to install. Deploy copies the stored files while the old storage serves. At
cutover, the Root fences writes, verifies the final copy and adopts the new
store in the file ledger. Every subsequent file path follows that ledger.
A standalone host uses its label partitions as the managed store. A move
to your own key rewraps every live data key under the new root. Your
code and `File` points do not change. Google Cloud and Azure have `placement.gcp.gcsBucket`,
`placement.azure.container`, `placement.gcp.kms` and `placement.azure.keyVault`.
`placement.azure.container({ account, container, subscriptionId,
resourceGroup, tenantId, clientId })` names the storage account's
subscription and resource group, whose settings Bijection checks, and the app
registration it federates with. Cloudflare R2 has
`placement.cloudflare.r2Bucket({ account, name, jurisdiction?, credential })`,
where `account` is the Cloudflare account ID, `name` is the bucket name, and
`jurisdiction` is the bucket's data jurisdiction (`eu` or `fedramp`) when it
has one. R2 has no workload
identity federation, so `credential` names a secret holding the bucket's
tokens as JSON: `{ "accessKeyId", "secretAccessKey", "configurationToken" }`,
an object read and write token scoped to the bucket and an API token that
reads the bucket's configuration.

Bijection keeps your files under `bijection/<installation>/files/` in your
bucket and must be their only writer there. Before it copies into an S3
bucket, and again when it verifies the copy, the bucket policy must carry
this statement, with the role `placement trust` printed:

```json theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
{
  "Effect": "Deny",
  "Principal": "*",
  "Action": "s3:*",
  "Resource": "arn:aws:s3:::acme-files/bijection/<installation>/files/*",
  "Condition": { "ArnNotEquals": { "aws:PrincipalArn": "<role>" } }
}
```

Every bucket must also stay private, with no lifecycle expiration, lock or
replication. Until it does, the move waits with its blocker. Bijection's host
reaches an S3 bucket by assuming your role with the installation's external
ID, a Cloud Storage bucket or Azure container through workload identity
federation, and an R2 bucket with the tokens in your secret, which it opens
only in its own process.

A move of `platform.files` or `platform.keys` runs in the host's own process,
for Bijection's storage and yours alike. `bijection placement show
platform.files` shows a move's phase and any blocker. When you move off
Bijection's storage, Bijection deletes its copy after the retention window,
every version of it. Moving back into Bijection's storage waits until that
deletion is no longer running. A local installation run by `bijection dev`
keeps its files on the local disk: a move of its `platform.files` waits at
the adoption step and the local copy stays in use. See
[switch providers](https://docs.bijection.com/operate/placement.md#switch-providers).

## Files kept elsewhere

Objects in a bucket you own stay there. The [object storage source
app](https://docs.bijection.com/files/connected-files.md) reads them into labelled files and facts of
your app.

## Libraries over files

* [Documents](https://docs.bijection.com/files/documents): collaborative notes and templates, and
  the reports rendered from them.
* [Recorded media](https://docs.bijection.com/files/media.md): recordings, playback, transcripts and
  renditions.


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