本文整理汇总了Python中google.oauth2.service_account.Credentials.from_service_account_info方法的典型用法代码示例。如果您正苦于以下问题:Python Credentials.from_service_account_info方法的具体用法?Python Credentials.from_service_account_info怎么用?Python Credentials.from_service_account_info使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.oauth2.service_account.Credentials
的用法示例。
在下文中一共展示了Credentials.from_service_account_info方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _lastBackupFile
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def _lastBackupFile(credentials: dict, bucket_name: str, key: str) -> str:
"""
Gets the name of the last backup file in the bucket.
:param credentials: The Google cloud storage service credentials retrieved from the Kubernetes secret.
:param bucket_name: The name of the bucket.
:param key: The prefix of tha backups
:return: The location of the last backup file.
"""
credentials = ServiceCredentials.from_service_account_info(credentials)
gcs_client = StorageClient(credentials.project_id, credentials)
bucket = gcs_client.get_bucket(bucket_name)
blobs = bucket.list_blobs(prefix=key)
last_blob = None
for blob in blobs:
logging.info("Found backup file '%s' in bucket '%s'", blob.name, bucket_name)
if last_blob is None or blob.time_created > last_blob.time_created:
last_blob = blob
logging.info("Returning backup file %s", last_blob.name.replace(key, ""))
return last_blob.name.replace(key, "") if last_blob else None
示例2: _downloadFile
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def _downloadFile(credentials: dict, bucket_name: str, key: str, file_name: str) -> str:
"""
Downloads a file from cloud storage.
:param credentials: The Google cloud storage service credentials retrieved from the Kubernetes secret.
:param bucket_name: The name of the bucket.
:param key: The key to download the file from the cloud storage.
:param file_name: The file that will be downloaded.
:return: The location of the downloaded file.
"""
credentials = ServiceCredentials.from_service_account_info(credentials)
gcs_client = StorageClient(credentials.project_id, credentials)
bucket = gcs_client.get_bucket(bucket_name)
logging.info("Going to download gcs://%s/%s", bucket_name, key)
bucket.blob(key).download_to_filename(file_name)
logging.info("Backup gcs://%s/%s downloaded to %s", bucket_name, key, file_name)
return file_name
示例3: get_bucket
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def get_bucket(service_account):
"""
Build a Google Cloud Storage client & bucket
from Taskcluster secret
"""
assert isinstance(service_account, dict)
# Load credentials from Taskcluster secret
if "bucket" not in service_account:
raise KeyError("Missing bucket in GOOGLE_CLOUD_STORAGE")
bucket = service_account["bucket"]
# Use those credentials to create a Storage client
# The project is needed to avoid checking env variables and crashing
creds = Credentials.from_service_account_info(service_account)
client = gcp_storage.Client(project=creds.project_id, credentials=creds)
return client.get_bucket(bucket)
示例4: _credentials_storage_service
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def _credentials_storage_service():
if RE_CREDENTIALS_JSON.match(UI_SERVICE):
credentials = Credentials.from_service_account_info(json.loads(UI_SERVICE))
else:
credentials = Credentials.from_service_account_file(UI_SERVICE)
return discovery.build('storage', 'v1', credentials=credentials)
示例5: CredentialsServiceWrapper
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def CredentialsServiceWrapper(service):
if isinstance(service, dict):
return CredentialsService.from_service_account_info(service)
elif RE_CREDENTIALS_JSON.match(service):
return CredentialsService.from_service_account_info(json.loads(service))
else:
return CredentialsService.from_service_account_file(service)
示例6: _load_oauth_credentials
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def _load_oauth_credentials(self):
json_str = os.environ.get(self.gcs_credential_name)
if not json_str:
return None
if os.path.isfile(json_str):
return Credentials.from_service_account_file(json_str)
return Credentials.from_service_account_info(json.loads(json_str))
示例7: get_gc_credentials
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def get_gc_credentials(key_path=None, keyfile_dict=None, scopes=None):
"""
Returns the Credentials object for Google API
"""
key_path = key_path or get_key_path()
keyfile_dict = keyfile_dict or get_keyfile_dict()
scopes = scopes or get_scopes()
if scopes is not None:
scopes = [s.strip() for s in scopes.split(',')]
else:
scopes = DEFAULT_SCOPES
if not key_path and not keyfile_dict:
logger.info('Getting connection using `google.auth.default()` '
'since no key file is defined for hook.')
credentials, _ = google.auth.default(scopes=scopes)
elif key_path:
# Get credentials from a JSON file.
if key_path.endswith('.json'):
logger.info('Getting connection using a JSON key file.')
credentials = Credentials.from_service_account_file(
os.path.abspath(key_path), scopes=scopes)
else:
raise PolyaxonStoresException('Unrecognised extension for key file.')
else:
# Get credentials from JSON data.
try:
if not isinstance(keyfile_dict, Mapping):
keyfile_dict = json.loads(keyfile_dict)
# Convert escaped newlines to actual newlines if any.
keyfile_dict['private_key'] = keyfile_dict['private_key'].replace('\\n', '\n')
credentials = Credentials.from_service_account_info(keyfile_dict, scopes=scopes)
except ValueError: # json.decoder.JSONDecodeError does not exist on py2
raise PolyaxonStoresException('Invalid key JSON.')
return credentials
示例8: dummy_service_account
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def dummy_service_account():
global _DUMMY_SERVICE_ACCOUNT
from google.oauth2.service_account import Credentials
if _DUMMY_SERVICE_ACCOUNT is None:
_DUMMY_SERVICE_ACCOUNT = Credentials.from_service_account_info(
_SERVICE_ACCOUNT_JSON
)
return _DUMMY_SERVICE_ACCOUNT
示例9: _uploadFile
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def _uploadFile(credentials: dict, bucket_name: str, key: str, file_name: str) -> None:
"""
Uploads a file to cloud storage.
:param credentials: The Google cloud storage service credentials retrieved from the Kubernetes secret.
:param bucket_name: The name of the bucket.
:param key: The key to save the file in the cloud storage.
:param file_name: The local file that will be uploaded.
"""
credentials = ServiceCredentials.from_service_account_info(credentials)
gcs_client = StorageClient(credentials.project_id, credentials)
bucket = gcs_client.bucket(bucket_name)
bucket.blob(key).upload_from_filename(file_name)
logging.info("Backup uploaded to gcs://%s/%s", bucket_name, key)
示例10: get_gc_credentials
# 需要导入模块: from google.oauth2.service_account import Credentials [as 别名]
# 或者: from google.oauth2.service_account.Credentials import from_service_account_info [as 别名]
def get_gc_credentials(
key_path=None, keyfile_dict=None, scopes=None, context_path: Optional[str] = None
):
"""
Returns the Credentials object for Google API
"""
key_path = key_path or get_key_path(context_path=context_path)
keyfile_dict = keyfile_dict or get_keyfile_dict(context_path=context_path)
scopes = scopes or get_scopes(context_path=context_path)
if scopes is not None:
scopes = [s.strip() for s in scopes.split(",")]
else:
scopes = DEFAULT_SCOPES
if not key_path and not keyfile_dict:
# Look for default GC path
if os.path.exists(CONTEXT_MOUNT_GC):
key_path = CONTEXT_MOUNT_GC
if not key_path and not keyfile_dict:
logger.info(
"Getting connection using `google.auth.default()` "
"since no key file is defined for hook."
)
credentials, _ = google.auth.default(scopes=scopes)
elif key_path:
# Get credentials from a JSON file.
if key_path.endswith(".json"):
logger.info("Getting connection using a JSON key file.")
credentials = Credentials.from_service_account_file(
os.path.abspath(key_path), scopes=scopes
)
else:
raise PolyaxonStoresException("Unrecognised extension for key file.")
else:
# Get credentials from JSON data.
try:
if not isinstance(keyfile_dict, Mapping):
keyfile_dict = json.loads(keyfile_dict)
# Convert escaped newlines to actual newlines if any.
keyfile_dict["private_key"] = keyfile_dict["private_key"].replace(
"\\n", "\n"
)
credentials = Credentials.from_service_account_info(
keyfile_dict, scopes=scopes
)
except ValueError: # json.decoder.JSONDecodeError does not exist on py2
raise PolyaxonStoresException("Invalid key JSON.")
return credentials