Edit files in place
Change a few bytes of a large file without downloading or re-uploading it. Replace, insert, cut, append or truncate in one call, saved as a new version, safe against concurrent saves.
這一頁還沒有翻譯,所以先以英文顯示。
Agents and scripts mostly make small changes to big files: a line in a long script, a timecode in a subtitle file, a field in a manifest, a line at the end of a log. With S3 that means downloading the whole object, changing it and uploading it all again. In Side Space you send only the change:
read -r NODE HEAD < <(curl -s -H "Authorization: Bearer $SIDESPACE_TOKEN" \
"$SIDESPACE_URL/v1/space/paths/Ep01/script.md" | jq -r '"\(.id) \(.head_version_id)"') # the file's id and the version you read
curl -X POST -H "Authorization: Bearer $SIDESPACE_TOKEN" -H "Content-Type: application/json" \
-H "If-Match: \"$HEAD\"" \
"$SIDESPACE_URL/v1/space/nodes/$NODE/edits" \
-d '{"ops": [{"at": 14, "delete": 5, "text": "DAWN"}]}'
# {"version": {"id": "ver_01M7GX04…", "seq": 3, "size": 36, …}, "node": {…}}The result is a new version of the file, with the old one kept in its history. The server stores only the chunks near your change and reuses the rest, so the bytes stored for a 1 MiB change to a 1 GiB file are about the chunks it touches, not another gigabyte.
Operations
Every op replaces delete bytes starting at offset at with text (or base64 for binary bytes). The common edits are special cases:
| You want to | Op |
|---|---|
| Replace 5 bytes at offset 14 | {"at": 14, "delete": 5, "text": "DAWN"} |
| Insert at offset 2048 | {"at": 2048, "text": "\nINT. HARBOR"} |
| Cut 100 bytes | {"at": 512, "delete": 100} |
| Append | {"at": <size>, "text": "FADE OUT.\n"} |
| Write binary bytes | {"at": 4096, "delete": 4, "base64": "AAECAw=="} |
| Truncate | "truncate": 1024 next to ops (applied last) |
- One request can carry many ops. Offsets all count in the version you edit, before any of your changes, and ranges must not overlap.
- At most 10,000 ops and 4 MiB of new data per JSON request. Bigger changes, up to 64 MiB, go as the binary body.
Large and binary edits
For raw bytes or more than 4 MiB, send the edits as Content-Type: application/vnd.sidespace.patch. There is no base64, so nothing grows by a third, and one request can carry up to 64 MiB of new data. The body is SSP1, then for each edit three unsigned 64-bit big-endian integers (at, delete, the data's length) followed by the data. truncate and if_version_id go in the query (or use If-Match).
import struct, requests
body = b"SSP1" + struct.pack(">QQQ", 4096, 4, 4) + b"\x00\x01\x02\x03"
requests.post(f"{url}/v1/space/nodes/{node}/edits?if_version_id={ver}",
headers={"Authorization": f"Bearer {token}", "Content-Type": "application/vnd.sidespace.patch"},
data=body)The SDKs do this for you: give an op raw bytes (bytes in TypeScript and Python, Bytes in Go, bytes in Rust), or more than 4 MiB of data, and they send the binary body.
In each SDK
const saved = await side.edit("/Ep01/script.md", [{ at: 14, delete: 5, text: "DAWN" }], { ifVersionId: "ver_01M7GWV8…" });
await side.appendText("/Ep01/log.txt", "09:41 pick-ups done\n");saved = side.edit("/Ep01/script.md", [{"at": 14, "delete": 5, "text": "DAWN"}], if_version_id="ver_01M7GWV8…")
side.append_text("/Ep01/log.txt", "09:41 pick-ups done\n")saved, err := side.Edit(ctx, "/Ep01/script.md",
[]sidespace.EditOp{sidespace.Replace(14, 5, "DAWN")},
&sidespace.EditOptions{IfVersionID: "ver_01M7GWV8…"})
_, err = side.AppendText(ctx, "/Ep01/log.txt", "09:41 pick-ups done\n")use sidespace::{EditOp, EditOptions};
let guard = EditOptions { if_version_id: Some("ver_01M7GWV8…".into()), ..Default::default() };
let saved = side.edit("/Ep01/script.md", &[EditOp::replace(14, 5, "DAWN")], &guard).await?;
side.append_text("/Ep01/log.txt", "09:41 pick-ups done\n").await?;Go also has sidespace.Insert(at, text) and sidespace.Cut(at, n); Rust has EditOp::insert and EditOp::cut.
Over MCP, Agents get the same thing in text form: edit_text replaces an exact piece of text (it must be unique, or pass replace_all) and append_text adds to the end, so Claude can fix a line of a long script without rewriting it.
Safe against concurrent saves
Offsets only make sense in the version you read. Send that version as If-Match: "ver_…" (or if_version_id in the body, or the SDK option). If anyone saved since, nothing changes and the answer is 412:
{"error": {"code": "precondition_failed",
"message": "script.md changed since ver_01M7GWV8… (now ver_01M7GX04…)",
"details": {"current_version_id": "ver_01M7GX04…", "node_id": "nod_…"}}}Read the file again at current_version_id, work out the offsets again and retry. Without If-Match the edit applies to whatever is current when it arrives, and a save that lands while it runs still makes it fail rather than mixing the two. Retrying an unguarded insert or cut can apply it twice, so guard every edit you might retry.
How much it saves
An edit is rebuilt next to the data: the block gateway reads, re-chunks and stores only the chunks near the change, so its cost hardly depends on the file's size. Measured against uploading the file again to a plain bucket:
| Change | Upload it again | Edit in place |
|---|---|---|
| 4 KiB in the middle of a 64 MiB file | 326 ms | 18.5 ms |
| 4 KiB in the middle of a 1 GiB file | 2.9 s | 108 ms |
| Append a line to a 64 MiB log | 323 ms | 37 ms |
For a file of a few KiB there's little to gain: an edit looks the file up first, so a whole save is about as fast. The method and every result are on the performance page.