繁体   English   中英

Python:在Google云数据存储模拟器中保存数据

[英]Python: Save data in google cloud datastore emulator

我正试图在本地测试谷歌的云数据存储。 我的测试步骤是:

  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云数据存储区python库使用本地模拟器而不是直接使用云

您需要eval $(gcloud beta emulators datastore env-init)

gcloud beta emulators datastore env-init仅打印设置必要环境变量的命令。

你可以尝试类似的东西

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
  # Production
else:
  # Local development server

您可以在此处关注更多内容, https://cloud.google.com/appengine/docs/standard/python/tools/using-local-server

暂无
暂无

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

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