jschneier / django-storages

https://django-storages.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
2.74k stars 859 forks source link

Some Static URLs are malformed (missing location part) for some libraries. #1389

Closed KaivG closed 3 months ago

KaivG commented 5 months ago

Edit (Fix found): I found the solution to get it working. I was missing AWS_LOCATION = STATIC_LOCATION. Adding that line made the static path work correctly for all libraries. It seems a bit confusing however, as AWS_LOCATION to me sounds like a generic location, not just for static files. However, now the static and media paths are both working. I would rather have thought that specifying the location in STORAGES should be sufficient, or that AWS_LOCATION should be a generic path that is applied for all data stored including media files. If this is all expected behavior, it may help to clarify this in the docs.

Original issue: This issue seems to be related to other ones, but I haven't seen this specific problem raised.

I have the following config to use an s3 for static and media files with cloudfront in django 4.2:

CLOUDFRONT_DOMAIN = os.environ.get("CLOUDFRONT_DOMAIN") APP_VERSION = "1-0" STATIC_LOCATION = "static/" + APP_VERSION STATIC_URL = f'https://{CLOUDFRONT_DOMAIN}/{STATIC_LOCATION}/'

PUBLIC_MEDIA_LOCATION = "media" MEDIA_URL = f"{CLOUDFRONT_DOMAIN}/{PUBLIC_MEDIA_LOCATION}/"

AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME") AWS_S3_CUSTOM_DOMAIN = CLOUDFRONT_DOMAIN AWS_S3_REGION_NAME = 'af-south-1'

STORAGES = { "default": { "BACKEND": "storages.backends.s3boto3.S3Boto3Storage", "OPTIONS": { "location": "media", }, }, "staticfiles": { "BACKEND": "storages.backends.s3boto3.S3Boto3Storage", "OPTIONS": { "location": STATIC_LOCATION, }, }, }

What works:

What doesn't work:

I would very much appreciate a fix or work-around, as this is currently preventing me from using s3 for static files.

jschneier commented 5 months ago

The solution seems to indicate that the way BaseStorage handles settings is buggy, can you try to confirm that?

jschneier commented 5 months ago

Do you have staticfiles in your INSTALLED_APPS?

https://github.com/django/django/blob/85c154da2f07485a1cdc4d886eee4c1a1ef56137/django/templatetags/static.py#L126-L129

jschneier commented 5 months ago

Can you share a bit more details? I strongly suspect you are hitting the comment on this bug that was fixed. In short, are you on 4.2.5+ and also do you have another package that hasn't transitioned to the new STORAGES setting?

https://code.djangoproject.com/ticket/34773#comment:3

jschneier commented 3 months ago

Please reopen when there is more data, I simply cannot reproduce this or related issues.