Created
July 1, 2024 15:46
-
-
Save kilonzi/1ebb345577c75f0a123016178f13a6b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""How to authenticate with service account file | |
""" | |
# !pip install google-auth | |
# !pip install google-auth-httplib2 | |
# !pip install google-cloud-storage | |
from google.oauth2 import service_account | |
from google.cloud import storage | |
from google.auth.transport.requests import Request | |
"""* Upload the .json service account file to the Notebook and rename it to "credentials.json" | |
""" | |
class Auth: | |
def __init__(self, credentials_path): | |
self.credentials_path = credentials_path | |
def get_credentials(self): | |
credentials = service_account.Credentials.from_service_account_file(self.credentials_path, | |
scopes=['https://www.googleapis.com/auth/cloud-platform']) | |
credentials.refresh(Request()) | |
return credentials | |
if __name__ == "main": | |
credentials = Auth(credentials_path='/content/credentials.json').get_credentials() | |
print(credentials.token) | |
storage_client = storage.Client(credentials=credentials) | |
buckets = storage_client.list_buckets() | |
for bucket in buckets: | |
print(bucket.name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment