Skip to content

Commit

Permalink
fix: set a project_id manually (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored Jan 30, 2025
2 parents bb3b372 + d4ca07f commit 4de0d8d
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions pytest_gee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import json
import os
import re
import tempfile
from pathlib import Path
from typing import Union

Expand Down Expand Up @@ -79,23 +78,22 @@ def init_ee_from_service_account():
pattern = re.compile(r"^'[^']*'$")
private_key = private_key[1:-1] if pattern.match(private_key) else private_key

# connect to GEE using a temp file to avoid writing the key to disk
with tempfile.TemporaryDirectory() as temp_dir:
file = Path(temp_dir) / "private_key.json"
file.write_text(private_key)
ee_user = json.loads(private_key)["client_email"]
credentials = ee.ServiceAccountCredentials(ee_user, str(file))
ee.Initialize(credentials=credentials, http_transport=httplib2.Http())
# connect to GEE using a a ServiceAccountCredential object based on the
# private key data
ee_user = json.loads(private_key)["client_email"]
credentials = ee.ServiceAccountCredentials(ee_user, key_data=private_key)
ee.Initialize(
credentials=credentials, project=credentials.project_id, http_transport=httplib2.Http()
)

elif "EARTHENGINE_PROJECT" in os.environ:
# if the user is in local development the authentication should already be available
# we simply need to use the provided project name
ee.Initialize(project=os.environ["EARTHENGINE_PROJECT"], http_transport=httplib2.Http())

else:
raise ValueError(
"EARTHENGINE_SERVICE_ACCOUNT or EARTHENGINE_PROJECT environment variable is missing"
)
msg = "EARTHENGINE_SERVICE_ACCOUNT or EARTHENGINE_PROJECT environment variable is missing"
raise ValueError(msg)


@deprecated(version="0.3.5", reason="Use the vanilla GEE ``wait_for_task`` function instead.")
Expand Down

0 comments on commit 4de0d8d

Please sign in to comment.