Django Deployment Part 2 - Integration of Django with AWS S3 Bucket-Storage to serve static files.

Опубликовано: 20 Сентябрь 2018
на канале: Ultimate Backend & AI
3,366
26

Part one of this series deployment is at    • Django 2.0 Deployment with Gunicorn, ...  

In this second video, we will be covering how to remove internal server error. Then we go through adding S3 bucket in our Django project to serve static files.

Obtain YOUR_ACCESS_TOKEN and YOUR_SECRET_TOKEN from AWS S3 Tokens settings or from digital ocean spaces configuration.
_______________________________________________________________________________________
Installations required:
(add 'storages' in Installed app in settings.py)

pip3 install django-storages boto3
_______________________________________________________________________________________
AWS settings to be added in settings.py file:

STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static_my_proj"),
]
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "static_root")

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn", "media_root")

buckets settings
AWS_ACCESS_KEY_ID = 'YOUR_ACCESS_TOKEN'
AWS_SECRET_ACCESS_KEY = 'YOUR_SECRET_TOKEN'
AWS_STORAGE_BUCKET_NAME = 'YOUR_SPACE_NAME'
AWS_S3_ENDPOINT_URL = 'https://sgp1.digitaloceanspaces.com'
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'

STATIC_URL = 'https://%s/%s/' % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
AWS_S3_CUSTOM_DOMAIN = '%s.sgp1.digitaloceanspaces.com' %AWS_STORAGE_BUCKET_NAME

DEFAULT_FILE_STORAGE = 'YOUR_DJANGO_PROJECT.utils.MediaRootS3BotoStorage'
STATICFILES_STORAGE = 'YOUR_DJANGO_PROJECT.utils.StaticRootS3BotoStorage'
_______________________________________________________________________________________

Make new utils.py file in the same directory where settings.py file is and add these:


from storages.backends.s3boto3 import S3Boto3Storage
StaticRootS3BotoStorage = lambda: S3Boto3Storage(location='static')
MediaRootS3BotoStorage = lambda: S3Boto3Storage(location='media')
_______________________________________________________________________________________
In last step:
Create a new folder named static_my_proj in same directory where manage.py is and then run
Python3 manage.py collectstatic

Now your static files will be served from digital ocean spaces.


Смотрите видео Django Deployment Part 2 - Integration of Django with AWS S3 Bucket-Storage to serve static files. онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Ultimate Backend & AI 20 Сентябрь 2018, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 3,36 раз и оно понравилось 2 людям.