Storage¶
commons.storage
¶
A file storage abstraction with local, SQL and Azure Blob Storage backends.
- Files (
Files,File): files by key, an absolute slugified path such as/cases/123/report.pdf;find,get,list(every file below a path, recursively),putanddelete. - Directories (
Files.list_directory,DirectoryListing,Directory): the directories and files directly below a path, e.g. for a file browser. Directories are virtual: one exists as long as a file is stored below it, so there are no empty directories and nothing to create or delete. - Metadata (
File.size,File.last_modified): known by every backend without reading the file. Metadata with a domain meaning (title, owner, tags, permissions, content type) belongs to the application, linked by the key. - Backends (
LocalFiles,SQLFiles,AzureFiles) andFilesWithPrefix, which puts every key below a prefix, e.g. one per tenant.SQLFilestables created before commons 1.4 have no columnlast_modifiedand aren't migrated: drop the table or add the column by hand.
Modules:
| Name | Description |
|---|---|
azure |
|
files |
|
local |
|
prefix |
|
sql |
|
Classes:
| Name | Description |
|---|---|
AzureFile |
Lazy-loading Azure Blob Storage file. |
AzureFiles |
Azure Blob Storage implementation of Files. |
Directory |
A virtual directory: it exists as long as at least one file is stored below it. |
DirectoryListing |
The directories and files directly below a path, each sorted by key. |
File |
|
Files |
|
LocalFile |
Local file system implementation of File. |
LocalFiles |
Local file system implementation of Files. |
FilesWithPrefix |
Delegate that transparently prepends a path prefix to all file operations. |
SQLFile |
SQL implementation of File. |
SQLFiles |
SQL implementation of Files for storing binary files in a database table. |
__all__
module-attribute
¶
__all__ = [
"AzureFile",
"AzureFiles",
"Directory",
"DirectoryListing",
"File",
"Files",
"FilesWithPrefix",
"LocalFile",
"LocalFiles",
"SQLFile",
"SQLFiles",
]
AzureFile
¶
AzureFile(
key: str,
container_client: ContainerClient,
*,
size: int | None = None,
last_modified: datetime | None = None
)
Bases: File
Lazy-loading Azure Blob Storage file.
The blob bytes are not fetched until open() is called. size and last_modified come from the listing
or lookup that created the file; if they weren't given, the blob's properties are fetched once on first access.
Methods:
| Name | Description |
|---|---|
open |
|
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
|
size |
int
|
|
last_modified |
datetime
|
|
Source code in src/commons/storage/azure.py
AzureFiles
¶
AzureFiles(
settings: AzureStorageSettings | None = None,
*,
container_client: ContainerClient | None = None
)
Bases: Files
Azure Blob Storage implementation of Files.
Maps each Files key (e.g. /foo/bar.txt) to a blob name in a single
Azure Storage container (e.g. foo/bar.txt). Multi-tenant prefixing is
handled by :class:commons.storage.FilesWithPrefix.
Methods:
| Name | Description |
|---|---|
find |
|
list |
|
list_directory |
|
Source code in src/commons/storage/azure.py
list
¶
list_directory
¶
list_directory(path: str = '/') -> DirectoryListing
Source code in src/commons/storage/azure.py
Directory
dataclass
¶
DirectoryListing
dataclass
¶
File
¶
Bases: ABC
Methods:
| Name | Description |
|---|---|
get |
|
open |
|
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
|
size |
int
|
The size of the file in bytes. |
last_modified |
datetime
|
When the file was last written, with time zone (UTC). |
size
property
¶
The size of the file in bytes.
The backends of commons know it without reading the file; this default reads the content.
last_modified
property
¶
When the file was last written, with time zone (UTC).
Every backend of commons provides it. It isn't abstract, so subclasses outside commons that don't implement
it keep working until it is read; this default raises NotImplementedError.
get
¶
Files
¶
Bases: ABC
Methods:
| Name | Description |
|---|---|
find |
Search a file by its key. Returns None if file doesn't exist. |
get |
Get a file by its key. Throws error if file does not exist. |
list |
Get all lists which are below provided path. |
list_directory |
Get the directories and files directly below the provided path, not the ones further down. |
put |
Adds or overwrites a file. |
delete |
Delete a file or files by key. |
list
abstractmethod
¶
Get all lists which are below provided path.
Only full path segments match, e.g. we have the following files
list("/foo") returns
Source code in src/commons/storage/files.py
list_directory
¶
list_directory(path: str = '/') -> DirectoryListing
Get the directories and files directly below the provided path, not the ones further down.
Directories are virtual: a directory is listed if at least one file is stored below it, at any depth.
Only full path segments match, as in list. /foo and /foo/ are the same path; a path with nothing
below it (a file key or a missing key) returns an empty listing.
With the files
list_directory("/foo") returns the directory /foo/bar and the file /foo/a.txt.
Source code in src/commons/storage/files.py
put
¶
Adds or overwrites a file.
Source code in src/commons/storage/files.py
delete
¶
Delete a file or files by key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The file key or path prefix to delete. |
required |
recursive
|
bool
|
If True, deletes all files under the given path prefix. If False, only deletes the exact file matching the key. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
The number of files deleted. |
Source code in src/commons/storage/files.py
LocalFile
¶
Bases: File
Local file system implementation of File.
Methods:
| Name | Description |
|---|---|
open |
|
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
|
size |
int
|
|
last_modified |
datetime
|
|
Source code in src/commons/storage/local.py
LocalFiles
¶
Bases: Files
Local file system implementation of Files.
Methods:
| Name | Description |
|---|---|
find |
|
list |
|
list_directory |
|
Source code in src/commons/storage/local.py
list
¶
Source code in src/commons/storage/local.py
list_directory
¶
list_directory(path: str = '/') -> DirectoryListing
Source code in src/commons/storage/local.py
FilesWithPrefix
¶
FilesWithPrefix(inner: Files, prefix: str)
Bases: Files
Delegate that transparently prepends a path prefix to all file operations.
Methods:
| Name | Description |
|---|---|
find |
|
list |
|
list_directory |
|
Source code in src/commons/storage/prefix.py
list
¶
list_directory
¶
list_directory(path: str = '/') -> DirectoryListing
Source code in src/commons/storage/prefix.py
SQLFile
¶
Bases: File
SQL implementation of File.
Methods:
| Name | Description |
|---|---|
open |
|
Attributes:
| Name | Type | Description |
|---|---|---|
key |
str
|
|
size |
int
|
|
last_modified |
datetime
|
|
Source code in src/commons/storage/sql.py
SQLFiles
¶
Bases: Files
SQL implementation of Files for storing binary files in a database table.
The table files has the columns key, data and last_modified. A table created by a version of commons
before 1.4 has no column last_modified; it isn't migrated, so drop it or add the column by hand.
Methods:
| Name | Description |
|---|---|
find |
|
list |
|