# Uploading files

Source: https://cubibase.com/docs/storage/uploads

Also available: [Markdown version](https://cubibase.com/docs/storage/uploads.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Uploads go directly from the client to S3. The engine reserves quota first, issues a short-lived policy pinned to one staging key and the exact size, then verifies and finalizes the object.

## The three-step contract

1. **Start the upload** — POST the bucket, logical path, exact size in bytes, and MIME type. The engine validates the path, checks the plan and bucket limits, and atomically reserves the bytes against both the project and the account quota. If any limit would be exceeded, the request fails before any authorization is issued.
2. **POST the file to S3** — Build a multipart form: every field from the response’s upload.fields first, then Content-Type, then the file part last. POST it to upload.url. The policy pins the exact staging key and a content-length-range equal to the declared size, so S3 itself rejects any other size or destination.
3. **Complete the session** — POST to /storage/v1/uploads/{id}/complete. The engine heads the staged object, verifies its exact size and MIME type, copies it to its final key, converts the reservation into committed usage, and records the creation event — all in one transaction. Completion is idempotent: calling it twice returns the same object.

**1 · Start the upload**

```http
POST https://example-project.api.cubibase.com/storage/v1/objects/upload
apikey: cb_pub_EXAMPLE_ONLY
Authorization: Bearer <USER_JWT_OR_SECRET_KEY>
Content-Type: application/json

{
  "bucket": "avatars",
  "path": "images/mika.png",
  "sizeBytes": 182344,
  "mimeType": "image/png",
  "overwrite": false
}
```

**Response (trimmed)**

```json
{
  "uploadId": "f2c6…",
  "objectId": "9b1e…",
  "bucket": "avatars",
  "path": "images/mika.png",
  "upload": {
    "url": "https://<deployment-bucket>.s3.<region>.amazonaws.com",
    "fields": { "key": "staging/<project>/<upload>", "policy": "…", "x-amz-signature": "…" },
    "method": "POST"
  },
  "expiresAt": "2026-11-20T10:10:00.000Z"
}
```

**3 · Complete**

```http
POST https://example-project.api.cubibase.com/storage/v1/uploads/<UPLOAD_ID>/complete
apikey: cb_pub_EXAMPLE_ONLY
Authorization: Bearer <USER_JWT_OR_SECRET_KEY>
```

## Browser example

**JavaScript · fetch + FormData**

```ts
const start = await fetch(`https://example-project.api.cubibase.com/storage/v1/objects/upload`, {
  method: 'POST',
  headers: {
    apikey: PUBLISHABLE_KEY,
    authorization: `Bearer ${userJwt}`,
    'content-type': 'application/json',
  },
  body: JSON.stringify({
    bucket: 'avatars',
    path: `images/${file.name}`,
    sizeBytes: file.size,
    mimeType: file.type,
    overwrite: false,
  }),
}).then((r) => r.json());

const form = new FormData();
for (const [k, v] of Object.entries(start.upload.fields)) form.append(k, v);
form.append('Content-Type', file.type);
form.append('file', file); // must be the last part
await fetch(start.upload.url, { method: 'POST', body: form });

await fetch(`https://example-project.api.cubibase.com/storage/v1/uploads/${start.uploadId}/complete`, {
  method: 'POST',
  headers: { apikey: PUBLISHABLE_KEY, authorization: `Bearer ${userJwt}` },
});
```

## Overwrites and expiry

- A path that already holds a ready object rejects the upload with `409 storage_object_exists` unless you pass `overwrite: true`.
- Overwrite accounting uses the net change: replacing a 1 MB file with a 2 MB file commits +1 MB, not +2 MB.
- Sessions expire (default 5 minutes). An expired session releases its reservation and can never be completed; start a new upload.
- Failed or abandoned uploads never produce a downloadable object and never emit a creation event. Staging keys are cleaned up by a lifecycle rule and a cleanup worker.

> **warning**: The declared size must equal the actual file size exactly. S3 enforces the content-length-range, and completion re-verifies the size — a mismatch fails the session and releases the reservation.

## Additional resources

- [Downloads and sharing](https://cubibase.com/docs/storage/downloads) Read the file back.
- [Quotas and errors](https://cubibase.com/docs/storage/quotas) What a rejected reservation looks like.
