0

My website index.html style broken after I configure aws S3. I added following code in my settings.py:

AWS_S3_ACCESS_KEY_ID = "my_key"
AWS_S3_SECRET_ACCESS_KEY = "my_key"
AWS_STORAGE_BUCKET_NAME = "my_bucket_name"
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
AWS_DEFAULT_ACL = "public-read" 

INSTALLED_APPS = [
        'storages',
]

AWS_LOCATION = 'static'
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

when I am opening any image url or stylesheet url like this https://mydomain.com.s3.amazonaws.com/static/theme/assets/js/main.js I am seeing below message :

<Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>HHT43N24VA3Q7QHC</RequestId>
<HostId>z5UvXR8RJU5U9H4IY4f2ntuw1dqF6tk4Vts7s+riqaQI5FJT5/H+u8TI8LnMFbH+uAUPwgR8Kp4=</HostId>
</Error>

should I put anything's in aws Bucket policy and Cross-origin resource sharing (CORS)?

updated1:

I added permission to public. I turned off Block all public access and also added Bucket policy and Cross-origin resource sharing (CORS).

Bucket policy

{
    "Version": "2012-10-17",
    "Id": "Policy294......",
    "Statement": [
        {
            "Sid": "Stmt859....",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:DeleteObject",
                "s3:GetObject"
            ],
            "Resource": "arn:aws:s3:::my-bucket-name/*"
        }
    ]
}

Cross-origin resource sharing (CORS)

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

now I am not seeing any error in url and content serving from aws s3 but still now my site style broken. where I am doing wrong?

boyenec
  • 1,008
  • 3
  • 15
  • Yes, Make your bucket public or viewable to your Current IP Address. You can also make some folders public. Yes, (CORS) resource sharing policy is also required which you can make from here https://docs.aws.amazon.com/AmazonS3/latest/userguide/ManageCorsUsing.html . You also might want to look into Setting cache-control for files which you want your CDN to cache. Look here https://stackoverflow.com/questions/10435334/set-cache-control-for-entire-s3-bucket-automatically-using-bucket-policies – Dushyant Deshwal Oct 01 '21 at 01:04

0 Answers0