當前位置: 首頁>>代碼示例>>Python>>正文


Python Credentials.from_service_account_info方法代碼示例

本文整理匯總了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 
開發者ID:Ultimaker,項目名稱:k8s-mongo-operator,代碼行數:23,代碼來源:RestoreHelper.py

示例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 
開發者ID:Ultimaker,項目名稱:k8s-mongo-operator,代碼行數:20,代碼來源:RestoreHelper.py

示例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) 
開發者ID:mozilla,項目名稱:code-coverage,代碼行數:20,代碼來源:gcp.py

示例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) 
開發者ID:google,項目名稱:starthinker,代碼行數:10,代碼來源:storage.py

示例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) 
開發者ID:google,項目名稱:starthinker,代碼行數:9,代碼來源:wrapper.py

示例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)) 
開發者ID:m3dev,項目名稱:gokart,代碼行數:11,代碼來源:gcs_config.py

示例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 
開發者ID:polyaxon,項目名稱:polystores,代碼行數:41,代碼來源:gc_client.py

示例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 
開發者ID:googleapis,項目名稱:python-storage,代碼行數:13,代碼來源:test__signing.py

示例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) 
開發者ID:Ultimaker,項目名稱:k8s-mongo-operator,代碼行數:15,代碼來源:BackupHelper.py

示例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 
開發者ID:polyaxon,項目名稱:polyaxon,代碼行數:55,代碼來源:base.py


注:本文中的google.oauth2.service_account.Credentials.from_service_account_info方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。