Files
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.
File methods#
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. user reads as that guest user (see
UploadOptions.user).
async def download(self, path: str, user: Optional[str] = None) -> bytes| Parameter | Type | Default |
|---|---|---|
path | String | required |
user | Option<String> | None |
Returns Vec<u8> · Async · Raises CuaError
SpacesdClient.download_file#
Downloads path to a local file. user reads as that guest user.
async def download_file(self, path: str, local_path: str, user: Optional[str] = None) -> TransferResult| Parameter | Type | Default |
|---|---|---|
path | String | required |
local_path | String | required |
user | Option<String> | None |
Returns TransferResult · Async · Raises CuaError
SpacesdClient.stat#
Stat. user acts as that guest user (see UploadOptions.user);
xattr_namespace picks the extended-attribute prefix reported in
FileEntry.metadata (default user.cua.meta.). no_follow reports
a symbolic link itself rather than its target.
async def stat(self, path: str, user: Optional[str] = None, xattr_namespace: Optional[str] = None, no_follow: bool = False) -> FileEntry| Parameter | Type | Default |
|---|---|---|
path | String | required |
user | Option<String> | None |
xattr_namespace | Option<String> | None |
no_follow | bool | false |
Returns FileEntry · Async · Raises CuaError
SpacesdClient.list_dir#
ListDir (depth 1 = direct children), as user.
async def list_dir(self, path: str, depth: int, user: Optional[str] = None, xattr_namespace: Optional[str] = None) -> List[FileEntry]| Parameter | Type | Default |
|---|---|---|
path | String | required |
depth | u32 | required |
user | Option<String> | None |
xattr_namespace | Option<String> | None |
Returns Vec<FileEntry> · Async · Raises CuaError
SpacesdClient.make_dir#
MakeDir (with parents), as user: the new directories are owned
by that user.
async def make_dir(self, path: str, user: Optional[str] = None) -> FileEntry| Parameter | Type | Default |
|---|---|---|
path | String | required |
user | Option<String> | None |
Returns FileEntry · Async · Raises CuaError
SpacesdClient.move_path#
Move: renames source to destination, as user. overwrite
replaces an existing destination (otherwise it fails with
AlreadyExists); create_parents makes the destination's missing
parent directories (owned by user).
async def move_path(self, source: str, destination: str, overwrite: bool = False, create_parents: bool = False, user: Optional[str] = None) -> FileEntry| Parameter | Type | Default |
|---|---|---|
source | String | required |
destination | String | required |
overwrite | bool | false |
create_parents | bool | false |
user | Option<String> | None |
Returns FileEntry · Async · Raises CuaError
SpacesdClient.remove#
Remove, as user.
async def remove(self, path: str, recursive: bool, user: Optional[str] = None) -> None| Parameter | Type | Default |
|---|---|---|
path | String | required |
recursive | bool | required |
user | Option<String> | None |
Async · Raises CuaError
FileEntry record#
A file or directory in the guest.
Returned by SpacesdClient.list_dir, SpacesdClient.make_dir, SpacesdClient.move_path, 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. | |
owner | Option<String> | Owning user name (a numeric id when the guest has no such user); None where the guest has no owners (Windows). | |
group | Option<String> | Owning group name; None where the guest has none (Windows). | |
metadata | HashMap<String, String> | Extended attributes under the call's xattr_namespace (default user.cua.meta.), without the prefix. Empty when there are none or the guest file system has no extended attributes. |
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. |
user | Option<String> | None | Guest user to write as: the file (and parents it creates) is owned by that user and the user's permissions apply. Needs a guest agent that runs as root or has passwordless sudo (the Linux images do); otherwise the call fails with a permission error that says why. |
metadata | Option<HashMap<String, String>> | None | Extended attributes to store with the file, published atomically with its content. At most 32 keys; a key is 1-128 bytes of A-Za-z0-9._:/@+-, a value at most 1024 bytes, 3072 bytes in all. |
xattr_namespace / xattrNamespace | Option<String> | None | Extended-attribute key prefix for metadata (and for the metadata reported back): must start with user. and end with .. Default user.cua.meta.. |