diff --git a/dvc/fs/gdrive.py b/dvc/fs/gdrive.py index 327462426..2d2c92b54 100644 --- a/dvc/fs/gdrive.py +++ b/dvc/fs/gdrive.py @@ -3,6 +3,7 @@ import logging import os import posixpath import re +import tempfile import threading from collections import defaultdict from contextlib import contextmanager @@ -208,8 +209,17 @@ class GDriveFileSystem(BaseFileSystem): from pydrive2.auth import GoogleAuth from pydrive2.drive import GoogleDrive - if os.getenv(GDriveFileSystem.GDRIVE_CREDENTIALS_DATA): - with open(self._gdrive_user_credentials_path, "w") as cred_file: + temporary_save_path = self._gdrive_user_credentials_path + is_credentials_temp = os.getenv( + GDriveFileSystem.GDRIVE_CREDENTIALS_DATA + ) + if self._use_service_account: + temporary_save_path = os.path.join( + tempfile.gettempdir(), "google-creds.json" + ) + + if is_credentials_temp: + with open(temporary_save_path, "w") as cred_file: cred_file.write( os.getenv(GDriveFileSystem.GDRIVE_CREDENTIALS_DATA) ) @@ -217,6 +227,7 @@ class GDriveFileSystem(BaseFileSystem): auth_settings = { "client_config_backend": "settings", "save_credentials": True, + "save_credentials_backend": "file", "save_credentials_file": self._gdrive_user_credentials_path, "get_refresh_token": True, "oauth_scope": [ @@ -226,13 +237,16 @@ class GDriveFileSystem(BaseFileSystem): } if self._use_service_account: - auth_settings["save_credentials_backend"] = "json" auth_settings["service_config"] = { "client_user_email": self._service_account_user_email, "client_json_file_path": self._service_account_json_file_path, } + if is_credentials_temp: + auth_settings["service_config"][ + "client_json_file_path" + ] = temporary_save_path + else: - auth_settings["save_credentials_backend"] = "file" auth_settings["client_config"] = { "client_id": self._client_id or self.DEFAULT_GDRIVE_CLIENT_ID, "client_secret": self._client_secret @@ -267,8 +281,8 @@ class GDriveFileSystem(BaseFileSystem): except Exception as exc: raise GDriveAuthError(self.credentials_location) from exc finally: - if os.getenv(GDriveFileSystem.GDRIVE_CREDENTIALS_DATA): - os.remove(self._gdrive_user_credentials_path) + if is_credentials_temp: + os.remove(temporary_save_path) return GoogleDrive(gauth) diff --git a/setup.py b/setup.py index a256e04d7..6bba528b6 100644 --- a/setup.py +++ b/setup.py @@ -95,7 +95,7 @@ install_requires = [ # Extra dependencies for remote integrations gs = ["gcsfs>=0.7.2"] -gdrive = ["pydrive2>=1.7.3", "six >= 1.13.0"] +gdrive = ["pydrive2>=1.8.1", "six >= 1.13.0"] s3 = ["boto3>=1.9.201"] azure = ["adlfs>=0.6.3", "azure-identity>=1.4.0", "knack"] # https://github.com/Legrandin/pycryptodome/issues/465