Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid Authentication When Connecting to Storage Emulator #324

Closed
stefanadelbert opened this issue Nov 23, 2020 · 2 comments · Fixed by #679
Closed

Avoid Authentication When Connecting to Storage Emulator #324

stefanadelbert opened this issue Nov 23, 2020 · 2 comments · Fixed by #679
Labels
api: storage Issues related to the googleapis/python-storage API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@stefanadelbert
Copy link

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.

@busunkim96 busunkim96 transferred this issue from googleapis/google-cloud-python Nov 23, 2020
@product-auto-label product-auto-label bot added the api: storage Issues related to the googleapis/python-storage API. label Nov 23, 2020
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Nov 23, 2020
@tseaver tseaver added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. labels Nov 24, 2020
@HemangChothani
Copy link
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

@oittaa
Copy link

oittaa commented Nov 24, 2021

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
Labels
api: storage Issues related to the googleapis/python-storage API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
5 participants