简体   繁体   English

Google Cloud Datastore Emulator不使用默认凭据

[英]Google Cloud Datastore Emulator does not use default credentials

Per Google's Cloud Datastore Emulator installation instructions , I was able to install and run the emulator in a bash terminal window without problem with gcloud beta emulators datastore start --project gramm-id . 按照Google的Cloud Datastore Emulator安装说明 ,我能够在bash终端窗口中安装并运行该模拟器,而gcloud beta emulators datastore start --project gramm-id没有问题。

I also setup the environment variables, per the instructions , in another terminal with $(gcloud beta emulators datastore env-init) and verified they were defined. 我还按照说明在另一个带有$(gcloud beta emulators datastore env-init)终端中设置了环境变量,并验证了它们是否已定义。

However, when I run my python script to add an entity to the local datastore with this code: 但是,当我运行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)

I get the error: 我得到错误:

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.

I don't think I need to create a GCP service account and provide access credentials to use the datastore emulator on my machine. 我认为我无需创建GCP服务帐户并提供访问凭据即可在计算机上使用数据存储模拟器。

My system: 我的系统:

  • Ubuntu 18.04 Ubuntu 18.04
  • Anaconda python 3.6.6 蟒蛇蟒蛇3.6.6
  • Google Cloud SDK 215.0.0 Google Cloud SDK 215.0.0
  • cloud-datastore-emulator 2.0.2. cloud-datastore-emulator 2.0.2。

What am I missing? 我想念什么?

gcloud auth application-default login gcloud auth应用程序-默认登录

This will prompt you to login through a browser window and will set your GOOGLE_APPLICATION_CREDENTIALS correctly for you. 这将提示您通过浏览器窗口登录,并为您正确设置GOOGLE_APPLICATION_CREDENTIALS。 [1] [1]

In theory you should be able to use mock credentials, eg: 从理论上讲,您应该能够使用模拟凭据,例如:

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
)

However it seems like this doesn't currently work , so for now you'll need to set GOOGLE_APPLICATION_CREDENTIALS 但是,目前看来这无法正常工作 ,因此,现在您需要设置GOOGLE_APPLICATION_CREDENTIALS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM