Appearance
File System API Reference
Related Tutorial
- File Operations Guide - Complete guide to file system operations
Overview
The File System module provides comprehensive file and directory operations within the AGB cloud environment. It supports file upload, download, and manipulation.
FileChangeEvent
python
class FileChangeEvent()Represents a single file change event.
FileInfoResult
python
class FileInfoResult(ApiResponse)Result of file info operations.
DirectoryListResult
python
class DirectoryListResult(ApiResponse)Result of directory listing operations.
FileContentResult
python
class FileContentResult(ApiResponse)Result of file read operations.
MultipleFileContentResult
python
class MultipleFileContentResult(ApiResponse)Result of multiple file read operations.
FileSearchResult
python
class FileSearchResult(ApiResponse)Result of file search operations.
FileChangeResult
python
class FileChangeResult(ApiResponse)Result of file change detection operations.
has_changes
python
def has_changes() -> boolCheck if there are any file changes.
get_modified_files
python
def get_modified_files() -> List[str]Get list of modified file paths.
get_created_files
python
def get_created_files() -> List[str]Get list of created file paths.
get_deleted_files
python
def get_deleted_files() -> List[str]Get list of deleted file paths.
FileSystem
python
class FileSystem(BaseService)FileSystem provides file system operations for the session.
transfer_path
python
def transfer_path() -> Optional[str]Get the path for file transfer operations.
This method ensures the context ID is loaded and returns the associated context path that was retrieved from GetAndLoadInternalContext API.
Returns:
Optional[str]: The transfer path if available, None otherwise.
upload
python
def upload(
local_path: str,
remote_path: str,
*,
content_type: Optional[str] = None,
wait: bool = True,
wait_timeout: float = 30.0,
poll_interval: float = 1.5,
progress_cb: Optional[Callable[[int], None]] = None) -> UploadResultUpload a file from local to remote path using pre-signed URLs.
Arguments:
local_path: Local file path to upload
remote_path: Remote file path to upload to
content_type: Optional content type for the file
wait: Whether to wait for the sync operation to complete
wait_timeout: Timeout for waiting for sync completion
poll_interval: Interval between polling for sync completion
progress_cb: Callback for upload progress updates
Returns:
UploadResult: Result of the upload operation
Example:
python
remote_path = session.file.transfer_path() + "/file.txt"
upload_result = session.file.upload("/local/file.txt", remote_path)download
python
def download(
remote_path: str,
local_path: str,
*,
overwrite: bool = True,
wait: bool = True,
wait_timeout: float = 30.0,
poll_interval: float = 1.5,
progress_cb: Optional[Callable[[int], None]] = None) -> DownloadResultDownload a file from remote path to local path using pre-signed URLs.
Arguments:
remote_path: Remote file path to download from
local_path: Local file path to download to
overwrite: Whether to overwrite existing local file
wait: Whether to wait for the sync operation to complete
wait_timeout: Timeout for waiting for sync completion
poll_interval: Interval between polling for sync completion
progress_cb: Callback for download progress updates
Returns:
DownloadResult: Result of the download operation
Example:
python
remote_path = session.file.transfer_path() + "/file.txt"
download_result = session.file.download(remote_path, "/local/file.txt")DEFAULT_CHUNK_SIZE
python
DEFAULT_CHUNK_SIZE = 50 * 1024mkdir
python
def mkdir(path: str) -> BoolResultCreate a new directory at the specified path.
Arguments:
pathstr - The path of the directory to create.
Returns:
BoolResult: Result object containing success status and error message if
any.
edit
python
def edit(path: str,
edits: List[Dict[str, str]],
dry_run: bool = False) -> BoolResultEdit a file by replacing occurrences of oldText with newText.
Arguments:
pathstr - The path of the file to edit.editsList[Dict[str, str]] - A list of dictionaries specifying oldText and newText.dry_runbool - If True, preview changes without applying them. Defaults to False.
Returns:
BoolResult: Result object containing success status and error message if
any.
info
python
def info(path: str) -> FileInfoResultGet information about a file or directory.
Arguments:
pathstr - The path of the file or directory to inspect.
Returns:
FileInfoResult: Result object containing file info and error message if any.
list
python
def list(path: str) -> DirectoryListResultList the contents of a directory.
Arguments:
pathstr - The path of the directory to list.
Returns:
DirectoryListResult: Result object containing directory entries and error
message if any.
move
python
def move(source: str, destination: str) -> BoolResultMove a file or directory from source path to destination path.
Arguments:
sourcestr - The source path of the file or directory.destinationstr - The destination path.
Returns:
BoolResult: Result object containing success status and error message if
any.
remove
python
def remove(path: str) -> BoolResultDelete a file at the specified path.
Arguments:
pathstr - The path of the file to delete.
Returns:
BoolResult: Result object containing success status and error message if any.
Example:
python
session = (agb.create()).session
session.file.write("/tmp/to_delete.txt", "hello")
delete_result = session.file.remove("/tmp/to_delete.txt")
session.delete()read
python
@overload
def read(path: str) -> FileContentResultread
python
@overload
def read(path: str, *, format: Literal["text"]) -> FileContentResultread
python
@overload
def read(path: str, *, format: Literal["bytes"]) -> BinaryFileContentResultread
python
def read(
path: str,
*,
format: str = "text"
) -> Union[FileContentResult, BinaryFileContentResult]Read the contents of a file. Automatically handles large files by chunking.
Arguments:
pathstr - The path of the file to read.formatstr - Format to read the file in. "text" (default) or "bytes".- "text": Returns FileContentResult with content as string (UTF-8)
- "bytes": Returns BinaryFileContentResult with content as bytes
Returns:
FileContentResult: For text format, contains file content as string.
BinaryFileContentResult: For bytes format, contains file content as bytes.
Raises:
FileError: If the file does not exist or is a directory.
Example:
python
session = (agb.create()).session
# Read text file (default)
text_result = session.file.read("/tmp/test.txt")
print(text_result.content) # str
# Read binary file
binary_result = session.file.read("/tmp/image.png", format="bytes")
print(binary_result.content) # bytes
session.delete()Notes:
- Automatically handles large files by reading in chunks (default 50KB per chunk)
- Returns empty string/bytes for empty files
- Returns error if path is a directory
- Binary files are returned as bytes (backend uses base64 encoding internally)
See Also:
FileSystem.write, FileSystem.list, FileSystem.info
write
python
def write(path: str, content: str, mode: str = "overwrite") -> BoolResultWrite content to a file. Automatically handles large files by chunking.
Arguments:
pathstr - The path of the file to write.contentstr - The content to write to the file.modestr - The write mode ("overwrite" or "append"). Defaults to "overwrite".
Returns:
BoolResult: Result object containing success status and error message if
any.
read_batch
python
def read_batch(paths: List[str]) -> MultipleFileContentResultRead the contents of multiple files at once.
Arguments:
pathsList[str] - A list of file paths to read.
Returns:
MultipleFileContentResult: Result object containing a dictionary mapping
file paths to contents, and error message if any.
search
python
def search(path: str,
pattern: str,
exclude_patterns: Optional[List[str]] = None) -> FileSearchResultSearch for files in the specified path using a pattern.
Arguments:
pathstr - The base directory path to search in.patternstr - The glob pattern to search for.exclude_patternsOptional[List[str]] - Optional list of patterns to exclude from the search. Defaults to None.
Returns:
FileSearchResult: Result object containing matching file paths and error
message if any.
watch_dir
python
def watch_dir(
path: str,
callback: Callable[[List[FileChangeEvent]], None],
interval: float = 1.0,
stop_event: Optional[threading.Event] = None) -> threading.ThreadWatch a directory for file changes and call the callback function when changes occur.
Arguments:
pathstr - The directory path to monitor for file changes.callbackCallable[[List[FileChangeEvent]], None] - Callback function that will be called with a list of FileChangeEvent objects when changes are detected.intervalfloat - Polling interval in seconds. Defaults to 1.0.stop_eventOptional[threading.Event] - Optional threading.Event to stop the monitoring. If not provided, a new Event will be created and returned via the thread object. Defaults to None.
Returns:
threading.Thread: The monitoring thread. Call thread.start() to begin monitoring.
Use the thread's stop_event attribute to stop monitoring.
Best Practices
- Always check file permissions before operations
- Use appropriate file paths and handle path separators correctly
- Clean up temporary files after operations
- Handle file operation errors gracefully
- Use streaming for large file operations
Related Resources
Documentation generated automatically from source code using pydoc-markdown.