diff --git a/daras_ai_v2/settings.py b/daras_ai_v2/settings.py index 5e93349c2..d7408d062 100644 --- a/daras_ai_v2/settings.py +++ b/daras_ai_v2/settings.py @@ -240,11 +240,18 @@ # django-storages settings for google-cloud from google.oauth2 import service_account -GCS_CONFIG = config("GCS_CONFIG", default="") GCS_PRIVATE_KEY = config("GCS_PRIVATE_KEY", default="") -GCS_CREDENTIALS = service_account.Credentials.from_service_account_info( - info={**json.loads(GCS_CONFIG), "private_key": GCS_PRIVATE_KEY} -) + +GCS_CREDENTIALS = "" +# create credentials from env vars +try: + GCS_CONFIG = config("GCS_CONFIG") +except UndefinedValueError: + pass +else: + GCS_CREDENTIALS = service_account.Credentials.from_service_account_info( + info={**json.loads(GCS_CONFIG), "private_key": GCS_PRIVATE_KEY} + ) STORAGES["default"] = { "BACKEND": "storages.backends.gcloud.GoogleCloudStorage", diff --git a/daras_ai_v2/static_pages.py b/daras_ai_v2/static_pages.py index 9f93d754a..6cb5e7401 100644 --- a/daras_ai_v2/static_pages.py +++ b/daras_ai_v2/static_pages.py @@ -1,5 +1,4 @@ import io -import gooey_ui as st from static_pages.models import StaticPage from google.cloud import storage @@ -7,11 +6,13 @@ from daras_ai_v2.settings import GCP_PROJECT, GCS_CREDENTIALS, GS_BUCKET_NAME -client = storage.Client( - GCP_PROJECT, - GCS_CREDENTIALS, -) -bucket = client.get_bucket(GS_BUCKET_NAME) +def gcs_bucket() -> "storage.storage.Bucket": + client = storage.Client( + GCP_PROJECT, + GCS_CREDENTIALS, + ) + bucket = client.get_bucket(GS_BUCKET_NAME) + return bucket def populate_imported_css(html: str, uid: str): @@ -41,7 +42,7 @@ def get_all_styles(links: list, uid: str): for link in links: if not link.endswith(".css"): # ignore for css files continue - blob = bucket.get_blob(f"{uid}/{link}") + blob = gcs_bucket().get_blob(f"{uid}/{link}") blob = blob.download_as_string() blob = blob.decode("utf-8") blob = io.StringIO(blob).read() @@ -57,6 +58,7 @@ def serve(page_slug: str, file_path: str = None): return None uid = static_page.uid + bucket = gcs_bucket() def render_page(): if file_path: