pythondjangodjango-staticfilespython-django-storages

Django storages: Need authenticated way of reading static files from google cloud storage


I am trying to read static files from GCP storage using a service account key. The problem is while most of the requests are authenticated django-storages, some of the requests are public.

Developer console: Networks tab

And because of which I am getting a broken Django admin UI.

Broken Django admin UI

Here's my static file settings in settings.py file.

STATIC_URL = "/static/"
if DEPLOYED_URL:
    DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
    STATICFILES_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
    GS_BUCKET_NAME = env("GS_BUCKET_NAME")
    GS_CREDENTIALS = service_account.Credentials.from_service_account_info(
        json.loads(get_secret(PROJECT_NAME, "service_account_json"))
    )
    GS_DEFAULT_ACL = "projectPrivate"

My restrictions are I have Fine-grained: Object-level ACLs enabled bucket on which public access cannot be given.

PS: Since there are restrictions to the project I cannot use a public bucket. Alternate ways other than the usage of django-storages package are also appreciated. The only condition is reads should be authenticated and not public.


Solution

  • At the time of writing this, it's apparently an open bug related to django-storages but on AWS. But similar thing is happening on GCP on further inspection.

    I have already deployed my application using whitenoise to overcome this bug and have hosted my application on GCP cloud run.