Django AWS S3 403 (Forbidden)

472 views
Skip to first unread message

Sai Maruthi

unread,
Dec 20, 2020, 8:19:17 PM12/20/20
to Django users

I'm hosting my Static and Media Files of my Django Project on AWS s3. The static files for admin and the image files are working fine, but some of the static files CSS & JS are giving a 403 error(URL for reference- https://maruthi-static.s3.amazonaws.com/static/css/customer_view/main/main.css). I'm using boto3-1.16.40 and django-storages-1.11, with AWS IAM user with AmazonS3FullAccess permission. The following is my code.

Settings.py

# STORAGES 
# ------------------------------------------------------------------------------ 
AWS_ACCESS_KEY_ID = "----" 
AWS_SECRET_ACCESS_KEY = "----" 
AWS_STORAGE_BUCKET_NAME = "maruthi-static" 
AWS_QUERYSTRING_AUTH = False
 _AWS_EXPIRY = 60 * 60 * 24 * 7 
AWS_S3_OBJECT_PARAMETERS = { "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate" } 
AWS_S3_REGION_NAME = "us-east-2"
AWS_S3_CUSTOM_DOMAIN = None 
aws_s3_domain = f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com
AWS_DEFAULT_ACL = None

# STATIC
# --------------------------------------------------------------------------------------- 
AWS_STATIC_LOCATION = 'static' 
STATICFILES_STORAGE = "tps.storages.StaticRootS3Boto3Storage" 
COLLECTFAST_STRATEGY = "collectfast.strategies.boto3.Boto3Strategy"
STATIC_URL = f"https://{aws_s3_domain}/{AWS_S3_REGION_NAME}/static/" 

# MEDIA 
# ------------------------------------------------------------------------------ 
AWS_PUBLIC_MEDIA_LOCATION = 'media/public' 
DEFAULT_FILE_STORAGE = "tps.storages.MediaRootS3Boto3Storage" 
MEDIA_URL = f"https://{aws_s3_domain}/{AWS_S3_REGION_NAME}/media/"

AWS_PRIVATE_MEDIA_LOCATION = 'media/private' 
PRIVATE_FILE_STORAGE = 'mysite.storages.PrivateMediaRootS3Boto3Storage'

storages.py

from storages.backends.s3boto3 import S3Boto3Storage
from django.conf import settings


class StaticRootS3Boto3Storage(S3Boto3Storage):
    location = settings.AWS_STATIC_LOCATION
    default_acl = "public-read"


class MediaRootS3Boto3Storage(S3Boto3Storage):
    location = settings.AWS_PUBLIC_MEDIA_LOCATION
    file_overwrite = False
    
class PrivateMediaRootS3Boto3Storage(S3Boto3Storage):
    location = settings.AWS_PRIVATE_MEDIA_LOCATION
    default_acl = 'private'
    file_overwrite = False
    custom_domain = False

All my static and media files were uploaded to my s3 bucket when I ran collectstatic.

I have set the following permissions: bucket block-public-access settings

CORS

[ { "AllowedHeaders": [ "*" ], "AllowedMethods": [ "PUT", "POST", "GET" ], "AllowedOrigins": [ "*" ], "ExposeHeaders": [] } ]

 

apart from the above settings, I didn't set any bucket policies. If someone can help me I will be very grateful.

Sai Maruthi

unread,
Dec 21, 2020, 2:46:22 AM12/21/20
to Django users
Fixed! It's due to case sensitive paths in aws. 
Reply all
Reply to author
Forward
0 new messages