Async file storage abstraction library for Litestar
Project description
litestar-storages
Async file storage abstraction library for Litestar.
Features
- Async-native: Built from the ground up for async/await
- Type-safe: Full typing support with Protocol-based design
- Modular: Optional backend dependencies via extras
- Litestar Integration: First-class plugin support with dependency injection
Installation
# Core library (includes MemoryStorage only)
pip install litestar-storages
# With filesystem support
pip install litestar-storages[filesystem]
# With S3 support
pip install litestar-storages[s3]
# All backends
pip install litestar-storages[all]
Quick Start
Memory Storage (for testing)
from litestar_storages import MemoryStorage
storage = MemoryStorage()
# Store a file
stored = await storage.put("test.txt", b"hello world", content_type="text/plain")
# Retrieve it
data = await storage.get_bytes("test.txt")
print(data.decode()) # "hello world"
# Get a URL
url = await storage.url("test.txt") # "memory://test.txt"
Filesystem Storage
from pathlib import Path
from litestar_storages import FileSystemStorage, FileSystemConfig
storage = FileSystemStorage(
config=FileSystemConfig(
path=Path("/var/uploads"),
base_url="https://cdn.example.com/uploads",
)
)
await storage.put("images/photo.jpg", image_data)
url = await storage.url("images/photo.jpg")
# Returns: https://cdn.example.com/uploads/images/photo.jpg
S3 Storage
from litestar_storages import S3Storage, S3Config
# AWS S3
storage = S3Storage(
config=S3Config(
bucket="my-bucket",
region="us-east-1",
)
)
# Cloudflare R2
storage = S3Storage(
config=S3Config(
bucket="my-bucket",
endpoint_url="https://account.r2.cloudflarestorage.com",
access_key_id="...",
secret_access_key="...",
)
)
# Upload file
stored = await storage.put("uploads/file.pdf", file_data, content_type="application/pdf")
# Generate presigned URL (expires in 1 hour by default)
url = await storage.url("uploads/file.pdf")
Storage Protocol
All backends implement the Storage protocol:
async def put(key: str, data: bytes | AsyncIterator[bytes], *, content_type: str | None, metadata: dict[str, str] | None) -> StoredFile
async def get(key: str) -> AsyncIterator[bytes]
async def get_bytes(key: str) -> bytes
async def delete(key: str) -> None
async def exists(key: str) -> bool
async def list(prefix: str = "", *, limit: int | None = None) -> AsyncIterator[StoredFile]
async def url(key: str, *, expires_in: timedelta | None = None) -> str
async def copy(source: str, destination: str) -> StoredFile
async def move(source: str, destination: str) -> StoredFile
async def info(key: str) -> StoredFile
Backends
Implemented
- MemoryStorage: In-memory storage for testing (no external dependencies)
- FileSystemStorage: Local filesystem with aiofiles (requires
aiofiles) - S3Storage: AWS S3 and S3-compatible services (requires
aioboto3)
Planned
- GCSStorage: Google Cloud Storage
- AzureStorage: Azure Blob Storage
Security Features
Path Traversal Prevention (FileSystemStorage)
The filesystem backend sanitizes all keys to prevent directory traversal attacks:
storage.put("../../etc/passwd", data) # Safe: becomes "etc/passwd"
Presigned URLs (S3Storage)
Generate time-limited URLs for secure file access:
from datetime import timedelta
url = await storage.url("private/file.pdf", expires_in=timedelta(minutes=15))
Development
# Clone repository
git clone https://github.com/JacobCoffee/litestar-storages.git
cd litestar-storages
# Install with development dependencies
uv sync --extra dev
# Run tests
uv run pytest
# Run linting
uv run ruff check src tests
uv run mypy src
License
MIT License - see LICENSE file for details.
Credits
Built for the Litestar framework.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litestar_storages-0.1.0.tar.gz.
File metadata
- Download URL: litestar_storages-0.1.0.tar.gz
- Upload date:
- Size: 31.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
abb68824a595518af2337097f9dad6a7e6487c9249b2e73b5aeb03b104fea9fa
|
|
| MD5 |
5a922490a9852e80a0ba932509d9d3ed
|
|
| BLAKE2b-256 |
0617192d323db89b97c63dcea293c500d17d58f41837f66254fd391101ee8616
|
Provenance
The following attestation bundles were made for litestar_storages-0.1.0.tar.gz:
Publisher:
publish.yml on JacobCoffee/litestar-storages
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_storages-0.1.0.tar.gz -
Subject digest:
abb68824a595518af2337097f9dad6a7e6487c9249b2e73b5aeb03b104fea9fa - Sigstore transparency entry: 726529205
- Sigstore integration time:
-
Permalink:
JacobCoffee/litestar-storages@77a2e82225557a7e32508c6f3a2ea5b37b7bc837 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/JacobCoffee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@77a2e82225557a7e32508c6f3a2ea5b37b7bc837 -
Trigger Event:
release
-
Statement type:
File details
Details for the file litestar_storages-0.1.0-py3-none-any.whl.
File metadata
- Download URL: litestar_storages-0.1.0-py3-none-any.whl
- Upload date:
- Size: 40.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67d608bee0a6f2f57aef497fb35646abe6beaeaa8ee6e7510d436405cc1edd8a
|
|
| MD5 |
ec5b228e0803cb544eb9fc8c01b5045b
|
|
| BLAKE2b-256 |
3ae50cd85e4209489addb2dd6b52d5a3d681c22cdaa646c627977774a772e19f
|
Provenance
The following attestation bundles were made for litestar_storages-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on JacobCoffee/litestar-storages
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
litestar_storages-0.1.0-py3-none-any.whl -
Subject digest:
67d608bee0a6f2f57aef497fb35646abe6beaeaa8ee6e7510d436405cc1edd8a - Sigstore transparency entry: 726529230
- Sigstore integration time:
-
Permalink:
JacobCoffee/litestar-storages@77a2e82225557a7e32508c6f3a2ea5b37b7bc837 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/JacobCoffee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@77a2e82225557a7e32508c6f3a2ea5b37b7bc837 -
Trigger Event:
release
-
Statement type: