The file storage system configured for your Django app should be a class that implements django.core.files.storage.Storage
[1] https://docs.djangoproject.com/en/2.1/howto/custom-file-storage/#writing-a-custom-storage-system
storages.backends.s3boto3.S3Boto3Storage
already implements this storage interface. [2] https://github.com/jschneier/django-storages/blob/1.7.1/storages/backends/s3boto.py#L186
Setting StaticRootS3BotoStorage
in utils.py
to a lambda
, the Storage
system is instantiated lazily with the proper location
value; but the location
attribute in the storage class itself is never changes. [3] https://github.com/jschneier/django-storages/blob/1.7.1/storages/backends/s3boto.py#L216
location = setting('AWS_LOCATION', '')
Django clears properties of storage instance when the project settings changes. [4] https://github.com/django/django/blob/2.1.4/django/core/files/storage.py#L180 So that when the location
attribute is resolved on the storage system, it effectively looks up the class attribute one (location
value is shown in above snippet) because location
attribute is missing in the instance.
这种情况可以通过子类化来解决storages.backends.s3boto3.S3Boto3Storage
反而。这保证了location
无论项目设置如何更改,值都不会改变。
class StaticRootS3BotoStorage(S3Boto3Storage):
location = 'static'
class MediaRootS3BotoStorage(S3Boto3Storage):
location = 'media'