Using Amazon AWS to serve files
This episode covers :-
1) How to serve static assets and user-uploaded files from an Amazon bucket.
- Setup :-
Use the project from the “Heroku Deployment” chapter to test this.
- Creating an Amazon AWS bucket :-
Visit https://samuli.to/AWS and create an account.
Visit https://samuli.to/S3 and add a bucket:
Click Next for the rest of the settings and hit Create bucket.
- Setting up permissions :-
Visit Services and click IAM under the Security, Identity & Compliance label:
Click Users and Add user:
Check Programmatic access:
Create a new group:
Check AmazonS3FullAccess:
- Updating settings.py file :-
Update settings.py file and add the configuration:
mysite/settings.py
django_heroku.settings(locals())
AWS_ACCESS_KEY_ID = 'ACCESS_KEY'
AWS_SECRET_ACCESS_KEY = 'SECRET'
AWS_STORAGE_BUCKET_NAME = 'sn-test-01'
AWS_DEFAULT_ACL = None
AWS_LOCATION = 'static'
AWS_MEDIA_LOCATION = 'media'
STATIC_URL = 'https://%s.s3.amazonaws.com/%s/' % (AWS_STORA\
GE_BUCKET_NAME, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Sto\
rage'
DEFAULT_FILE_STORAGE = 'mysite.storages.MediaStorage'
try:
from .local_settings import *
except ImportError:
pass
Create a storages.py file and fill it with these lines:
mysite/storages.py
from django.conf import settings
from storages.backends.s3boto3 import S3Boto3Storage
class MediaStorage(S3Boto3Storage):
location = settings.AWS_MEDIA_LOCATION
file_overwrite = False
- Adding an image field to the Post model :-
Edit blog app models.py file and add an ImageField:
blog/models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=255, default='')
image = models.ImageField(default='', blank=True, uploa\
d_to='images') # < here
- Installing packages :-
Install packages and push:
Terminal
pip install django-storages boto3 pillow
python manage.py makemigrations
python manage.py migrate
pip freeze > requirements.txt
git add .
git commit -m "Add django-storages, boto3, pillow and Post \
model image field"
git push heroku master
heroku run python manage.py migrate
Visit the production site in https://YOUR_APP.herokuapp.com/ and create a Post with an image.
The post image will be now served from an URL like this: sn-test-
01.amazonaws.com/media/images/Agapanthus.png
Open the page source code and you will see that the static files are now served from URLS like this: sn-test-01.s3.amazonaws.com/static/admin/css/base.css
In the bucket folder you now have separate folders for media and static files:
Summary :-
- Boto3 is an Amazon software development kit that allows Python programs to use services like Amazon S3.
- It’s not uncommon to serve static assets and user-uploaded files from external sources.
- Amazon S3 can also be integrated with a content delivery network like Amazon CloudFront https://samuli.to/Amazon-CloudFront.
👈Episode 27(P). Episode 29(P)👉
Share this Post
PRINT THIS POST














No comments:
Post a Comment
If you have any doubts. Please let me know.