Skip to content

cloudpathlib.GSClient

Client class for Google Cloud Storage which handles authentication with GCP for GSPath instances. See documentation for the __init__ method for detailed authentication options.

Methods

__init__(self, application_credentials: Union[str, os.PathLike] = None, credentials: Optional[Credentials] = None, project: Optional[str] = None, storage_client: Optional[StorageClient] = None, local_cache_dir: Union[str, os.PathLike] = None) special

Class constructor. Sets up a Storage Client. Supports the following authentication methods of Storage Client.

  • Environment variable "GOOGLE_APPLICATION_CREDENTIALS" containing a path to a JSON credentials file for a Google service account. See Authenticating as a Service Account.
  • File path to a JSON credentials file for a Google service account.
  • OAuth2 Credentials object and a project name.
  • Instantiated and already authenticated Storage Client.

If multiple methods are used, priority order is reverse of list above (later in list takes priority). If no authentication methods are used, then the client will be instantiated as anonymous, which will only have access to public buckets.

Parameters:

Name Type Description Default
application_credentials Union[str, os.PathLike]

Path to Google service account credentials file.

None
credentials Optional[Credentials]

The OAuth2 Credentials to use for this client. See documentation for StorageClient.

None
project Optional[str]

The project which the client acts on behalf of. See documentation for StorageClient.

None
storage_client Optional[StorageClient]

Instantiated StorageClient.

None
local_cache_dir Union[str, os.PathLike]

Path to directory to use as cache for downloaded files. If None, will use a temporary directory.

None
Source code in cloudpathlib/gs/gsclient.py
def __init__(
    self,
    application_credentials: Optional[Union[str, os.PathLike]] = None,
    credentials: Optional["Credentials"] = None,
    project: Optional[str] = None,
    storage_client: Optional["StorageClient"] = None,
    local_cache_dir: Optional[Union[str, os.PathLike]] = None,
):
    """Class constructor. Sets up a [`Storage
    Client`](https://googleapis.dev/python/storage/latest/client.html).
    Supports the following authentication methods of `Storage Client`.

    - Environment variable `"GOOGLE_APPLICATION_CREDENTIALS"` containing a
      path to a JSON credentials file for a Google service account. See
      [Authenticating as a Service
      Account](https://cloud.google.com/docs/authentication/production).
    - File path to a JSON credentials file for a Google service account.
    - OAuth2 Credentials object and a project name.
    - Instantiated and already authenticated `Storage Client`.

    If multiple methods are used, priority order is reverse of list above
    (later in list takes priority). If no authentication methods are used,
    then the client will be instantiated as anonymous, which will only have
    access to public buckets.

    Args:
        application_credentials (Optional[Union[str, os.PathLike]]): Path to Google service
            account credentials file.
        credentials (Optional[Credentials]): The OAuth2 Credentials to use for this client.
            See documentation for [`StorageClient`](
            https://googleapis.dev/python/storage/latest/client.html).
        project (Optional[str]): The project which the client acts on behalf of. See
            documentation for [`StorageClient`](
            https://googleapis.dev/python/storage/latest/client.html).
        storage_client (Optional[StorageClient]): Instantiated [`StorageClient`](
            https://googleapis.dev/python/storage/latest/client.html).
        local_cache_dir (Optional[Union[str, os.PathLike]]): Path to directory to use as cache
            for downloaded files. If None, will use a temporary directory.
    """
    if application_credentials is None:
        application_credentials = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")

    if storage_client is not None:
        self.client = storage_client
    elif credentials is not None:
        self.client = StorageClient(credentials=credentials, project=project)
    elif application_credentials is not None:
        self.client = StorageClient.from_service_account_json(application_credentials)
    else:
        self.client = StorageClient.create_anonymous_client()

    super().__init__(local_cache_dir=local_cache_dir)

CloudPath(self, cloud_path: Union[str, ~BoundedCloudPath]) -> ~BoundedCloudPath inherited

Source code in cloudpathlib/gs/gsclient.py
def CloudPath(self, cloud_path: Union[str, BoundedCloudPath]) -> BoundedCloudPath:
    return self._cloud_meta.path_class(cloud_path=cloud_path, client=self)

GSPath(self, cloud_path: Union[str, ~BoundedCloudPath]) -> ~BoundedCloudPath

Source code in cloudpathlib/gs/gsclient.py
def CloudPath(self, cloud_path: Union[str, BoundedCloudPath]) -> BoundedCloudPath:
    return self._cloud_meta.path_class(cloud_path=cloud_path, client=self)

set_as_default_client(self) -> None inherited

Set this client instance as the default one used when instantiating cloud path instances for this cloud without a client specified.

Source code in cloudpathlib/gs/gsclient.py
def set_as_default_client(self) -> None:
    """Set this client instance as the default one used when instantiating cloud path
    instances for this cloud without a client specified."""
    self.__class__._default_client = self