无法在 Rails 项目的 Ruby 中使用 Google 数据存储模拟器

Can't use Google datastore emulator from within Ruby on Rails project

我在 Rails 上使用 Ruby 和 Google App Engine,即本教程:https://cloud.google.com/ruby/getting-started/hello-world. I am using Google Datastore instead of a conventional database. For production, I am easily able to connect to my project's datastore, however development mode using the Google Datastore Emulator proves an issue. I was able to install the Datastore Emulator and get it up and running using the provided google tutorial (https://cloud.google.com/datastore/docs/tools/datastore-emulator).

我 运行 数据存储模拟器,然后 运行 gcloud beta emulators datastore env-init 并确保这些环境变量在我的 .bashrc 文件中(并为我的 .bashrc 文件提供源)。

我的环境变量看起来像这样:

export DATASTORE_DATASET=my-app
export DATASTORE_HOST=http://localhost:8473
export DATASTORE_EMULATOR_HOST=localhost:8473
export DATASTORE_PROJECT_ID=my-app

然后我尝试在 Rails 中执行以下操作:

require 'gcloud'
gcloud = Gcloud.new('my-app')
dataset = gcloud.datastore

这会导致 'gcloud' gem 出现以下错误:

RuntimeError: Could not load the default credentials. Browse to
https://developers.google.com/accounts/docs/application-default-credentials
for more information

    from /home/sam/.rvm/gems/ruby-2.2.3/gems/googleauth-0.5.1/lib/googleauth.rb:119:in `get_application_default'
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/gcloud-0.7.2/lib/gcloud/credentials.rb:83:in `default'
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/gcloud-0.7.2/lib/gcloud/datastore.rb:62:in `datastore'
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/gcloud-0.7.2/lib/gcloud.rb:106:in `datastore'
    from (irb):3
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/sam/.rvm/gems/ruby-2.2.3/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:18:in `require'
    from bin/rails:18:in `<main>'

我的印象是,如果使用数据存储模拟器,我不需要密钥文件。如果我确实需要一个密钥文件,我该如何创建一个让我连接到本地数据存储模拟器的密钥文件?数据存储模拟器文档根本没有提到这个需求,并且似乎建议仅仅设置环境变量就足以让本地客户端自动连接。

请指教!

虽然我无法ruby实际连接到数据存储模拟器,但此处似乎概述了更好的解决方案:https://github.com/GoogleCloudPlatform/gcloud-ruby/issues/610

更新:模拟器连接不工作是由于我的一个简单错误,请参阅链接线程以获取有关如何执行此操作的良好指南