Using Google Application Default Credentials (ADC) with gcloud-aio Storage in Python

111 views Asked by At

I'm using gcloud-aio/storage to access GCS buckets, with the authentication via service account key file (JSON file). Here's my working Python code:

from os import path
from gcloud.aio.auth import Token
from gcloud.aio.storage import Storage

SCOPES = ["https://www.googleapis.com/auth/cloud-platform.read-only"]

pwd = path.realpath(path.dirname(__file__))
service_file = path.join(pwd, "../../my_key.json")
token = Token(service_file=service_file, scopes=SCOPES)
async with Storage(token=token) as storage:
    _ = await storage.list_objects(bucket_name)


I'd like to instead authenticate via Application Default Credentials, and typically use this code to get an access token:

from google.auth import default
from google.auth.transport.requests import Request

credentials, project_id = default(scopes=SCOPES)
_ = Request()
credentials.refresh(_)
access_token = credentials.token

But can't figure out how (or if) I can pass this type of token to aio storage.

0

There are 0 answers