# Upload files of any size to Culacc from your code

Store what your users upload, your app's exports, backups or media in Culacc's
EU storage. The bytes go straight to storage, never through Culacc's API
server or (for browser uploads) yours. Files can be as big as 5 TB.

## From your server (Node)

```js
const { Culacc } = require('@culacc/sdk');
const culacc = new Culacc({ key: process.env.CULACC_KEY });   // a key with files:write

// Bytes you have in memory: a Buffer, Uint8Array or Blob
const photo = await culacc.upload(buffer, { name: 'photo.jpg', path: '/Users/42/Photos' });

// A file on disk, any size, without reading it all into memory
const backup = await culacc.uploadFile('./db-backup.tar.gz', {
  path: '/Backups',
  onProgress: ({ loaded, total }) => console.log(Math.round(loaded / total * 100) + '%'),
});

// A whole folder, keeping its subfolders
const { folder, files, failed } = await culacc.uploadFolder('./site-export', { path: '/Sites/acme' });
```

Missing folders in `path` are made. Files over 64 MB go in parts, four at a
time, and a part that fails is sent again on its own.

## When the name is taken

| `onConflict` | What happens |
|---|---|
| `rename` (default) | the new file becomes `photo (1).jpg` |
| `replace` | the existing file gets the new contents and keeps its id, so shares and published links go on working; the old contents are deleted |
| `version` | the same, but the old contents stay as an earlier version |
| `fail` | an error (409) |

```js
await culacc.uploadFile('./prices.csv', { path: '/Feeds', onConflict: 'replace' });
```

## Straight from your user's browser

Your key stays on your server. Your server asks Culacc for an upload address
and hands the page only that address:

```js
// Your server (Express)
app.post('/api/upload-start', requireLogin, async (req, res) => {
  const { name, size, type } = req.body;
  const up = await culacc.uploads.create({ name, size, mime: type, path: `/Users/${req.user.id}` });
  res.json({ uploadId: up.uploadId, uploadUrl: up.uploadUrl });
});

app.post('/api/upload-done', requireLogin, async (req, res) => {
  const { file } = await culacc.uploads.complete(req.body.uploadId);
  res.json(file);                       // save file.id with your user
});
```

```js
// Your page
const f = input.files[0];
const { uploadId, uploadUrl } = await post('/api/upload-start', { name: f.name, size: f.size, type: f.type });
await fetch(uploadUrl, { method: 'PUT', body: f });
const file = await post('/api/upload-done', { uploadId });
```

For files over 500 MB the answer has a `multipart` plan instead of an
`uploadUrl`: slices of `partSize` bytes, each PUT to its own address. Each
address takes exactly the `size` it lists, so send each slice as a buffer or
blob (with its `Content-Length`). Ask for more addresses with
`culacc.uploads.parts(uploadId, { from: 101 })`.

## By hand, from any language

```
POST /v1/uploads          { "name": "film.mp4", "size": 2147483648, "path": "/Video" }
PUT  <uploadUrl>          the bytes  (or each part to its address)
POST /v1/uploads/<id>/done
```

Size is what storage really received; more than you announced is refused.
`GET /v1/uploads/<id>` shows which parts have arrived, so an interrupted
upload can carry on. `DELETE /v1/uploads/<id>` cancels and removes the parts.

## Folders, deleting, finding files

```js
await culacc.folders.create('/Users/42/Invoices');
const { files } = await culacc.files.list({ path: '/Users/42', limit: 100 });
await culacc.files.delete(files[0].id, { permanent: true });   // frees the space at once
```

## Know when it's there

The `file.uploaded` webhook fires for every finished upload, with the file's
id, name, path and size.

## Limits and costs

- One file: up to 5 TB. Up to 500 MB in one PUT, above that in parts of 32 MB or more.
- An upload has 15 minutes to finish (24 hours in parts); unfinished ones are cleaned up.
- Free space is checked when an upload starts (counting the ones already on their way) and again when it's done;
  up to 50 uploads can be on their way at once.
- Uploads count toward storage (5 GB free, then €0.50 per 100 GB a month), not traffic.

## Reference

- [Uploading](/developers#uploading) and [Big files, in parts](/developers#big-files-in-parts) in the API reference
- [Send files to your users](/developers/send-files/)
