1

I have Django application and I want to serve static and media files from AWS s3 bucket but after configuration only media files are served from s3 and static files are served locally. When I collect static all static files are collected in local staticfiles folder. No files are send to AWS s3 bucket. I had to copy them manually to s3.

I use Django==2.1.4, boto3==1.18.3, django-storages==1.11.1

Below I show the configuration in settings.py (some irrelevant parts are removed and I comment STATIC_URL and MEDIA_URL values that I have tried) I tried for example what was advised in this topic Django and S3 Bucket AWS Admin Static files.

import os
from decouple import config
import django_heroku

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = config('SECRET_KEY')

DEBUG = config('DEBUG', default=False, cast=bool)

ALLOWED_HOSTS = ['yogahouse-ap.herokuapp.com']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
    'storages',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'yogahouse.urls'

IMAGEKIT_URL = config('IMAGEKIT_URL')

AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_REGION_NAME = 'eu-central-1'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_STATIC_LOCATION = 'static'
DEFAULT_FILE_STORAGE = 'yogahouse.storages.MediaStorage'
AWS_S3_CUSTOM_DOMAIN = f"{AWS_STORAGE_BUCKET_NAME}.s3.{AWS_S3_REGION_NAME}.amazonaws.com"
AWS_IS_GZIPPED = True

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# STATIC_URL = f"{IMAGEKIT_URL}/static/"
# STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/"

MEDIA_URL = '/media/'
# MEDIA_URL = f"{IMAGEKIT_URL}/media/"
# MEDIA_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

django_heroku.settings(locals())

yogahouse.storages.py:

from storages.backends.s3boto3 import S3Boto3Storage


class MediaStorage(S3Boto3Storage):
    location = 'media'
    file_overwrite = False


class StaticStorage(S3Boto3Storage):
    location = 'static'
    file_overwrite = False
ann.piv
  • 681
  • 1
  • 7
  • 17

2 Answers2

1

The main issue was here:

ALLOWED_HOSTS = ['yogahouse-ap.herokuapp.com']

django_heroku.settings(locals())

After adding an argument staticfiles=False the files were served from s3.

For ALLOWED_HOSTS = [ ] static files would be also served from s3.

django_heroku.settings(locals(), staticfiles=False)

The url was still not correct, as I have static files 'static' folder in s3. This setting fix it:

AWS_LOCATION = 'static' 
ann.piv
  • 681
  • 1
  • 7
  • 17
  • Exactly, Without the staticfiles=False argument, Even collectstatic puts everything in a local dir names staticfiles. – nofoobar Apr 03 '22 at 06:06
0

Try the following in settings.py file-

STATICFILES_DIRS = [
    'mystatic',
]    
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_LOCATION = 'static'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
    # STATIC_URL = '/static/'

Knowledge shared from my learning and implementation from this video - https://youtu.be/4VIkWSZKMRA

Edit: Added value for STATICFILES_DIRS. I noticed you didn't set a value for that.

F A
  • 326
  • 2
  • 12
  • Thank you for the answer. I just fixed it. It was mostly that I configure heroku deploy, but didn't disable staticfiles. I will describe below in more details. It was also necessary to set AWS_LOCATION = 'static' as you wrote. Surprisingly for me changing STATIC_URL change nothing. I can put there anything and I don't see any impact. – ann.piv Jul 23 '21 at 18:09