Skip to main content

Async file storage abstraction library with optional Litestar integration

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.2.0.tar.gz (32.0 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.2.0-py3-none-any.whl (41.0 kB view details)

Uploaded Python 3

File details

Details for the file litestar_storages-0.2.0.tar.gz.

File metadata

  • Download URL: litestar_storages-0.2.0.tar.gz
  • Upload date:
  • Size: 32.0 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.2.0.tar.gz
Algorithm Hash digest
SHA256 17fb6a7774aa807471b4eecf711b514c4f43d3b1407692fd7e803565ed24d0c6
MD5 0916007a2fcb861e8a1041a732e4b3ff
BLAKE2b-256 16958aed7b0dafa5d276e468912e708974a028cc42dfad79d3b5630700fde428

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_storages-0.2.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.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for litestar_storages-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0ed8b15b55d7ed12c2a0a400e7084b686b0efaa0914ef05561354ae64355624
MD5 63c38edf64a78064d56e65fb4f7bdaf2
BLAKE2b-256 f0f6dff8d5b77859711559917f1a5feb9a234bd2d549aab14438b38a82143f37

See more details on using hashes here.

Provenance

The following attestation bundles were made for litestar_storages-0.2.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