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

时间:2017-05-18 14:53:34

标签: python google-app-engine google-cloud-datastore datastore

我正在尝试在本地测试Google的云数据存储区。我的测试步骤是:

  1. 使用Scrapy抓取数据
  2. 使用本地模拟器
  3. 将数据保存到云数据存储区

    我按照所有步骤使用本地模拟器

    1. 启动本地模拟器:gcloud beta emulators datastore start
    2. 设置本地环境变量:gcloud beta emulators datastore env-init
    3. 但是,在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库使用本地模拟器而不是直接使用云

2 个答案:

答案 0 :(得分:0)

您需要gcloud beta emulators datastore env-init

myDriver.findElement.ById("select-pcsl-9777-protocol") 仅打印设置必要环境变量的命令。

答案 1 :(得分:0)

您可以尝试类似

的内容
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

相关问题