Opened 3 years ago

Last modified 8 months ago

#26029 new New feature

Provide an API to configure arbitrary file storage backends

Reported by: Aymeric Augustin Owned by:
Component: File uploads/storage Version: master
Severity: Normal Keywords:
Cc: django@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently Django has two hardcoded file storage backends, "default" (for media files) and "static" (for static files). Idioms for configuring file storage backends in pluggable apps are cumbersome.

It would be nice to be able to configure arbitrary file storage backends, like caches and databases e.g.:

FILE_STORAGES = {
   'media': {
       'BACKEND': settings.DEFAULT_FILE_STORAGE,
       'OPTIONS': {
            'location': settings.MEDIA_ROOT,
            'base_url': settings.MEDIA_URL,
            # possible override of settings.FILE_CHARSET
       },
   },
   'static': {
       'BACKEND': settings.STATICFILES_STORAGE,
       'OPTIONS': {
            'location': settings.STATIC_ROOT,
            'base_url': settings.STATIC_URL,
            # replacement for STATICFILES_FINDERS and STATICFILES_DIRS that would look a lot like template loaders
            # possible override of settings.FILE_CHARSET
       },
   }

This was discussed on django-developers: https://groups.google.com/d/msg/django-developers/gEbFApLLuzg/IW1LDUwmEgAJ

There were some concerns about introducing another large dict in settings. The general ideas was uncontroversial.

Change History (7)

comment:1 Changed 3 years ago by Keryn Knight

For what it's worth, bmispelon and I discussed something akin to this recently, out of which I started implementing something similar entirely as an experiment. One thing I liked about where I went with that was that a named storage key in the dictionary was a dotted path, and behaved somewhat like a logging config in that the nearest 'parent' match such that each model field which uses a storage could be addressed separately, like so:

class Test(Model):
    a = FileField(storage='myapp.test.a')
    b = ImageField(storage='myapp.test.b')

and then if myapp.test.a or myapp.test.b were in the configuration dictionary, they would be used, but if not, and myapp.test was, that would be used, and so on walking backwards up the dotted path (falling back to ostensibly default [''] in the end)

The notion there was third-party apps could namespacing storages per-field, but at a project level one only has to opt-in as much as one cares (ie: myapp, or myapp.test, or default etc.) to have finegrained control.

comment:2 Changed 3 years ago by Keryn Knight

Cc: django@… added

comment:3 Changed 3 years ago by Sasha Gaevsky

Configurable file storage backends was already proposed to introduce in terms of task #23251.

comment:4 Changed 3 years ago by Sasha Gaevsky

Owner: changed from nobody to Sasha Gaevsky
Status: newassigned

comment:5 in reply to:  description Changed 3 years ago by Sasha Gaevsky

Replying to aaugustin:

FILE_STORAGES = {
   'media': {
       'BACKEND': settings.DEFAULT_FILE_STORAGE,
       'OPTIONS': {
            'location': settings.MEDIA_ROOT,
            'base_url': settings.MEDIA_URL,
            # possible override of settings.FILE_CHARSET
       },
   ...
}

Media backend will have media key in settings, not default, to keep it consistent with the current settings?

comment:6 Changed 3 years ago by Sasha Gaevsky

As proposed Aymeric Augustin in the original PR, I started to compose DEP, so now it's in progress and I hope to finish it sometime soon.

comment:7 Changed 8 months ago by Sasha Gaevsky

Owner: Sasha Gaevsky deleted
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top