Search code examples
djangoamazon-s3django-storage

Django Model FileField storage use class depends on settings


I would like to manage django multimedia files locally and in the aws s3, I have all the configuration and if I specify the storage class in the model it works fine, but it is not practical because I have to do migrations every time, but I would like to establish that the File configuration depends on the configuration variable called 'FILES' to work programmatically.

This is the code

settings.py

FILE_OVERWRITE = True

if FILES == 'LOCAL':
  MEDIA_ROOT = os.path.join(BASE_DIR, "media")
  MEDIAFILES_STORAGE = 'app.files.CustomStorage'
  MEDIA_URL = '/media/'

elif FILES == 'AWS':
  MEDIAFILES_STORAGE = 'app.files.MediaStorage'
  MEDIA_URL='https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, MEDIA_LOCATION)

storages.py

class CustomStorage(FileSystemStorage):

  def get_available_name(self, name, *args, **kwargs):

    if self.exists(name):

      if settings.FILE_OVERWRITE:
        os.remove(os.path.join(settings.MEDIA_ROOT, name))
      else:
        alternative_name = name.split('.')
        name = alternative_name[0] + str(timezone.now()).split(' ')[0] + '.' + alternative_name[1]

    return name

class MediaStorage(S3Boto3Storage):
  location = settings.MEDIA_LOCATION
  file_overwrite = settings.FILE_OVERWRITE

models.py

class Bill(ChangesMixin, models.Model):

  #pdf = models.FileField(storage=CustomStorage(), upload_to='media/', blank=True, null=True) <-- on local with this class works well
  #pdf = models.FileField(storage=MediaStorage(), upload_to='media/', blank=True, null=True)  <-- on aws with this class works well

  # throw an error because settings class name is not callable
  pdf = models.FileField(storage=settings.MEDIAFILES_STORAGE, upload_to='media/', blank=True, null=True)

Anybody could help me ? How can I do this ?

Thanks in advance.


Solution

  • The DEFAULT_FILE_STORAGE setting allows you to set the storage to use by default. This eliminates the need to assign `storages to individual fields, and eliminates the need to create migration files even if the storage changes.

    docs: https://docs.djangoproject.com/en/3.0/ref/settings/#default-file-storage

    I recommend separating settings.py to local.py and production.py configuration files. This eliminates the if statement inside the configuration file. Take a look at the configuration file for the project I'm working on: https://github.com/youngminz/mma-portal/tree/master/mma_portal/settings