Skip to main content

A Django S3 storage backend that supports dependency injection and shared boto3.Session reuse.

Project description

django-s3-session-storage

A lightweight Django storage backend for Amazon S3 that supports shared boto3.Session reuse and dependency injection
designed to eliminate latency spikes from repeated IAM credential fetching on every file access.


⚡ Why?

When using django-storages, each new instance of S3Storage internally creates a new boto3.Session.
This triggers fresh IAM credential resolution (via EC2/ECS metadata or STS), which can add tens to hundreds of milliseconds of latency per request —
especially when accessing S3-backed FileField or ImageField objects frequently.

django-s3-session-storage solves this by allowing you to inject or share a single, cached boto3.Session,
so IAM tokens are fetched once and reused across all storage operations.

Result:
✅ Consistent response times
✅ Fewer network round trips to AWS metadata endpoints
✅ Simpler dependency injection for testing and configuration


🚀 Features

  • 🔁 Reuses a shared boto3.Session across all S3 operations
  • ⚙️ Supports dependency injection for clean app design
  • 🧠 Drop-in replacement for storages.backends.s3.S3Storage
  • ⚡ Eliminates repeated IAM token lookups, reducing S3 latency
  • 🧪 Fully tested, type-annotated, and dependency-light

📦 Installation

pip install django-s3-session-storage

or with uv:

uv add django-s3-session-storage

⚙️ Usage

In your Django settings:

import boto3
from django_s3_session_storage import make_storages

BOTO3_SESSION = boto3.Session(region_name="eu-central-1")

STORAGES = {
    "default": make_storages(
        bucket_name="my-bucket",
        region_name="eu-central-1",
        session=BOTO3_SESSION,  # optional
    ),
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
    },
}

S3SessionStorage will reuse your provided session,
or lazily create one if none is given.


🧠 Example: Direct instantiation

from django_s3_session_storage import S3SessionStorage
import boto3

session = boto3.Session(region_name="us-east-1")
storage = S3SessionStorage(bucket_name="example-bucket", region_name="us-east-1", session=session)

🧪 Running Tests

uv sync --dev
pytest

Minimal Django settings are auto-configured in tests/conftest.py.


🧰 API Reference

S3SessionStorage

Subclass of storages.backends.s3.S3Storage that reuses an injected or cached boto3.Session.

Constructor:

S3SessionStorage(
    *,
    session: Optional[boto3.Session] = None,
    **kwargs
)
  • session: Optional preconfigured boto3.Session.
    If not provided, one is created lazily from the region_name.

make_storages(bucket_name, region_name, session=None)

Helper for building a single Django STORAGES entry.

Returns:

{
    "BACKEND": "django_s3_session_storage.S3SessionStorage",
    "OPTIONS": { ... }
}

🧾 License

MIT License © 2025 Credibur GmbH
Developed and maintained by the engineering team at Credibur GmbH.


🔗 Links

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

django_s3_session_storage-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

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

django_s3_session_storage-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file django_s3_session_storage-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_s3_session_storage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7779df36a8f25c3f7d4b29cf3aa740c745896165b5893b707f72e25962439d28
MD5 132930949b543c7c3ee415a016deea90
BLAKE2b-256 c09aa47629ba5ae8a2c396f0cea758f1c8e49eb9bee0b9d53c10d0c4103ae3e6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_s3_session_storage-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca3d6da585e26d3e24df33764fd5f9305e60a00e83ead5267d23b39834f42265
MD5 20469bfaf37c70de9895b1b43f4c02a9
BLAKE2b-256 773d3636d8f0d90915766d4ab18d296a8f3d8d57059246e15a32ae354e038a9b

See more details on using hashes here.

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