I have set up my django REST API to use local storage when in DEBUG mode and S3 storage when in production environment. This works well for public files, because I override the DEFAULT_FILE_STORAGE
like so:
if IS_DEBUG:
DEFAULT_FILE_STORAGE = 'api.storage_backends.PublicMediaStorage'
and every FileField
uses it automatically. Now I want to use private S3 storage the same way, but because I have to define the storage explicitly (FileField(storage=PrivateMediaStorage())
), the S3 storage is always used.
How can I use the local storage instead of S3 storage when in DEBUG mode?
PS: I have already thought about changing the model to either use a FileField
with or without an explicit storage depending on the DEBUG mode. This did not fully solve my problem, because my migrations are created in DEBUG mode and thus always contain the model without the private storage class.
UPDATE:
I am looking for a solution that can share the same migrations in both environments and only during runtime lazily instantiates the actual storageclass. Just like django handles the DEFAULT_FILE_STORAGE
already.