Replace custom static & media storage classes by passing options in the STORAGES setting (#4803)

The custom cloud storages module has been deleted, and the settings have been updated to use the storage backend settings directly from each cloud provider's storage backend libraries. These changes have simplified the cloud storage configuration for each cloud provider in the production settings file.
This commit is contained in:
Jens Kaeske 2024-01-25 10:01:27 +01:00 committed by GitHub
parent 4da8386f9c
commit bda89eaa06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 47 deletions

View File

@ -429,10 +429,6 @@ def remove_drf_starter_files():
os.remove(os.path.join("{{cookiecutter.project_slug}}", "users", "tests", "test_swagger.py"))
def remove_storages_module():
os.remove(os.path.join("{{cookiecutter.project_slug}}", "utils", "storages.py"))
def main():
debug = "{{ cookiecutter.debug }}".lower() == "y"
@ -499,7 +495,6 @@ def main():
WARNING + "You chose to not use any cloud providers nor Docker, "
"media files won't be served in production." + TERMINATOR
)
remove_storages_module()
if "{{ cookiecutter.use_celery }}".lower() == "n":
remove_celery_files()

View File

@ -115,24 +115,47 @@ STORAGES = {
},
{%- elif cookiecutter.cloud_provider == 'AWS' %}
"default": {
"BACKEND": "{{cookiecutter.project_slug}}.utils.storages.MediaS3Storage",
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
"location": "media",
"file_overwrite": False,
},
},
"staticfiles": {
"BACKEND": "{{cookiecutter.project_slug}}.utils.storages.StaticS3Storage",
"BACKEND": "storages.backends.s3.S3Storage",
"OPTIONS": {
"location": "static",
"default_acl": "public-read",
},
},
{%- elif cookiecutter.cloud_provider == 'GCP' %}
"default": {
"BACKEND": "{{cookiecutter.project_slug}}.utils.storages.MediaGoogleCloudStorage",
"BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
"OPTIONS": {
"location": "media",
"file_overwrite": False,
},
},
"staticfiles": {
"BACKEND": "{{cookiecutter.project_slug}}.utils.storages.StaticGoogleCloudStorage",
"BACKEND": "storages.backends.gcloud.GoogleCloudStorage",
"OPTIONS": {
"location": "static",
"default_acl": "publicRead",
},
},
{%- elif cookiecutter.cloud_provider == 'Azure' %}
"default": {
"BACKEND": "{{cookiecutter.project_slug}}.utils.storages.MediaAzureStorage",
"BACKEND": "storages.backends.azure_storage.AzureStorage",
"OPTIONS": {
"location": "media",
"file_overwrite": False,
},
},
"staticfiles": {
"BACKEND": "{{cookiecutter.project_slug}}.utils.storages.StaticAzureStorage",
"BACKEND": "storages.backends.azure_storage.AzureStorage",
"OPTIONS": {
"location": "static",
},
},
{%- endif %}
}

View File

@ -1,36 +0,0 @@
{% if cookiecutter.cloud_provider == 'AWS' -%}
from storages.backends.s3 import S3Storage
class StaticS3Storage(S3Storage):
location = "static"
default_acl = "public-read"
class MediaS3Storage(S3Storage):
location = "media"
file_overwrite = False
{%- elif cookiecutter.cloud_provider == 'GCP' -%}
from storages.backends.gcloud import GoogleCloudStorage
class StaticGoogleCloudStorage(GoogleCloudStorage):
location = "static"
default_acl = "publicRead"
class MediaGoogleCloudStorage(GoogleCloudStorage):
location = "media"
file_overwrite = False
{%- elif cookiecutter.cloud_provider == 'Azure' -%}
from storages.backends.azure_storage import AzureStorage
class StaticAzureStorage(AzureStorage):
location = "static"
class MediaAzureStorage(AzureStorage):
location = "media"
file_overwrite = False
{%- endif %}