Skip to content

Default client on gce instances #191

@amardeep

Description

@amardeep

With GOOGLE_APPLICATION_CREDENTIALS not set on gce instances, cloudpathlib operations such as is_dir fail:

from cloudpathlib import CloudPath
p = CloudPath("gs://<some non-public path>")
p.is_dir()

fails with:

/lib/python3.7/site-packages/google/auth/credentials.py in refresh(self, request)
    171         """Raises :class:`ValueError``, anonymous credentials cannot be
    172         refreshed."""
--> 173         raise ValueError("Anonymous credentials cannot be refreshed.")
    174 
    175     def apply(self, headers, token=None):

ValueError: Anonymous credentials cannot be refreshed

On gce instances, storage client doesn't need any further auth setup. For eg., without GOOGLE_APPLICATION_CREDENTIALS set, this works:

from google.cloud import storage
storage_client = storage.Client()
# Make an authenticated API request
buckets = list(storage_client.list_buckets())
print(buckets)

and this also works:

from cloudpathlib import CloudPath
from google.cloud import storage

gs_client = cloudpathlib.GSClient(storage_client=storage.Client())
gs_client.set_as_default_client()

p = CloudPath("gs://<some non-public path>")
p.is_dir()

It would be nice if this extra setup is not needed.
Maybe while constructing it can try creating non-anonymous storage client and if it fails create anonymous one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions