Facts read
How do I upload a large file to my computer from my own code?
With program 0.1.18 on the computer and a current Python or Node package, one call sends a local file of up to 256 MiB to a computer you connected: uploadFile in TypeScript (Node) or upload_file in Python. It uploads in chunks, can continue after a lost reply, checks the whole-file SHA-256 at the end and never overwrites an existing file.
What you get
A file on the machine where your program runs lands on one of your connected computers, byte for byte: the computer checks the whole-file hash before it creates the file. Your program sends binary data in chunks; nothing goes through one huge call or base64 pasted into a prompt.
What you need
- Program 0.1.18 or later on the computer that receives the file.
- A current Python or Node package. The docs page below states the minimum version that has the upload helper.
- An agent token with
writeswitched on, by you. A new agent can only read. CMDOP_URLandCMDOP_TOKENset in your program's environment, as in the first program from code.
The code
The example reads ./export.zip on the computer running your program. Replace <machine> with your computer's name and /<destination-folder>/export.zip with a path that does not exist yet. It saves a fresh transfer id to upload-id.txt before the first call, so you can recover after a disconnect.
import os
import secrets
import cmdop
transfer = secrets.token_hex(16)
with open("./upload-id.txt", "x", encoding="utf-8") as checkpoint:
checkpoint.write(transfer + "\n")
checkpoint.flush()
os.fsync(checkpoint.fileno())
with cmdop.connect() as client:
result = cmdop.upload_file(
client.machine("<machine>"),
"./export.zip",
"/<destination-folder>/export.zip",
transfer=transfer,
)
if result.receipt is None:
raise RuntimeError("No committed file receipt.")
print("committed")In Node, uploadFile takes the same three arguments and { transfer }; the TypeScript version of this example is on the docs page.
It prints committed only after the computer returns a verified receipt: the destination path, its size, its SHA-256 and that it was created. Python also has upload_file_async, and the helpers in both languages accept a progress callback.
How it works
The helper begins an upload with an id, the size and the SHA-256, sends chunks of at most 1 MiB, then commits. If a reply is lost, fs_upload_status returns the offset the computer confirmed and the helper continues from there. At commit the computer checks the size and the whole-file hash, then creates the destination. If a file already exists there, the upload is refused. An upload expires 15 minutes after it begins.
Check what it did
Each upload call is recorded under the agent's name on the computer's own record, with the time. Give each program its own token, so the record says which one wrote the file.
Limits
- Up to 256 MiB per file. Each computer runs at most 4 uploads at a time and reserves at most 512 MiB for them.
- The file comes from a path your program can read. It is not a browser upload, and a file inside a ChatGPT conversation is not a local path.
- Create-only: an existing destination is refused, never replaced.
- Without
writeswitched on for that agent, the call is refused and no file is created. - macOS and Linux computers; on macOS it is supported by the shipped machine. Windows is not supported yet.
