How to Auth to Google Cloud using Service Account in Python?

后端 未结 4 827
说谎
说谎 2020-12-14 02:02

Im trying to make a project that will upload google storage json file to BigQuery (just automate something that is done manually now).

And i\'d like to use \'service

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 02:12

    If you have implemented more fine-grained control over service accounts permissions and you have an app that needs to use several of them (say one for Pub/Sub, one for storage), then you would have to set the GOOGLE_APPLICATION_CREDENTIALS environment variable before creating each client.

    Instead, you can load your credentials separately and pass them to their appropriate clients like so:

    import json
    
    from google.cloud import storage
    from google.oauth2 import service_account
    
    project_id = 'my-test-project'
    
    with open('/path/to/my/service_keys/storage_service.json') as source:
        info = json.load(source)
    
    storage_credentials = service_account.Credentials.from_service_account_info(info)
    
    storage_client = storage.Client(project=project_id, credentials=storage_credentials)
    

    Just make sure in your IAM console that the account has the right permissions to perform the operations you need it to do, but luckily in that case the error messages are really informative.

提交回复
热议问题