Python:在Google Cloud Datastore模拟器中保存数据

9
我想在本地测试谷歌的云数据存储。我的测试步骤如下:
  1. 使用Scrapy爬取数据
  2. 使用本地模拟器将数据保存到云数据存储中
我按照顺序执行了所有使用本地模拟器的步骤:
  1. 启动本地模拟器:gcloud beta emulators datastore start
  2. 设置本地环境变量:gcloud beta emulators datastore env-init
然而,在Python中,当使用以下命令访问云数据存储时,它总是直接将数据保存到谷歌云中,而不是保存到本地模拟器中。
#Imports the Google Cloud client library
from google.cloud import datastore

# Instantiates a client
datastore_client = datastore.Client()

sample_entry = some_data

# Saves the entity
datastore_client.put(sample_entry)

似乎您无法像Node.js客户端提供的那样指定使用本地数据存储模拟器的库。

var datastore = gcloud.datastore({
        apiEndpoint: "http://localhost:8380"
});
我的问题是,如何请求 Google Cloud Datastore Python 库使用本地模拟器而不是直接使用云端。

1
在调用env-init之后,DATASTORE_EMULATOR_HOST环境变量的值是什么? - Ed Davisson
1
虽然我总是喜欢进行本地测试和开发服务,但谷歌云环境非常混乱,我绝对不建议使用他们的平台仿真包。这样的错误以及与实际部署环境行为不一致的问题会带来很多麻烦,除非你有理由不能这样做,否则我建议只在云环境中使用一个暂存项目。 - kungphu
可能你需要按照这里给出的步骤再次检查你的模拟器设置:https://cloud.google.com/datastore/docs/tools/datastore-emulator - Jadav Bheda
不确定这是否会对您有所帮助:https://github.com/GoogleCloudPlatform/google-cloud-python/issues/1837 - Jadav Bheda
2个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
1
你需要执行 eval $(gcloud beta emulators datastore env-init)gcloud beta emulators datastore env-init 仅打印设置必要环境变量的命令。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,