Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Avoid Authentication When Connecting to Storage Emulator #324

Description

@stefanadelbert

Problem
When connecting to a 3rd party storage emulator, like gcloud-storage-emulator, authentication is required. This requires injecting credentials into containers and adding conditional logic in code to authenticate when running against emulators.

import os
from firebase_admin import initialize_app, storage

os.environ['STORAGE_EMULATOR_HOST'] = 'http://localhost:9090'
assert 'GOOGLE_APPLICATION_CREDENTIALS' not in os.environ

initialize_app()
storage.bucket() # google.auth.exceptions.DefaultCredentialsError

Solution
When STORAGE_EMULATOR_HOST is set, use an insecure connection, as is done for firestore.

Activity

  1. added
    type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.
    and removed
    triage meI really want to be triaged.
    on Nov 24, 2020
  2. HemangChothani commented on Nov 28, 2020

    @HemangChothani
    Contributor

    @stefanadelbert For now i think you can use a class method create_anonymous_client which doesn't throw an error of credential.

    from google.cloud import storage
    
    client = storage.Client.create_anonymous_client()
    client. project = 'project_name'
    print (list(client.list_buckets()))

    def create_anonymous_client(cls):
    """Factory: return client with anonymous credentials.
    .. note::
    Such a client has only limited access to "public" buckets:
    listing their contents and downloading their blobs.
    :rtype: :class:`google.cloud.storage.client.Client`
    :returns: Instance w/ anonymous credentials and no project.
    """
    client = cls(project="<none>", credentials=AnonymousCredentials())
    client.project = None
    return client

  3. oittaa commented on Nov 24, 2021

    @oittaa

    Is this issue abandoned?

    It's quite annoying that you can't simply initiate the storage client if you ever intend to run tests.

    client = storage.Client()
    

    You always have to write some kind of wrapper like this.

    if os.getenv("STORAGE_EMULATOR_HOST"):
        # Local testing etc.
        client = storage.Client.create_anonymous_client()
        client.project = "none"
    else:
        # Normal usage
        client = storage.Client() 
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: storageIssues related to the googleapis/python-storage API.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions