Skip to main content

Simplified S3 storage backends for Django projects with intelligent bucket and region selection.

Project description

django-smart-storages

Reusable, specialized storage backends built on top of django-storages

This package provides a simple, consistent way to define multiple custom S3 or file storage backends in Django projects — each with its own configuration, bucket, or logic — without repeating boilerplate code.

Features

Clean abstraction for per-use-case S3 buckets Seamless integration with django-storages Built-in backend resolver for dynamic imports Optional fallback to local storage in development Easy to extend for any specialized storage use case

Installation

pip install django-special-storages

Configuration

In your Django settings, add the relevant apps:

# settings.py

INSTALLED_APPS = [
    'storages',    # this is for django-storages
]

Example: Custom Storage Classes

Define specialized storage classes in your code (e.g., views.py or a separate storages.py):

# views.py (or storages.py)

from smart_storages import BaseSpecialS3Storage

class ImportExportS3Storage(BaseSpecialS3Storage):
    storage_key = "import_export"

class AnalyticsS3Storage(BaseSpecialS3Storage):
    storage_key = "analytics"

Example: Configuring Multiple Storages

Add your bucket names and the STORAGES setting:

# settings.py

AWS_STORAGE_BUCKET_NAME = "main-bucket"

STORAGES = {
    # Default file storage
    "default": {
        "BACKEND": "django.core.files.storage.FileSystemStorage",
    },

    # Static files
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
    },

    # Import-export-specific S3 storage (using your custom base class)
    "import_export": {
        "BACKEND": "special_storages.s3.ImportExportS3Storage",
        "OPTIONS": {
            "custom_domain": None,
            "querystring_auth": True,
        },
    },

    # Analytics-specific S3 storage
    "analytics": {
        "BACKEND": "special_storages.s3.AnalyticsS3Storage",
        "OPTIONS": {
            "custom_domain": None,
        },
    },
}

Example 1: Saving directly to S3 via FileField

This will automatically use the given bucket configured in ImportExportS3.

class PublicImage(models.Model):
    file = models.FileField(storage=ImportExportS3())

Example 2: Saving a file via model's save() method

Useful if you need to process or manipulate the file before uploading.

class PublicImage(models.Model):
    file = models.FileField()

    def save(self, *args, **kwargs):
        # Only upload if a file is provided
        if self.file and hasattr(self.file, 'file'):
            storage = ImportExportS3()
            # Save file content to S3
            saved_name = storage.save(self.file.name, self.file)
            # Update the file name to the S3 path
            self.file.name = saved_name

        super().save(*args, **kwargs)

Example 3: Uploading a file from a remote URL (binary download or streaming)

Useful for fetching external content and storing it directly in S3.

import requests
from django.core.files.base import ContentFile

class PublicImage(models.Model):
    image = models.URLField(blank=True, null=True)

    def save(self, *args, **kwargs):
        # Fetch an image from a remote URL
        image_url = "https://www.edx.org/contentful/ii9ehdcj88bc/2SkUwC7Kf9G5I5b49hjVgu/1fa2453e92e46d980f9f99cf08a51e73/image_processing.jpg?w=435&h=245&fm=webp"
        response = requests.get(image_url)

        if response.status_code == 200:
            # Wrap content in Django ContentFile
            content = ContentFile(response.content)

            # Use custom S3 storage to save
            storage = ImportExportS3()
            file_name = storage.save("deb.png", content)

            # Update the model field with the S3 URL
            self.image = storage.url(file_name)

        # Save model instance
        super().save(*args, **kwargs)

Note:
You can place these custom storage classes in any appropriate module (such as storages.py), and reference them in your STORAGES Django setting.

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_smart_storages-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

django_smart_storages-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_smart_storages-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_smart_storages-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3dd20a8b3bc261366f9e7a29a41f39b52104ffa7c17587da15288e979a0748e2
MD5 8e426b5bd53d7ad41f27f33be4d772aa
BLAKE2b-256 f3b6193657eb2b11c4a90d2bb4512adafb49f5345e91d968933618a92e9eeb82

See more details on using hashes here.

Provenance

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

Publisher: pypi-publish.yml on awais786/django-smart-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 django_smart_storages-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_smart_storages-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8dd533a9f186563a255106301a87154ddc68a6f1ff25341ed01ac9afb743783
MD5 0e69cbcf6fd90324ecfdbd0855616377
BLAKE2b-256 c16ea895e8e49a26ce0e2be9a5e463bf148c324014ba14eb7dfbf9fb8376c7c9

See more details on using hashes here.

Provenance

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

Publisher: pypi-publish.yml on awais786/django-smart-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