Django Collectstatic with AWS S3 not copying correct static and media files

1.1k views Asked by At

I am trying to copy all of my static and media files to an S3 bucket. Unfortunately despite the fact that my media root and static root are provide in my settings.py file....

DEFAULT_FILE_STORAGE = 'jeffrey.aws_storage_classes.MediaStorage'
AWS_ACCESS_KEY_ID = 'keyhere'
AWS_SECRET_ACCESS_KEY = 'secretkeyhere'
AWS_STORAGE_BUCKET_NAME = 'bucketname'
STATICFILES_STORAGE = 'jeffrey.aws_storage_classes.StaticStorage'

AWS_S3_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
STATIC_URL = "https://%s/static/" % AWS_S3_DOMAIN
MEDIA_URL = "https://%s/media/" % AWS_S3_DOMAIN

MEDIA_ROOT = u'/home/namehere/mysite/media'
STATIC_ROOT = u'/home/namehere/mysite/static'

....files from my django contrib folder seem to be being copied, and my files in static and media folders are not. Below is my bash console:

Type 'yes' to continue, or 'no' to cancel: yes
Copying '/usr/local/lib/python2.7/dist-        packages/django/contrib/admin/static/admin/css/fonts.css'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/icon-addlink.svg'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/icon-no.svg'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/inline-delete.svg'
....
....
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/gis/move_vertex_on.svg'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/gis/move_vertex_off.svg'
61 static files copied.
1

There are 1 answers

0
Dirk R On

I think the problem lies with your INSTALLED_APPS setting.

It is not wrong for it to copy files from django contrib folder - if you have 'django.contrib.admin' inside of your INSTALLED_APPS setting then it will copy the static files for this such as you have listed above. (This is the Django admin site).

If you don't have your app listed in INSTALLED_APPS then it will skip over collecting static files from your app's folder. I managed to reproduce your problem exactly by removing my app name from this setting, and without a doubt, suddenly it only copied files from django contrib and skipped over my apps static files. So even if your problem turns out to be different, this will have same effect as you described.

Example: In settings.py you need to ensure you have something that looks like this:

INSTALLED_APPS = [
    # Add your apps here to enable them
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'storages',                     #Amazon S3
    'myapp',
]

Note that 'myapp' will be whatever the name of your app is.