Django static files on S3: S3ResponseError: 301 moved forever

I try to host Django Static and Media files on Amazon S3 and I follow every guide there, but I still get S3ResponseError: 301 Moved Permanently errors when deploying my Elastic Beanstalk application when it tries to execute collectstatic .

My S3 works, and I can access other files on it. I also installed it in a user domain so that you can access the same file in the following ways:

This is the third option that I want to use, but I have tried others. Both with and without https:// in the settings below.

My settings file is as follows

 #settings.py file AWS_ACCESS_KEY_ID = 'XXX' AWS_SECRET_ACCESS_KEY = 'XXX' AWS_HEADERS = { 'Expires': 'Thu, 31 Dec 2099 20:00:00 GMT', 'Cache-Control': 'max-age=94608000', } AWS_STORAGE_BUCKET_NAME = 's3.condopilot.com' # I have also tried setting AWS_S3_CUSTOM_DOMAIN to the following: # - "s3-eu-west-1.amazonaws.com/%s/" % AWS_STORAGE_BUCKET_NAME # - "s3-eu-west-1.amazonaws.com/%s" % AWS_STORAGE_BUCKET_NAME # - "s3.condopilot.com" AWS_S3_CUSTOM_DOMAIN = "%s.s3-eu-west-1.amazonaws.com" % AWS_STORAGE_BUCKET_NAME AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat' AWS_S3_SECURE_URLS = False # Tried both True and False AWS_S3_URL_PROTOCOL = 'http' # Tried with and without STATICFILES_LOCATION = 'static' STATIC_URL = "http://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, STATICFILES_LOCATION) STATICFILES_STORAGE = 'custom_storages.StaticStorage' MEDIAFILES_LOCATION = 'media' MEDIA_URL = "http://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION) DEFAULT_FILE_STORAGE = 'custom_storages.MediaStorage' 

I have AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat' because without it I get the following error: ssl.CertificateError: hostname 's3.condopilot.com.s3.amazonaws.com' doesn't match either of '*.s3.amazonaws.com', 's3.amazonaws.com' . All the tips I find on the Internet regarding this error say that OrdinaryCallingFormat should be used when the bucket name contains s3.condopilot.com , for example s3.condopilot.com .

My user repositories look like

 #custom_storages.py from django.conf import settings from storages.backends.s3boto import S3BotoStorage class StaticStorage(S3BotoStorage): location = settings.STATICFILES_LOCATION class MediaStorage(S3BotoStorage): location = settings.MEDIAFILES_LOCATION 

And yes, my S3 bucket is configured in eu-west-1 .

+5
source share
1 answer

I think you do not need to set the S3 area to the URL, and if you use django-storage , replace this application with django-storages-redux . You do not need the custom_storages.py file.

Keep things simple. It's enough.

 from django.utils import six DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXX' AWS_SECRET_ACCESS_KEY = 'XxXxXxXxXxXxXxXxXxXxXxXxXxXxxXxX' AWS_STORAGE_BUCKET_NAME = 'bucket-name' AWS_AUTO_CREATE_BUCKET = False AWS_QUERYSTRING_AUTH = False AWS_EXPIRY = 60 * 60 * 24 * 7 AWS_HEADERS = { 'Cache-Control': six.b('max-age=%d, s-maxage=%d, must-revalidate' % ( AWS_EXPIRY, AWS_EXPIRY)) } MEDIA_URL = 'https://%s.s3.amazonaws.com/' % AWS_STORAGE_BUCKET_NAME STATICFILES_STORAGE = DEFAULT_FILE_STORAGE STATIC_URL = MEDIA_URL 
+2
source

All Articles