1

我正在使用帶有Google App Engine的Ruby on Rails,即本教程:https://cloud.google.com/ruby/getting-started/hello-world。我正在使用Google Datastore而不是傳統的數據庫。對於生產,我可以輕鬆連接到我的項目的數據存儲,但使用Google數據存儲模擬器的開發模式證明了一個問題。我能夠安裝數據存儲模擬器並使用提供的谷歌教程(https://cloud.google.com/datastore/docs/tools/datastore-emulator)啓動並運行它。無法在Ruby on Rails項目中使用Google數據存儲模擬器

我運行數據存儲模擬器,然後運行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 

然後我嘗試做從內軌以下內容:

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

這將導致以下錯誤從「gcloud」寶石:

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>' 

我的印象是,如果使用數據,我不需要密鑰文件撕毀模擬器。如果我確實需要密鑰文件,我該如何創建一個能讓我連接到本地數據存儲模擬器的密鑰文件?數據存儲模擬器文檔根本沒有提到這一點,並且似乎表明,僅僅使用環境變量就足以讓本地客戶端自動連接。

請指教!

回答

2

雖然我還沒有能夠得到紅寶石真正連接到數據存儲仿真器,更好的解決方案似乎是這裏概述:https://github.com/GoogleCloudPlatform/gcloud-ruby/issues/610

更新:仿真器連接不工作是因爲一個簡單的錯誤在我的部分,請參閱鏈接線索瞭解如何做到這一點的良好指導