Skip to main content

A Django storage backend that names files by hash value.

Project description

A Django storage backend that names files by hash value.

By default, django.core.files.storage.FileSystemStorage deals with conflicting filenames by appending an underscore to the file. For instance, if you try to create hello.txt when it already exists, it will rename it as hello_.txt.

django-hashedfilenamestorage creates hashed filenames, so if you try to create hello.txt with the content Hello world!, it will save it as d3486ae9136e7856bc42212385ea797094475802.txt. Directory names and extensions are preserved, only the root filename is changed. This means that there will one copy of this file will be stored in the underlying backend, as long as no matter how many times hello.txt is uploaded.

Installing

The easiest way to install django-hashedfilenamestorage is to use pip:

pip install django-hashedfilenamestorage

Quick Start

In your Django settings file:

  • Set DEFAULT_FILE_STORAGE to 'django_hashedfilenamestorage.storage.HashedFilenameFileSystemStorage'

This gives you hashed filenames, backed on Django’s FileSystemStorage storage class.

HashedFilenameMetaStorage

You can define a new underlying storage class by using HashedFilenameMetaStorage to wrap it:

from django.core.files.storage import get_storage_class

from django_hashedfilenamestorage.storage import HashedFilenameMetaStorage

HashedFilenameMyStorage = HashedFilenameMetaStorage(
    storage_class=get_storage_class('myapp.storage.MyStorage'),
)

Hashing algorithm

HashedFilenameMetaStorage is meant to generate duplicate filenames for files with identical contents. To do this, it reads the contents of the file and generates a SHA-1 hash of them.

Filenames have their extensions preserved, so it is possible to have duplicate contents on the filesystem, but it is important to help serve files with their proper content types.

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-hashedfilenamestorage-1.0.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file django-hashedfilenamestorage-1.0.tar.gz.

File metadata

File hashes

Hashes for django-hashedfilenamestorage-1.0.tar.gz
Algorithm Hash digest
SHA256 81cb55c808cfd28c791e88ed446a78982b1a42ff1ca607ac6be863c0557371f9
MD5 523ab0c41cccb3bfb64adbd4eb0f4ab2
BLAKE2b-256 1ec952f0fb156668870fc451de1a2efe92861c0312158b164d4dc11763bcd2cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page