Storage¶
django-snapshots uses two stacked protocols for storage. Third-party backends use structural subtyping — no inheritance from the protocol class is required.
Protocols¶
- class django_snapshots.SnapshotStorage(*args, **kwargs)[source]¶
Minimum storage interface.
readandwriteuseIO[bytes]file-like objects to avoid loading entire artifacts into memory.list(prefix)returns all stored paths whose full path string starts with prefix. Pass""to list everything.- __init__(*args, **kwargs)¶
- class django_snapshots.AdvancedSnapshotStorage(*args, **kwargs)[source]¶
Extended storage interface with streaming and atomic operations.
Required for
snapshot_format="archive"and for future incremental backup support.
Guard function¶
- django_snapshots.storage.protocols.requires_advanced_storage(backend: SnapshotStorage, operation: str) None[source]¶
Raise
SnapshotStorageCapabilityErrorif backend is not anAdvancedSnapshotStorage.Call this at the start of any function that requires the extended interface.
Built-in backends¶
LocalFileSystemBackend¶
The default backend. Implements the full AdvancedSnapshotStorage
interface. All paths are relative to the configured location directory, which is
created automatically if it does not exist.
Use this backend for local development and single-server deployments.
from django_snapshots.storage import LocalFileSystemBackend
storage = LocalFileSystemBackend(location="/var/backups/snapshots")
- class django_snapshots.LocalFileSystemBackend(location: str)[source]¶
Store snapshots as files in a local directory.
Satisfies
AdvancedSnapshotStorage— the default backend is never subject to OOM on large artifacts.- Args:
- location: Absolute path to the root directory for snapshot storage.
Created automatically if it does not exist.
DjangoStorageBackend¶
Wraps any django.core.files.storage.Storage instance to satisfy the
SnapshotStorage basic protocol. Does not satisfy
AdvancedSnapshotStorage.
Use this backend when you already have a configured Django storage (e.g.
django-storages S3 backend) and only need basic upload/download.
from django.core.files.storage import FileSystemStorage
from django_snapshots.storage import DjangoStorageBackend
storage = DjangoStorageBackend(storage=FileSystemStorage(location="/tmp/snaps"))
- class django_snapshots.DjangoStorageBackend(storage: Storage)[source]¶
Wrap any Django
Storagebackend as aSnapshotStorage.Satisfies
SnapshotStoragevia structural subtyping. Does not satisfyAdvancedSnapshotStorage— useLocalFileSystemBackendwhen streaming or atomic operations are required.- Args:
storage: Any
django.core.files.storage.Storageinstance.
Writing a custom backend¶
Implement all methods of SnapshotStorage (or
AdvancedSnapshotStorage) on any class. No base
class is needed — Python’s structural subtyping will recognise it automatically:
from typing import IO, Iterator
class MyS3Backend:
def read(self, path: str) -> IO[bytes]: ...
def write(self, path: str, content: IO[bytes]) -> None: ...
def list(self, prefix: str) -> list[str]: ...
def delete(self, path: str) -> None: ...
def exists(self, path: str) -> bool: ...
# Add the five AdvancedSnapshotStorage methods to satisfy that tier too.