简体   繁体   English

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

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

I am trying to test google's cloud datastore locally. 我正试图在本地测试谷歌的云数据存储。 My test steps are: 我的测试步骤是:

  1. Crawl data using Scrapy 使用Scrapy抓取数据
  2. Save data to the cloud datastore using local emulator 使用本地仿真器将数据保存到云数据存储区

I followed all the steps in order to use the local emulator 我按照所有步骤使用本地模拟器

  1. start local emulator: gcloud beta emulators datastore start 启动本地模拟器: gcloud beta emulators datastore start
  2. set local environment variables: gcloud beta emulators datastore env-init 设置本地环境变量: gcloud beta emulators datastore env-init

However, in python, when use the following command to access cloud datastore, it always save the data directly to google cloud instead of saving them to the local emulators 但是,在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)

It seems like you cannot specify the library to use the local datastore emulator, just like what they offer in their Node.js client 您似乎无法指定库以使用本地数据存储模拟器,就像它们在Node.js客户端中提供的那样

var datastore = gcloud.datastore({
        apiEndpoint: "http://localhost:8380"
});

My question is, How can I ask the google cloud datastore python library to use local emulator instead of using the cloud directly 我的问题是,如何让Google云数据存储区python库使用本地模拟器而不是直接使用云

You need to eval $(gcloud beta emulators datastore env-init) . 您需要eval $(gcloud beta emulators datastore env-init)

gcloud beta emulators datastore env-init only prints the commands that set the necessary environment variables. gcloud beta emulators datastore env-init仅打印设置必要环境变量的命令。

You can try something like 你可以尝试类似的东西

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

You can follow more here, https://cloud.google.com/appengine/docs/standard/python/tools/using-local-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