Storage
Full attribute and method reference for the file storage backends, generated from docstrings. For a task-oriented walkthrough, see File Storage.
starlette_admin.storage.base.FileInfo
dataclass
Metadata describing a stored file. This (as a dict, via [to_dict][starlette_admin.storage.FileInfo.to_dict]) is what file fields persist in the database column.
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
str
|
Original (sanitised) filename, for display. |
content_type |
str
|
MIME type reported at upload time. |
size |
int
|
File size in bytes. |
storage |
str
|
Name of the storage backend holding the file. |
key |
str
|
Storage-relative path / object key. |
url |
str
|
Public-facing URL. Refreshed on every render by
|
uploaded_at |
datetime | None
|
UTC timestamp of the upload. |
width |
int | None
|
Image width in pixels ( |
height |
int | None
|
Image height in pixels ( |
thumbnail |
dict | None
|
Thumbnail metadata ( |
extra |
dict
|
Free-form backend-specific metadata. |
Source code in starlette_admin/storage/base.py
from_dict(data)
classmethod
Rebuild a FileInfo from its stored dict form.
Source code in starlette_admin/storage/base.py
to_dict()
JSON-serializable dict form, suitable for a database JSON column.
Source code in starlette_admin/storage/base.py
starlette_admin.storage.base.BaseStorage
Bases: ABC
Base class for file storage backends.
Each instance registers itself under its name so that stored values
(which carry only the storage name in their metadata) can be resolved
back to the backend that holds them. See
get_storage.
Source code in starlette_admin/storage/base.py
delete(key)
abstractmethod
async
Remove the file identified by key. Must not fail if the file is
already gone.
read(key)
async
Return the full contents of the file identified by key as bytes.
Used by the export system to embed files into ZIP archives. Backends
whose files live on an external host (e.g. S3) must override this;
the default raises NotImplementedError so callers can skip the file
gracefully rather than crashing the export.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
for backends that have not implemented this. |
FileNotFoundError
|
if the file does not exist at |
Source code in starlette_admin/storage/base.py
save(upload, dest)
abstractmethod
async
Persist upload under the (sanitised, storage-relative) path
dest and return the resulting FileInfo.
Implementations must never overwrite an existing file: dest is
uniquified when taken.
Source code in starlette_admin/storage/base.py
serve(request, key)
async
Serve the file identified by key over HTTP. Backs the admin's
/files/{storage}/{path} route; backends whose URLs point at an
external host don't need to implement it.
Source code in starlette_admin/storage/base.py
url(request, key, *, signed=False, expires=3600)
abstractmethod
async
Return a public-facing URL for key. Called on every page render;
signed URLs are never stored.
Source code in starlette_admin/storage/base.py
starlette_admin.storage.local.LocalStorage
Bases: BaseStorage
Filesystem storage rooted at base_dir.
Files are served exclusively through the admin's /_files/{storage}/{path}
route; no external StaticFiles mount is needed or supported.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_dir
|
str | Path
|
Directory under which all files are stored (created if absent). |
required |
name
|
str | None
|
Registry name for this instance. Override it when using several
|
None
|
Source code in starlette_admin/storage/local.py
starlette_admin.storage.s3.S3Storage
Bases: BaseStorage
AWS S3 (and compatible) storage backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bucket
|
str
|
S3 bucket name. |
required |
prefix
|
str
|
Key prefix for every stored object (acts like a folder). |
'uploads/'
|
region
|
str
|
AWS region (used for URL construction and signing). |
'us-east-1'
|
access_key
|
str | None
|
AWS access key ID. Falls back to the standard boto3 credential chain (env vars, shared credentials file, IAM role). |
None
|
secret_key
|
str | None
|
AWS secret access key. Falls back to the credential chain. |
None
|
public
|
bool
|
When |
True
|
expires
|
int
|
Lifetime of pre-signed URLs in seconds (default 3600 = 1 h).
Only used when |
3600
|
endpoint_url
|
str | None
|
Override the S3 endpoint URL for MinIO / R2 / Backblaze. |
None
|
name
|
str | None
|
Registry name for this instance. Override it when using several
|
None
|
Source code in starlette_admin/storage/s3.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
read(key)
async
Return the object's bytes from S3.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
if the object does not exist at |
Source code in starlette_admin/storage/s3.py
serve(request, key)
async
Redirect to the bucket URL; S3 files are served directly from there.
Source code in starlette_admin/storage/s3.py
starlette_admin.storage.base.UnknownStorageError
starlette_admin.storage.base.register_storage(storage)
Register storage under its name (replacing any previous instance).
Source code in starlette_admin/storage/base.py
starlette_admin.storage.base.get_storage(name)
Resolve a storage name (as stored in FileInfo.storage) back to its
registered instance.
Raises:
| Type | Description |
|---|---|
UnknownStorageError
|
if no storage with that name has been instantiated. |
Source code in starlette_admin/storage/base.py
starlette_admin.storage.base.secure_filename(filename)
Return a sanitised version of an (untrusted) uploaded filename:
path components are stripped and anything outside [A-Za-z0-9_.-]
is collapsed to _.
Source code in starlette_admin/storage/base.py
starlette_admin.storage.base.delete_stored_files(value)
async
Delete the file(s) referenced by a stored file-field value: a
FileInfo, its dict form, or a list of either. Values referencing an
unregistered storage (or of any other shape) are skipped silently.
A thumbnail nested under thumbnail.key (ImageField with
thumbnail_size) is deleted alongside the main file, from the same
storage.