4646
4747
4848def get_client (project_id , credentials = None , service_account = None ,
49- private_key = None , readonly = True , swallow_results = True ):
49+ private_key = None , private_key_file = None , readonly = True ,
50+ swallow_results = True ):
5051 """Return a singleton instance of BigQueryClient. Either
5152 AssertionCredentials or a service account and private key combination need
5253 to be provided in order to authenticate requests to BigQuery.
@@ -58,6 +59,9 @@ def get_client(project_id, credentials=None, service_account=None,
5859 service_account: the Google API service account name.
5960 private_key: the private key associated with the service account in
6061 PKCS12 or PEM format.
62+ private_key_file: the name of the file containing the private key
63+ associated with the service account in PKCS12 or PEM
64+ format.
6165 readonly: bool indicating if BigQuery access is read-only. Has no
6266 effect if credentials are provided.
6367 swallow_results: If set to false then return the actual response value
@@ -67,9 +71,13 @@ def get_client(project_id, credentials=None, service_account=None,
6771 an instance of BigQueryClient.
6872 """
6973
70- if not credentials and not (service_account and private_key ):
71- raise Exception ('AssertionCredentials or service account and private'
72- 'key need to be provided' )
74+ if not credentials :
75+ assert service_account and (private_key or private_key_file ), \
76+ 'Must provide AssertionCredentials or service account and key'
77+
78+ if private_key_file :
79+ with open (private_key_file , 'rb' ) as key_file :
80+ private_key = key_file .read ()
7381
7482 bq_service = _get_bq_service (credentials = credentials ,
7583 service_account = service_account ,
@@ -83,7 +91,8 @@ def _get_bq_service(credentials=None, service_account=None, private_key=None,
8391 readonly = True ):
8492 """Construct an authorized BigQuery service object."""
8593
86- assert credentials or (service_account and private_key )
94+ assert credentials or (service_account and private_key ), \
95+ 'Must provide AssertionCredentials or service account and key'
8796
8897 if not credentials :
8998 scope = BIGQUERY_SCOPE_READ_ONLY if readonly else BIGQUERY_SCOPE
@@ -820,7 +829,7 @@ def _get_all_tables(self, dataset_id, cache=False):
820829 projectId = self .project_id ,
821830 datasetId = dataset_id ,
822831 pageToken = page_token
823- ).execute ()
832+ ).execute ()
824833 page_token = res .get ('nextPageToken' )
825834 result ['tables' ] += res .get ('tables' , [])
826835 self .cache [dataset_id ] = (datetime .now (), result )
0 commit comments