vendredi 31 juillet 2015

Unable to serve static media from S3 with Django

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:

  1. When running ./manage.py collectstatic, it starts to upload the files to S3 and after a couple of files, I get Broken Pipe error.

  2. When trying to run the webpage, I get the error: 'http://ift.tt/1IOXqGg' isn't accessible via COMPRESS_URL ('/static/') and can't be compressed.

No idea what's going on here.

To be more clearer, my bucket policy is:

{
"Version": "2008-10-17",
"Statement": [
    {
        "Sid": "AllowPublicRead",
        "Effect": "Allow",
        "Principal": {
            "AWS": "*"
        },
        "Action": "s3:GetObject",
        "Resource": [
            "arn:aws:s3:::zz/*",
            "arn:aws:s3:::zz"
        ]
    }
]
}




1 commentaire:

  1. It'sVery informative blog and useful article thank you for sharing with us , keep posting learn more
    AWS Online Training Hydeerabad

    RépondreSupprimer