Add azure blob storage as default media storage

This commit is contained in:
Lorenz Padberg 2023-05-22 14:56:50 +02:00 committed by Daniel Egger
parent 6e264b1480
commit 0a5a6fa4ce
7 changed files with 104 additions and 24 deletions

4
azure_deploy.sh Normal file
View File

@ -0,0 +1,4 @@
npm install
python server/manage.py collectstatic --noinput
docker build --platform linux/amd64 -f compose/django/Dockerfile -t iterativ/vbv-lernwelt-django:azure .
docker image push iterativ/vbv-lernwelt-django:azure

Binary file not shown.

View File

@ -5,14 +5,14 @@ from environs import Env
env = Env()
env.read_env()
class AzureMediaStorage(AzureStorage):
account_name = env("AZURE_STORAGE_ACCOUNT")
account_key = env("AZURE_STORAGE_KEY")
account_name = env("IT_AZURE_STORAGE_ACCOUNT")
account_key = env("IT_AZURE_STORAGE_KEY")
azure_container = 'media'
expiration_secs = None
class AzureStaticStorage(AzureStorage):
account_name = env("AZURE_STORAGE_ACCOUNT")
account_key = env("AZURE_STORAGE_KEY")
account_name = env("IT_AZURE_STORAGE_ACCOUNT")
account_key = env("IT_AZURE_STORAGE_KEY")
azure_container = 'static'
expiration_secs = None

View File

@ -214,18 +214,18 @@ if USE_AWS:
# https://wagtail.org/blog/amazon-s3-for-media-files/
MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
elif env(USE_AZURE_BLOB_STORAGE):
elif env("IT_USE_AZURE_BLOB_STORAGE", False):
# https://medium.com/@DawlysD/django-using-azure-blob-storage-to-handle-static-media-assets-from-scratch-90cbbc7d56be
DEFAULT_FILE_STORAGE = 'backend.custom_azure.AzureMediaStorage'
STATICFILES_STORAGE = 'backend.custom_azure.AzureStaticStorage'
#STATICFILES_STORAGE = 'backend.custom_azure.AzureStaticStorage'
STATIC_LOCATION = "static"
#STATIC_LOCATION = "static"
MEDIA_LOCATION = "media"
AZURE_ACCOUNT_NAME = "djangoaccountstorage"
AZURE_ACCOUNT_NAME = env("IT_AZURE_STORAGE_ACCOUNT")
AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/'
#STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{APP_ENVIRONMENT}/{STATIC_LOCATION}/'
MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{APP_ENVIRONMENT}/{MEDIA_LOCATION}/'
else:

View File

@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# pip-compile --output-file=requirements-dev.txt requirements-dev.in
#
@ -10,6 +10,8 @@ anyascii==0.3.1
# via wagtail
anyio==3.5.0
# via watchfiles
appnope==0.1.3
# via ipython
argon2-cffi==21.3.0
# via -r requirements.in
argon2-cffi-bindings==21.2.0
@ -29,6 +31,16 @@ attrs==21.4.0
# usort
authlib==1.0.0
# via -r requirements.in
azure-core==1.26.4
# via
# azure-identity
# azure-storage-blob
azure-identity==1.13.0
# via -r requirements.in
azure-storage-blob==12.16.0
# via
# -r requirements.in
# django-storages
backcall==0.2.0
# via ipython
beautifulsoup4==4.9.3
@ -82,7 +94,12 @@ coverage==6.3.2
# -r requirements-dev.in
# django-coverage-plugin
cryptography==36.0.2
# via authlib
# via
# authlib
# azure-identity
# azure-storage-blob
# msal
# pyjwt
decorator==5.1.1
# via
# ipdb
@ -155,7 +172,7 @@ django-ratelimit==3.0.1
# via -r requirements.in
django-redis==5.2.0
# via -r requirements.in
django-storages==1.13.1
django-storages[azure]==1.13.1
# via -r requirements.in
django-stubs==1.10.1
# via
@ -241,6 +258,8 @@ ipdb==0.13.9
# via -r requirements-dev.in
ipython==8.2.0
# via ipdb
isodate==0.6.1
# via azure-storage-blob
isort==5.10.1
# via
# flake8-isort
@ -281,6 +300,12 @@ moreorless==0.4.0
# via
# ufmt
# usort
msal==1.22.0
# via
# azure-identity
# msal-extensions
msal-extensions==1.0.0
# via azure-identity
mypy==0.942
# via
# -r requirements-dev.in
@ -330,7 +355,9 @@ pluggy==1.0.0
polib==1.1.1
# via wagtail-localize
portalocker==2.4.0
# via concurrent-log-handler
# via
# concurrent-log-handler
# msal-extensions
pre-commit==2.17.0
# via -r requirements-dev.in
promise==2.3
@ -353,6 +380,8 @@ pyflakes==2.4.0
# via flake8
pygments==2.11.2
# via ipython
pyjwt[crypto]==2.7.0
# via msal
pylint==2.13.4
# via
# pylint-django
@ -409,9 +438,11 @@ redis==4.2.1
# django-redis
requests==2.27.1
# via
# azure-core
# caprover-api
# coreapi
# djangorestframework-stubs
# msal
# wagtail
s3transfer==0.6.0
# via boto3
@ -422,8 +453,11 @@ sentry-sdk==1.5.8
six==1.16.0
# via
# asttokens
# azure-core
# azure-identity
# django-coverage-plugin
# html5lib
# isodate
# l18n
# promise
# python-dateutil
@ -490,8 +524,10 @@ types-pytz==2021.3.6
# via django-stubs
types-pyyaml==6.0.5
# via django-stubs
typing-extensions==4.1.1
typing-extensions==4.5.0
# via
# azure-core
# azure-storage-blob
# django-stubs
# django-stubs-ext
# djangorestframework-stubs

View File

@ -25,6 +25,7 @@ django-ratelimit
django-ipware
django-csp
django-storages
django-storages[azure]
django-notifications-hq
django-jsonform
@ -42,4 +43,7 @@ wagtail-factories>=4
wagtail-localize>=1.5
wagtail_grapple>=0.19.2
azure-storage-blob
azure-identity
boto3

View File

@ -1,6 +1,6 @@
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# pip-compile --output-file=requirements.txt requirements.in
#
@ -22,6 +22,16 @@ attrs==21.4.0
# via jsonschema
authlib==1.0.0
# via -r requirements.in
azure-core==1.26.4
# via
# azure-identity
# azure-storage-blob
azure-identity==1.13.0
# via -r requirements.in
azure-storage-blob==12.16.0
# via
# -r requirements.in
# django-storages
beautifulsoup4==4.9.3
# via wagtail
boto3==1.26.11
@ -50,7 +60,12 @@ click==8.1.1
concurrent-log-handler==0.9.20
# via -r requirements.in
cryptography==36.0.2
# via authlib
# via
# authlib
# azure-identity
# azure-storage-blob
# msal
# pyjwt
deprecated==1.2.13
# via redis
dj-database-url==1.0.0
@ -104,7 +119,7 @@ django-ratelimit==3.0.1
# via -r requirements.in
django-redis==5.2.0
# via -r requirements.in
django-storages==1.13.1
django-storages[azure]==1.13.1
# via -r requirements.in
django-taggit==2.1.0
# via wagtail
@ -154,6 +169,8 @@ idna==3.3
# requests
inflection==0.5.1
# via drf-spectacular
isodate==0.6.1
# via azure-storage-blob
jmespath==1.0.1
# via
# boto3
@ -166,6 +183,12 @@ l18n==2021.3
# via wagtail
marshmallow==3.15.0
# via environs
msal==1.22.0
# via
# azure-identity
# msal-extensions
msal-extensions==1.0.0
# via azure-identity
openpyxl==3.1.2
# via wagtail
packaging==21.3
@ -179,13 +202,17 @@ pillow==9.0.1
polib==1.1.1
# via wagtail-localize
portalocker==2.4.0
# via concurrent-log-handler
# via
# concurrent-log-handler
# msal-extensions
promise==2.3
# via graphene-django
psycopg2-binary==2.9.3
# via -r requirements.in
pycparser==2.21
# via cffi
pyjwt[crypto]==2.7.0
# via msal
pyparsing==3.0.7
# via packaging
pyrsistent==0.18.1
@ -221,7 +248,10 @@ redis==4.2.1
# -r requirements.in
# django-redis
requests==2.27.1
# via wagtail
# via
# azure-core
# msal
# wagtail
s3transfer==0.6.0
# via boto3
sendgrid==6.9.7
@ -230,7 +260,10 @@ sentry-sdk==1.5.8
# via -r requirements.in
six==1.16.0
# via
# azure-core
# azure-identity
# html5lib
# isodate
# l18n
# promise
# python-dateutil
@ -252,8 +285,11 @@ text-unidecode==1.3
# via
# graphene-django
# python-slugify
typing-extensions==4.2.0
# via wagtail-localize
typing-extensions==4.5.0
# via
# azure-core
# azure-storage-blob
# wagtail-localize
uritemplate==4.1.1
# via drf-spectacular
urllib3==1.26.9