Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

litestar_storages-0.1.0.tar.gz (31.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

litestar_storages-0.1.0-py3-none-any.whl (40.2 kB view details)

Uploaded Python 3

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

Hashes for litestar_storages-0.1.0.tar.gz
Algorithm Hash digest
SHA256 abb68824a595518af2337097f9dad6a7e6487c9249b2e73b5aeb03b104fea9fa
MD5 5a922490a9852e80a0ba932509d9d3ed
BLAKE2b-256 0617192d323db89b97c63dcea293c500d17d58f41837f66254fd391101ee8616

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_storages-0.1.0.tar.gz:

Publisher: publish.yml on JacobCoffee/litestar-storages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file litestar_storages-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_storages-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67d608bee0a6f2f57aef497fb35646abe6beaeaa8ee6e7510d436405cc1edd8a
MD5 ec5b228e0803cb544eb9fc8c01b5045b
BLAKE2b-256 3ae50cd85e4209489addb2dd6b52d5a3d681c22cdaa646c627977774a772e19f

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_storages-0.1.0-py3-none-any.whl:

Publisher: publish.yml on JacobCoffee/litestar-storages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page