Files
Upload, download, list and remove files in a sandbox through cua-spacesd.
Upload, download, list and remove files in a sandbox through cua-spacesd.
Paths are guest paths unless a parameter names a local one. Transfers are chunked, so size is not capped.
Python programs usually use the high-level API instead: sb.files.
Methods of SpacesdClient.
SpacesdClient.upload#Uploads bytes to path (chunked, resumable, SHA-256 verified).
async def upload(self, path: str, data: bytes, options: Optional[UploadOptions]) -> TransferResult| Parameter | Type | Default |
|---|---|---|
path | String | required |
data | Vec<u8> | required |
options | Option<UploadOptions> | required |
Returns TransferResult · Async · Raises CuaError
Example
import asyncio
import cua
async def main():
guest = await cua.embedded().spacesd(URL, TOKEN)
sent = await guest.upload("/tmp/note.txt", b"hi from the host", None)
back = await guest.download("/tmp/note.txt")
print([sent.size, back])
# [16, b'hi from the host']
asyncio.run(main())SpacesdClient.upload_file#Uploads a local file to path.
async def upload_file(self, local_path: str, path: str, options: Optional[UploadOptions]) -> TransferResult| Parameter | Type | Default |
|---|---|---|
local_path | String | required |
path | String | required |
options | Option<UploadOptions> | required |
Returns TransferResult · Async · Raises CuaError
SpacesdClient.download#Downloads path into memory.
async def download(self, path: str) -> bytes| Parameter | Type | Default |
|---|---|---|
path | String | required |
Returns Vec<u8> · Async · Raises CuaError
SpacesdClient.download_file#Downloads path to a local file.
async def download_file(self, path: str, local_path: str) -> TransferResult| Parameter | Type | Default |
|---|---|---|
path | String | required |
local_path | String | required |
Returns TransferResult · Async · Raises CuaError
SpacesdClient.stat#Stat.
async def stat(self, path: str) -> FileEntry| Parameter | Type | Default |
|---|---|---|
path | String | required |
Returns FileEntry · Async · Raises CuaError
SpacesdClient.list_dir#ListDir (depth 1 = direct children).
async def list_dir(self, path: str, depth: int) -> List[FileEntry]| Parameter | Type | Default |
|---|---|---|
path | String | required |
depth | u32 | required |
Returns Vec<FileEntry> · Async · Raises CuaError
SpacesdClient.make_dir#MakeDir (with parents).
async def make_dir(self, path: str) -> FileEntry| Parameter | Type | Default |
|---|---|---|
path | String | required |
Returns FileEntry · Async · Raises CuaError
SpacesdClient.remove#Remove.
async def remove(self, path: str, recursive: bool) -> None| Parameter | Type | Default |
|---|---|---|
path | String | required |
recursive | bool | required |
Async · Raises CuaError
FileEntry record#A file or directory in the guest.
Returned by SpacesdClient.list_dir, SpacesdClient.make_dir, SpacesdClient.stat.
| Field | Type | Default | Description |
|---|---|---|---|
name | String | Base name. | |
path | String | Absolute path. | |
kind | String | file, directory, symlink or other. | |
size | u64 | Size in bytes. | |
mode | u32 | Unix mode bits. | |
modified_ms / modifiedMs | Option<i64> | Modification time (Unix ms), when known. | |
symlink_target / symlinkTarget | Option<String> | Symlink target. |
TransferResult record#Result of an upload or download.
Returned by SpacesdClient.download_file, SpacesdClient.upload, SpacesdClient.upload_file.
| Field | Type | Default | Description |
|---|---|---|---|
size | u64 | Bytes transferred. | |
sha256 | String | SHA-256 (hex) of the content. | |
resumes | u32 | Resumes after dropped connections. |
UploadOptions record#Upload options.
| Field | Type | Default | Description |
|---|---|---|---|
create_new / createNew | bool | false | Fail when the file exists (instead of overwriting). |
append | bool | false | Append instead of overwriting. |
permissions | u32 | 0 | Unix permissions (0 = default). |
create_parents / createParents | bool | true | Create missing parent directories. |