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.Sessionacross 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 preconfiguredboto3.Session.
If not provided, one is created lazily from theregion_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
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 django_s3_session_storage-0.1.1.tar.gz.
File metadata
- Download URL: django_s3_session_storage-0.1.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
154e906a4c70f0edad9320734c5e2d5682ffc9ec228d68ca06c3d2594277671f
|
|
| MD5 |
458029ba1be29b68a77a07497b2ea654
|
|
| BLAKE2b-256 |
8c28bc32a9007a9b93b3d934a444ef720f5a87c5737bbb168b3006d56edb5b29
|
File details
Details for the file django_s3_session_storage-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_s3_session_storage-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f09fe008518c6fbe71d02d9a9a7c60092ec24c35ea062147a3639ae5edefe751
|
|
| MD5 |
516167327316dca8419034a304afbd45
|
|
| BLAKE2b-256 |
4534070671ed9d92ca23082322dbb9535adb5d0949bd7f9bd5debed6913fa720
|