I need to use Amazon S3 to serve my static and media files to my Django project.
However, I am facing a lot of issues with that. First, my code:
s3utils.py
from storages.backends.s3boto import S3BotoStorage
class FixedS3BotoStorage(S3BotoStorage):
def url(self, name):
url = super(FixedS3BotoStorage, self).url(name)
if name.endswith('/') and not url.endswith('/'):
url += '/'
return url
StaticS3BotoStorage = lambda: FixedS3BotoStorage(location='static')
MediaS3BotoStorage = lambda: FixedS3BotoStorage(location='media')
In settings.py
DEFAULT_FILE_STORAGE = 'SpareGuru.s3utils.MediaS3BotoStorage'
STATICFILES_STORAGE = 'SpareGuru.s3utils.StaticS3BotoStorage'
AWS_HOST = "s3-ap-southeast-1.amazonaws.com"
AWS_ACCESS_KEY_ID = 'xx'
AWS_SECRET_ACCESS_KEY = 'yy'
AWS_STORAGE_BUCKET_NAME = 'zz'
S3_URL = 'http://%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
MEDIA_DIRECTORY = '/media/'
STATIC_DIRECTORY = '/static/'
STATIC_URL = "/static/"
MEDIA_URL = "/media/"
STATIC_ROOT = S3_URL + STATIC_DIRECTORY
COMPRESS_ROOT = STATIC_ROOT
MEDIA_ROOT = S3_URL + MEDIA_DIRECTORY
Here are the issues I face:
When running
./manage.py collectstatic
, it starts to upload the files to S3 and after a couple of files, I getBroken Pipe
error.When trying to run the webpage, I get the error:
'https://zz.s3.amazonaws.com/static/css/bootstrap.min.css?Signature=sign&Expires=1438359937&AWSAccessKeyId=xx' isn't accessible via COMPRESS_URL ('/static/') and can't be compressed
.
No idea what's going on here.
Edit: My previous bucket policy allowed for Read only access. So maybe that could have been the reason that my compressor was unable to create files on S3. I have updated the policy but still doesn't work. Updated policy is:
{
"Statement": [
{
"Sid": "PublicReadForGetBucketObjects",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::zz/*"
]
},
{
"Action": "s3:*",
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::zz",
"arn:aws:s3:::zz/*"
],
"Principal": {
"AWS": [
"my-arn:/goes=here"
]
}
}
]
}
And the CORS Configuration
for my bucket is:
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>