Storage
The storage API manages files and folders within a room’s remote storage. All interactions are asynchronous (using 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 apath
argument identifying the file’s location in storage.
file_deleted
Triggered when a file is deleted. Handlers receive apath
argument identifying the file’s location in storage.
You can remove an event handler with:
API Methods
Below is a summary of the primary methods. Each method is asynchronous, so you should await
the call.
1. exists(path)
Description
Checks if a file or folder exists at the given path.
Parameters
path
(str): The path to check.
Returns
- (bool):
True
if the file or folder exists;False
otherwise.
Example:
2. open(path, overwrite=False)
Description
Opens 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
.
Returns
- Handle Object: An object representing an open file, usable with
write
andclose
.
Example:
3. write(handle, data)
Description
Writes binary data to an open file handle.
Parameters
handle
(file handle): The handle returned byopen
.data
(bytes): The data to write.
Returns
None
Example:
4. close(handle)
Description
Closes an open file handle, ensuring all data has been written.
Parameters
handle
(file handle): The handle to close.
Returns
None
Example:
5. download(path)
Description
Retrieves the content of a file from the remote storage.
Parameters
path
(str): The file path to download.
Returns
- File-like Response: Contains the file’s raw data, typically accessible through a
.data
property.
Example:
6. download_url(path)
Description
Requests 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.
Returns
- (str): A URL string you can fetch with your own HTTP or other suitable client.
Example:
7. list(path)
Description
Lists 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.
Returns
- (list): A list of entries, each containing a
name
andis_folder
property.
Example:
8. delete(path)
Description
Deletes a file at the given path. A file.deleted
event is typically emitted afterward.
Parameters
path
(str): The file path to delete.
Returns
None
Example:
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.deleted
event.
This sequence demonstrates basic creation, reading, and deletion flows within a single session.
Conclusion
The storage interface offers asynchronous file operations, suitable for real-time collaboration scenarios. You can track file updates or deletions through event handlers and immediately sync or update your application. This design makes it easy to build experiences around shared data that are robust, flexible, and user-friendly.
Was this page helpful?