Skip to main content

django-database-storage-backend

A Django 1.10+/2.x storages backend backed by your existing database.

This module provides an app named dbstorage. The app contains a single model StoredFile which is where files stored with the storage backend are put. The app also provides DatabaseStorage which you can set on your file fields.

Usage

Install libmagic, e.g.:

brew install libmagic

Install this package:

pip django-database-storage-backend

Put 'dbstorage' in your INSTALLED_APPS in your settings.py.

Add to your settings.py:

DEFAULT_FILE_STORAGE = 'dbstorage.storage.DatabaseStorage'

Add to your urls.py URLconf:

url(r'^user-media/', include('dbstorage.urls')),

user-media/ determines the URL path where media files are accessed from. You can set this to anything.

Add a File or ImageFile field to your model:

image = models.ImageField(upload_to='some-root')

Files saved into this field will be stored in the database and available at the URL path /user-media/some-root/{HASH}.{EXT}. HASH is the SHA-1 hash of the file content. The file extension is replaced with a normalized file extension by auto-detecting the file type so that no file name information besides the file's type is leaked. This also prevents unauthorized users from randomly guessing the URLs to uploaded files (although SHA-1 is generally deprecated for security purposes in favor of SHA-256 --- so don't use this as your only form of authorization logic if it matters).

The upload_to value cannot be more than 200 characters as the maximum full path length is 255 characters.

Features

  • Easily configure a media storage backend that just uses your existing database for storing and serving your media files.
  • Filenames don't leak the name of the file that it was uploaded from and are based on a hash of the file's content to prevent unauthorized users from guessing file URLs.
  • Secure headers are set to prevent untrusted content from being a cross-site scripting vulnerability.
  • The backend supports the delete, exists, listdir, size, url, created_time, and modified_time functions.
  • Stored files appear in the Django admin.

Dynamic image resizing

When storing images, the view method can automatically resize an image to one of a few pre-defined sizes. To use this feature, you must install pillow. Then add ?width= plus xs (768px), sm (1024 px), md (1100px), or lg (1400px) to the URL when accessing the image. The image returned in the response will have this size as a maximum dimension.

For Project Maintainers

To publish a universal wheel to pypi::

    pip3 install twine
    rm -rf dist
    python3 setup.py bdist_wheel --universal
    twine upload dist/*
    git tag v1.0.XXX
    git push --tags

Metadata

Release files for django-database-storage-backend 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for django-database-storage-backend 1.0.0
File Interpreter ABI Platform
django_database_storage_backend-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / django_database_storage_backend-1.0.0-py2.py3-none-any.whl

Download URL django_database_storage_backend-1.0.0-py2.py3-none-any.whl
Size 13.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
38b1ccd5ba9768b586d0af868fbbdc8a039d63aae040571ab66e17405a26bd25
BLAKE2b-256 checksum
How to use checksums
8020a59d56433a60d952001af484298b44585396debfd0a2699fe58f9a2807e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page