Skip to main content

A Django storage backend that gives random names to files.

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-randomfilenamestorage creates random filenames, so if you try to create hello.txt, it will save it as 7z0a8m25sh9fyitx.txt. Directory names and extensions are preserved, only the root filename is randomized.

Installing

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

pip install django-randomfilenamestorage

Quick Start

In your Django settings file:

  • Set DEFAULT_FILE_STORAGE to 'django_randomfilenamestorage.storage.RandomFilenameFileSystemStorage'

  • Optionally, add 'django-dbstorage' to INSTALLED_APPS

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

RandomFilenameMetaStorage

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

from django.core.files.storage import get_storage_class

from django_randomfilenamestorage.storage import RandomFilenameMetaStorage

RandomFilenameMyStorage = RandomFilenameMetaStorage(
    storage_class=get_storage_class('myapp.storage.MyStorage'),
)

RandomFilenameMetaStorage defaults to 16-character root filenames. To change the default, define settings.RANDOM_FILENAME_LENGTH to a different integer value.

To change the filename length of a wrapped storage class, pass in a length argument to RandomFilenameMetaStorage. To override it for a particular storage instance, pass in a randomfilename_length argument to its constructor.

Efficient random filename generation

RandomFilenameMetaStorage is careful about not overwriting existing files, on creation. Unfortunately, many storage classes do not throw an OSError with EEXISTS when they detect a duplicate file on _save().

If the underlying storage class accepts uniquify_names=False in its constructor, RandomFilenameMetaStorage will reduce the number of round-trips to the underlying storage class and make random filename generation more efficient.

Safe storage classes include:

Release files for django-randomfilenamestorage 1.1

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

Source distribution (sdist)

Source distribution for django-randomfilenamestorage 1.1
File Size Uploaded
django-randomfilenamestorage-1.1.tar.gz 6.3 kB Details

Release files / django-randomfilenamestorage-1.1.tar.gz

Download URL django-randomfilenamestorage-1.1.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
51ea56ff6970364573b8739a163a0e3f229e2240ddb8899a2ce0d82e94490ef5
BLAKE2b-256 checksum
How to use checksums
44a76b74bc36e37a78e0798ff945e9b4af0520a5a097bae2222d377c32379d1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1 This release

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