NotSoShabby
NotSoShabby

Reputation: 3688

django storages breaks the admin staticfiles

I tried moving from local static files to S3 using django-storages. I followed the documentation carefully but still there is no access to the static files.

In the local environment I have:

STATIC_URL = '/static/'

in the settings.py and everything works fine.

when I add all the S3 params as the documentation shows:

STATIC_URL = 'https://django-main.s3.amazonaws.com/'
ADMIN_MEDIA_PREFIX = 'https://django-main.s3.amazonaws.com/admin/' # tried with this and also without this



DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'


AWS_ACCESS_KEY_ID = '<AWS_ACCESS_KEY_ID>'
AWS_SECRET_ACCESS_KEY = '<AWS_SECRET_ACCESS_KEY>'
AWS_STORAGE_BUCKET_NAME = 'bucket-name'

I ran python manage.py collectstatic which seemed to work fine and uploaded the static files to the bucket.

but running the server and going to the admin page it looks like this:

enter image description here

which is because it doesn't have access to the static files. No error is thrown/shown

Any ideas?

EDIT:

So apperently I'm getting a forbbiden call: enter image description here

but I changed my settings.py to:

AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')

and made sure that access is available (put a breakpoint and downloaded a css file using boto3 from that bucket using these exact environment variables and still no solution

Upvotes: 0

Views: 1305

Answers (2)

Amiay Narayan
Amiay Narayan

Reputation: 529

Direction suggested by NotSoShabby is right. To further expand on this,

  • Run python manage.py collectstatic (will take some time, as it will upload the static files to your bucket AWS_STORAGE_BUCKET_NAME, patience in software dev. is the key and life in general ;))
  • Check that the files have been added to AWS_STORAGE_BUCKET_NAME/admin/ prefix, if everthing is set up properly.
  • Allow public access to the bucket (under the Permission tab) [without this you will not able to added bucket policy]
  • Add appropriate policy to the bucket. GetObject permission should be enough for admin/ prefix. For reference, I am adding my policy:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPublicAccessForStaticDjangoAdminFiles",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::AWS_STORAGE_BUCKET_NAME/admin/*"
        }
    ]
}

Upvotes: 2

NotSoShabby
NotSoShabby

Reputation: 3688

The issue was that the bucket read permissions were not public. Changing the permissions worked

Upvotes: 0

Related Questions