await) and return promises/futures. The storage system can notify clients when files are created, updated, or deleted via events, enabling real-time synchronization or UI updates.
Overview
The storage functionality provides a straightforward way to check if files exist, create or overwrite them, append data in multiple writes, download or get a direct URL to a file, list folder contents, and finally delete files when they are no longer needed. Key concepts include:- Async Methods: All I/O methods are asynchronous and return futures/promises.
- Handles: Opening a file returns an object you can use for writing data and eventually closing.
- Events: The storage system can emit events whenever a file is updated or deleted.
Events
The storage system emits two types of events:file.updated
Triggered when a file is created or updated. Handlers receive apathargument identifying the file’s location in storage.
file_deleted
Triggered when a file is deleted. Handlers receive apathargument identifying the file’s location in storage.
API Methods
Below is a summary of the primary methods. Each method is asynchronous, so you shouldawait the call.
1. exists(path)
DescriptionChecks if a file or folder exists at the given path. Parameters
path(str): The path to check.
- (bool):
Trueif the file or folder exists;Falseotherwise.
2. open(path, overwrite=False)
DescriptionOpens a file for writing. Returns a handle used for subsequent write calls and closing.
If
overwrite is True, an existing file at this path will be truncated.
Parameters
path(str): The file path to open or create.overwrite(bool, optional): Whether to overwrite if the file already exists. Defaults toFalse.
- Handle Object: An object representing an open file, usable with
writeandclose.
3. write(handle, data)
DescriptionWrites binary data to an open file handle. Parameters
handle(file handle): The handle returned byopen.data(bytes): The data to write.
None
4. close(handle)
DescriptionCloses an open file handle, ensuring all data has been written. Parameters
handle(file handle): The handle to close.
None
5. download(path)
DescriptionRetrieves the content of a file from the remote storage. Parameters
path(str): The file path to download.
- File-like Response: Contains the file’s raw data, typically accessible through a
.dataproperty.
6. download_url(path)
DescriptionRequests a downloadable URL for the specified file path, which can be used to fetch the file directly (e.g., via HTTP). The exact protocol or format of the returned URL may vary. Parameters
path(str): The file path to retrieve a download URL for.
- (str): A URL string you can fetch with your own HTTP or other suitable client.
7. list(path)
DescriptionLists the contents of a folder, returning file and subfolder names along with a flag indicating if each entry is a folder. Parameters
path(str): The folder path to list.
- (list): A list of entries, each containing a
nameandis_folderproperty.
8. delete(path)
DescriptionDeletes a file at the given path. A
file.deleted event is typically emitted afterward.
Parameters
path(str): The file path to delete.
None
Example Workflow
A common use case:- Check if a file exists.
- Create and write data if it doesn’t exist.
- Later, download the file to verify or use the data.
- Delete the file when it’s no longer needed, reacting to the
file.deletedevent.