Search code examples
djangoamazon-s3cdncnamedjango-staticfiles

Create custom domain CNAME for AWS S3 static and media files in Django


First would like to say I do not want to use Cloudfront.

Let's say site is example.com
Bucket Name S3: example
URL to static / media files: example.s3.amazonaws.com

What steps needs to be done to server static and media files over CNAME likes this: media.example.com

Also in DNS for the domain the CNAME media what should I point it to precisely?

Now the images are served like: https://example.s3.amazonaws.com/static/image.jpg
Would like to have it like this: https://media.example.com/static/image.jpg

Also the reason is that I can verify the CNAME in the webmaster tools.

production.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
# STATIC FILE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR('staticfiles'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
    str(APPS_DIR.path('static')),
)

# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#staticfiles-finders
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

# MEDIA CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(APPS_DIR('media'))

# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'

# DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
DEFAULT_FILE_STORAGE = 'config.custom_storages.MediaStorage'
THUMBNAIL_DEFAULT_STORAGE = 'config.custom_storages.MediaStorage'


AWS_ACCESS_KEY_ID = env('DJANGO_AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('DJANGO_AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('DJANGO_AWS_STORAGE_BUCKET_NAME')
AWS_S3_REGION_NAME = env('REGION_NAME')  # e.g. us-east-2
AWS_S3_CUSTOM_DOMAIN = '{}.s3.amazonaws.com'.format(AWS_STORAGE_BUCKET_NAME)
AWS_AUTO_CREATE_BUCKET = True
AWS_QUERYSTRING_AUTH = False
AWS_S3_CALLING_FORMAT = OrdinaryCallingFormat()
AWS_DEFAULT_ACL = "public-read"

# AWS cache settings, don't change unless you know what you're doing:
AWS_EXPIRY = 60 * 60 * 24 * 7

# TODO See: https://github.com/jschneier/django-storages/issues/47
# Revert the following and use str after the above-mentioned bug is fixed in
# either django-storage-redux or boto
AWS_HEADERS = {
    'Cache-Control': six.b('max-age=%d, s-maxage=%d, must-revalidate' % (
        AWS_EXPIRY, AWS_EXPIRY))
}

# URL that handles the media served from MEDIA_ROOT, used for managing
# stored files.
MEDIAFILES_LOCATION = 'media'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)


# Static Assests
# ------------------------
STATICFILES_STORAGE = 'config.custom_storages.CachedS3BotoStaticStorage'
COMPRESS_STORAGE = 'config.custom_storages.CachedS3BotoStaticStorage'

AWS_S3_SECURE_URLS = True

STATICFILES_LOCATION = 'static'
STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION)

Solution

  • If it helps someone - initially I was not going to use Cloudfront - but because of using HTTPS I had to. 2 great help docs how to set it up with @Michael-sqlbot and @hephalump help.

    1. https://aws.amazon.com/premiumsupport/knowledge-center/cloudfront-https-requests-s3/

    2. https://medium.com/@brodartec/hosting-a-static-site-with-https-enabled-using-aws-s3-cloudfront-and-godaddy-826dae41fdc6