Skip to content

Commit

Permalink
let gcs auth work outside of gcp
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkEdmondson1234 committed Apr 23, 2024
1 parent 5dc4c03 commit e715dcc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages

# Define your base version
version = '0.46.3'
version = '0.46.4'

setup(
name='sunholo',
Expand Down
22 changes: 15 additions & 7 deletions sunholo/gcs/download_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@
from google.auth.exceptions import RefreshError

from ..logging import log
from ..utils.gcp import is_running_on_gcp

# Perform a refresh request to get the access token of the current credentials (Else, it's None)
gcs_credentials, project_id = google.auth.default()

# Prepare global variables for client reuse
gcs_client = storage.Client()
gcs_credentials = None
project_id = None
gcs_client = None
gcs_bucket_cache = {}

if is_running_on_gcp():
# Perform a refresh request to get the access token of the current credentials (Else, it's None)
gcs_credentials, project_id = google.auth.default()
# Prepare global variables for client reuse
gcs_client = storage.Client()

def refresh_credentials():
if not is_running_on_gcp():
log.info("Not running on Google Cloud so no credentials available for GCS.")
return False
if not gcs_credentials.token or gcs_credentials.expired or not gcs_credentials.valid:
try:
gcs_credentials.refresh(requests.Request())
except Exception as e:
log.error(f"Failed to refresh credentials: {e}")
log.error(f"Failed to refresh gcs credentials: {e}")
return False
return True
# do this at module load time

refresh_credentials()

def get_bucket(bucket_name):
Expand Down

0 comments on commit e715dcc

Please sign in to comment.