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
<https://i.stack.imgur.com/fB4k6.png>
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.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/2d185bb6-e5e1-4c05-a1d5-2f1b3d74311an%40googlegroups.com.