kiwis
kiwis

Reputation: 75

Django and S3 Bucket aws Admin Static files

I have a django project, i wanted to configure S3 Bucket in order to store the static files. Once create, the site loads but there files are not retrieved from the bucket so there is no CSS even for the admin page as in the screen shot : After activating the bucket from the settings.py

Here is the settings used for the bucket configurations :

STATIC_URL = '/static/'

MEDIA_URL = '/images/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')




AWS_ACCESS_KEY_ID = '************'
AWS_SECRET_ACCESS_KEY = '********************'
AWS_STORAGE_BUCKET_NAME = 'BUCKET_NAME'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

offcourse i added (storages) to the installed apps. The bucket policy (CORS) is set to :

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [
            "x-amz-server-side-encryption",
            "x-amz-request-id",
            "x-amz-id-2"
        ],
        "MaxAgeSeconds": 3000

I tried also (py manage.py collectstatic) without uploading manually, an admin folder appears in the bucket, but still even after using "Collectstatic" i still have the same problem (no css, no images, ...)

I'm running in circles and can't find a solution, i'd very much appreciate if someone could help.

Thank you in advance

Upvotes: 2

Views: 1402

Answers (1)

CYW
CYW

Reputation: 135

Try to add the full aws link to your media and static url:

STATIC_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION_MEDIA)

make sure to add these fields hope it works.

AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_REGION_NAME = 'us-east-2'
AWS_S3_ADDRESSING_STYLE = "virtual"

Also, checking the static file link from aws generated in the browser to see what the response is might help you further.

Upvotes: 1

Related Questions