This function generates a unique id for my files, which will be stored in the database to reference them. It returns the id if unique, if not, it generates a new one. My separate file server can then use these ids to locate and retrieve a file.
I'm planning on storing thousands upon maybe millions of files uploaded by users in the future, so this needs to be robust and futureproof.
from django.utils.crypto import get_random_string from .models import Media def generateUID(): uid = get_random_string(length=16, allowed_chars=u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') date = datetime.datetime.now() result = '%s-%s-%s_%s' % (date.year, date.month, date.day, uid) print(result) try: obj = Media.objects.get(uid=result) except Media.DoesNotExist: return uid else: return generateUID()
Media model is a parent which stores the uid. There is an image model that
Media can relate to.
uid = 'test':
name = 't200x200'
name = 'original'
When the files are created, they are stored like this:
Let's say the ID is, for example:
and the filename for the file that we want to get is:
Then the file would be saved like this in my file server:
This ensures that there will be no collisions, and the file system is split and doesn't have thousand of files in one folder, which will damage performance.
Is there any way I can make this perform better or be more reliable? Avoiding collisions and performance issues is a must.