代码之家  ›  专栏  ›  技术社区  ›  Sam

Google云数据存储模拟器不使用默认凭据

  •  2
  • Sam  · 技术社区  · 6 年前

    Google's Cloud Datastore Emulator installation instructions ,我能够在 猛击 gcloud beta emulators datastore start --project gramm-id .

    我还设置了环境变量, per the instructions ,在另一个终端 $(gcloud beta emulators datastore env-init) 并验证了它们的定义。

    但是,当我运行python脚本向本地数据存储添加实体时,使用以下代码:

    from google.cloud import datastore
    
    print(os.environ['DATASTORE_HOST'])          # output: http://localhost:8081
    print(os.environ['DATASTORE_EMULATOR_HOST']) # output: localhost:8081
    
    
    client = datastore.Client('gramm-id')
    kind = 'Task'
    name = 'simpleTask'
    
    task_key = client.key(kind, name)
    task = client.Enity(key=task_key)
    task['description'] = 'Buy milk'
    client.put(task)
    

    Traceback (most recent call last):
      File "tools.py", line 237, in <module>
        client = datastore.Client('gramm-id')
      File "/home/.../lib/python3.6/site-packages/google/cloud/datastore/client.py", line 205, in __init__
        project=project, credentials=credentials, _http=_http)
    ... long stack trace ....
      File "/home/.../lib/python3.6/site-packages/google/auth/_default.py", line 306, in default
        raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
    google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://developers.google.com/accounts/docs/application-default-credentials.
    

    我想我不需要 create a GCP service account and provide access credentials

    我的系统:

    • Ubuntu 18.04版
    • 谷歌云SDK 215.0.0
    • 云数据存储模拟器2.0.2。

    我错过了什么?

    2 回复  |  直到 6 年前
        1
  •  5
  •   Serge Hendrickx    6 年前

    gcloud验证应用程序默认登录

    这将提示您通过浏览器窗口登录,并为您正确设置GOOGLE应用程序凭据。 [1]

        2
  •  1
  •   Dustin Ingram    6 年前

    class EmulatorCreds(google.auth.credentials.Credentials):
    
        def __init__(self):
            self.token = b'secret'
            self.expiry = None
    
        @property
        def valid(self):
            return True
    
        def refresh(self, _):
            raise RuntimeError('Should never be refreshed.')
    
    client = datastore.Client(
        project='gramm-id',
        credentials=EmulatorCreds() , 
        _http=requests.Session()  # Un-authorized
    )
    

    然而 it seems like this doesn't currently work GOOGLE_APPLICATION_CREDENTIALS