# Files

Files go in and out as raw bytes, no base64 and no multipart. Keep what matters under `/workspace`, the part of the sandbox that lasts. A sleeping sandbox wakes for a file call.

**CLI**

```
ID=$(kosa8 cloud sandbox create --template python --ttl 15m --json | jq -r .sandbox.id)

echo 'print("hello from a file")' > hello.py
kosa8 cloud files put $ID hello.py /workspace/hello.py
kosa8 cloud sandbox exec $ID -- python3 /workspace/hello.py > out.txt
kosa8 cloud files ls $ID /workspace
kosa8 cloud files get $ID /workspace/hello.py      # to stdout
cat out.txt

kosa8 cloud sandbox rm $ID
```

**curl**

```
API=https://api.kosa8.com/v1
AUTH="Authorization: Bearer $KOSA8_API_KEY"
SBX=$(curl -s $API/sandboxes -H "$AUTH" -H 'Content-Type: application/json' \
  -d '{"template":"python","ttl_s":900}' | jq -r .sandbox.id)

# Upload: the request body is the file, streamed, any size
curl -s -X PUT "$API/sandboxes/$SBX/files?path=/workspace/hello.py" -H "$AUTH" \
  --data-binary 'print("hello from a file")' > /dev/null

# Download, and list the folder
curl -s "$API/sandboxes/$SBX/files?path=/workspace/hello.py" -H "$AUTH"; echo
curl -s "$API/sandboxes/$SBX/files/list?path=/workspace" -H "$AUTH" | jq -c '.files[] | {name, size}'

curl -s -X DELETE $API/sandboxes/$SBX -H "$AUTH"
```

**Python**

```
from kosa8 import Kosa8Cloud

cloud = Kosa8Cloud()

with cloud.sandboxes.create(template="python", ttl_s=900) as box:
    box.files.write("/workspace/hello.py", 'print("hello from a file")\n')
    print(box.exec(["python3", "/workspace/hello.py"]).stdout)   # hello from a file
    print(box.files.read_text("/workspace/hello.py"))
    for f in box.files.list("/workspace"):
        print(f["name"], f["size"])
```

**JavaScript**

```
import { Kosa8Cloud } from '@kosa8/sdk';

const cloud = new Kosa8Cloud();
const box = await cloud.sandboxes.create({ template: 'python', ttlS: 900 });
try {
  await box.files.write('/workspace/hello.py', 'print("hello from a file")\n');
  console.log((await box.exec(['python3', '/workspace/hello.py'])).stdout); // hello from a file
  console.log(await box.files.readText('/workspace/hello.py'));
  for (const f of await box.files.list('/workspace')) console.log(f.name, f.size);
} finally {
  await box.delete();
}
```

## The calls

| Call | Does |
|---|---|
| `PUT /v1/sandboxes/{id}/files?path=…` | Write a file; the body is its bytes, streamed (100 MB per request; larger files go in parts or by direct link) |
| `GET /v1/sandboxes/{id}/files?path=…` | Read it back, streamed; `Range` headers work |
| `GET /v1/sandboxes/{id}/files/list?path=…` | List a folder (default `/workspace`) as `{"files": [...]}` |
| `POST /v1/sandboxes/{id}/files/mkdir` | Make a folder and its parents |
| `DELETE /v1/sandboxes/{id}/files?path=…` | Remove a file, or a folder with `recursive=1` |

The exact parameters are in the [API reference](https://docs.kosa8.com/api/sandbox-files/). For files of many megabytes, appends, offsets and direct upload links, see [terminal and large files](https://docs.kosa8.com/guides/cloud-terminal-files/).

## Tips

- To send a whole project, tar it, put one file, and untar it with one exec. That is far faster than one call per file.

- Results you want to keep must be read out before the sandbox is deleted, or saved in a [snapshot](https://docs.kosa8.com/concepts/snapshots/).

---
Source: https://docs.kosa8.com/concepts/files/
