# Uploading a large file in parts — resumable, onto a row's attachment

> A file larger than the single upload's 10 MB — a run's log, a transcript, a recording — is sent in parts of at most 32 MiB to `/api/files/uploads` (or the MCP tools `start_file_upload`, `upload_file_part`, `finish_file_upload`). An interrupted sender resumes with the parts it still has to send. The upload ends as a FileAsset on a row's `[Attachment]` field, or as a new version of a file, under the same rules as a single upload.

<!-- id: storage-parted-upload · area: storage · stability: preview · html: https://osysharp.com/reference/storage/parted-upload/ -->

## Summary        {#summary}

A single upload (`POST /api/files/upload`) takes at most 10 MB and is held in memory while it is stored. A larger
file goes **in parts**: start the upload, put each part, then finish. The platform checks each part's size and SHA-256
as it arrives, joins the parts on disk (never in memory) when you finish, checks the whole file's size and SHA-256,
and only then makes the file.

The file lands on a **row's `[Attachment] FileAsset` field**, or becomes the **next version of a file**. Your Osy# code
reads it like any other file:

```osy title="a run keeps its log as an attachment, and a page reads its name and size" test app=storage-parted-upload
using Osysharp.Storage;

[Principal] entity User { [Required, MaxLength(200)] string Email; }

entity Run {
  [MaxLength(200)] string Title = "";
  [Required] User Owner;
  [Attachment] FileAsset? Log;
  security {
    allow read when IsAuthenticated;
    allow create, update where Owner == user;
  }
}

/// The run's log as its owner's host uploaded it: name, size and the bytes themselves.
[Authorize(IsAuthenticated)]
public string LogSummary(Run run) {
  var withLog = Run.Where(r => r.Id == run.Id).Include(r => r.Log.CurrentVersion).First();
  if (withLog.Log == null) { return "no log yet"; }
  var bytes = File.ReadAllBytes(withLog.Log);
  return withLog.Log.Name + " · " + Text.ByteSize(withLog.Log.CurrentVersion.Size ?? 0) + " · " + bytes.Length + " bytes read";
}
```

## Description    {#description}

### The three steps    {#steps}

All three take the app's bearer ticket or API key, exactly as `POST /api/files/upload` does.

| step | REST | MCP tool |
|---|---|---|
| start, or resume | `POST /api/files/uploads` | `start_file_upload` |
| put part *n* (numbered from 0) | `PUT /api/files/uploads/{id}/parts/{n}` | `upload_file_part` |
| finish | `POST /api/files/uploads/{id}/finish` | `finish_file_upload` |
| which parts are held | `GET /api/files/uploads/{id}` | start again with `uploadId` |
| give up | `DELETE /api/files/uploads/{id}` | — |

**Start** names the file and where it goes:

```json title="start: a new file for a Run's Log field" syntax
{
  "fileName": "run-4711.log", "mimeType": "text/plain",
  "totalBytes": 157286400, "sha256": "<the whole file's SHA-256, hex>",
  "partBytes": 16777216,
  "target": { "entity": "Run", "id": "<the run's id>", "field": "Log" }
}
```

For a new version of an existing file, the target is `{ "fileAssetId": "<the file's id>" }`. The answer carries the
`uploadId`, the `partBytes` and `partCount`, the parts already `held`, and `expiresAt`.

**Put** each part as the raw request body, with its own SHA-256 (lower-case hex) in the `X-Osyrin-Part-Sha256`
header. Every part but the last is exactly `partBytes`. A part sent twice is harmless.

**Finish** joins the parts. It answers `{ fileAssetId, version, name, mimeType, size, sha256, url }`; when parts are
missing it answers `409` with `missing: [3, 7]`, and nothing is lost — send those and finish again.

### Resuming    {#resume}

Send the same start again with `"uploadId": "<id>"`. If it is still open, the answer's `held` lists the parts the
platform has, and you send only the rest. An upload whose manifest differs (another size, hash, name or target) is
`409`; one that finished, was abandoned or expired is `404` — start afresh.

### The rules — the same as a single upload    {#rules}

Every rule a single upload meets is checked **when the upload starts, before any part is kept**, and again when it
finishes:

- **The write rule.** Setting the field must be a write the caller may make: the row's `security` rules, any
  `except via`, and the attachment rule (only the file's owner may attach it — the uploader is). A new version needs
  the file's own write rule. A row or file the caller cannot read answers `404`.
- **The storage quota.** The whole size is charged at start; an upload that would pass the app's cap is `507`.
- **The organisation's largest file**, `files.maxBytes` (see [Your organization's policy: what each kind of rule affects, what its refusal says, and how to ask](https://osysharp.com/reference/project/organization-policy/)): `413`, saying the
  limit. With no rule a file may be at most 1 GiB; a rule may set any maximum up to 5 GiB. It holds for **every** file
  write — a single upload, `File.Create`, `File.Supersede` — not only for this one.

An upload is its starter's: a part, finish or status from anyone else answers as an upload that does not exist.

### Limits    {#limits}

| | |
|---|---|
| a part | at most 32 MiB (16 MiB when you name none) — well under a proxy's 100 MB / 100 s request bound |
| parts | at most 10 000 |
| the file | the organisation's `files.maxBytes`, else 1 GiB |
| an open upload | 24 hours from its start; after that its parts are reclaimed and its id answers `410`/`404` |

An SVG is sanitised as it is stored, so it is sent in one request. File contents never reach a log or a trace.

## See also       {#see-also}
- [[Attachment] — a file readable wherever its row is](https://osysharp.com/reference/storage/attachment/) — who may read and attach a file held by a row
- [A file's history — supersede, and what each version keeps](https://osysharp.com/reference/storage/file-versions/) — a file's history, which a parted upload can add to
- [Files (addressing something the app stores)](https://osysharp.com/reference/storage/index/) — the two ways the platform stores a file
- [publishing a REST API (app.Apis)](https://osysharp.com/reference/api/rest/) — the app's REST door and its credentials
- [Your organization's policy: what each kind of rule affects, what its refusal says, and how to ask](https://osysharp.com/reference/project/organization-policy/) — `files.maxBytes` and the organisation's other rules
