Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
KaivG opened this issue Apr 28, 2024 · 3 comments
Open

Comments

@KaivG
Copy link

KaivG commented Apr 28, 2024

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:

  • Collectstatic puts the files in the right place on s3.
  • Some static files are correctly served with the correct url e.g. https://cdn.mycdn.com/static/1-0/debug_toolbar/css/print.css

What doesn't work:

  • Some libraries like the django admin and restframework don't form the static urls correctly and are trying to access the static files at the root of the CDN, not using the /static/1-0/ path: https://cdn.mycdn.com/admin/css/nav_sidebar.css and are failing accordingly.

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

@KaivG KaivG changed the title Static URLs are missing location part for some libraries. Some Static URLs are malformed (missing location part) for some libraries. Apr 28, 2024
@jschneier
Copy link
Owner

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

@jschneier
Copy link
Owner

jschneier commented Apr 30, 2024

@jschneier
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants