2

I'm configuring my Django project to use S3 buckets to store static and media files, both for local and production settings.

My project tree is as follows:

   src/
      blog/
          settings/
                  __init__
                  local.py
                  production.py
          s3utils.py
          [..]
      [..]

My local.py:

access_key = "xx"
secret_key = "yy"

AWS_ACCESS_KEY_ID = access_key
AWS_SECRET_ACCESS_KEY = secret_key
AWS_STORAGE_BUCKET_NAME = 'zz'


STATICFILES_STORAGE = 'blog.s3utils.StaticRootS3BotoStorage'
DEFAULT_FILE_STORAGE = 'blog.s3utils.MediaRootS3BotoStorage'
S3DIRECT_REGION = 'us-west-2'
S3_URL = 'http://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME
MEDIA_URL = 'http://%s.s3.amazonaws.com/media/' % AWS_STORAGE_BUCKET_NAME
MEDIA_ROOT = MEDIA_URL
STATIC_URL = S3_URL + "/static/"
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

import datetime

two_months = datetime.timedelta(days=61)
date_two_months_later = datetime.date.today() + two_months
expires = date_two_months_later.strftime("%A, %d %B %Y 20:00:00 GMT")

AWS_HEADERS = { 
    'Expires': expires,
    'Cache-Control': 'max-age=%d' % (int(two_months.total_seconds()), ),
}

my s3utils.py:

from storages.backends.s3boto import S3BotoStorage

StaticRootS3BotoStorage = lambda: S3BotoStorage(location='static') 
MediaRootS3BotoStorage  = lambda: S3BotoStorage(location='media') 

When I run:

python manage.py collectstatic

only (django) admin static files are copied to my s3 bucket.

I thought that the problem was that I misconfigured the permission of my IAM user, but actually I do have the permissions to copy the 'admin/' files to the s3 bucket.

Thank you for any help you can provide.

1 Answer 1

2

Add STATICFILES_DIRS to your settings.

STATICFILES_DIRS = [
    "/path/to/your/static",
]

https://docs.djangoproject.com/en/1.10/ref/settings/#std:setting-STATICFILES_DIRS

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Rafael, you made my day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.