在雾中使用服务帐户(电子邮件和密钥)进行身份验证-google

Authenticating with service account (email & key) in fog-google

我不断收到 Missing required arguments: google_storage_access_key_id, google_storage_secret_access_key。我知道我应该将我的凭据放在 "in a /.fog" 文件中,但我不太明白它在 Rails 应用程序的上下文中应该如何工作。有人可以详细说明如何配置吗?我试过在初始化程序中传递设置(如建议的 here), but they don't seem to get recognized in the validate_options 方法。

config/initializers/fog.rb GoogleStorage = Fog::Storage.new( provider: 'Google', google_project: 'xxxxxxxxxxxxx', google_client_email: 'xxxxxxxxxxxxx-yyyyyyyyyyyyy@developer.gserviceaccount.com', google_key_location: 'private/google-cloud-service-key.p12' )

改用 Figaro Gem 来处理您要在整个应用程序中安全存储和使用的任何 ENV 变量。

Add Figaro to your Gemfile and bundle install:

gem "figaro"

Figaro installation is easy:

$ bundle exec figaro install

This creates a commented config/application.yml file and adds it to your .gitignore. Add your own configuration to this file and you're done!

示例application.yml文件

# config/application.yml

GOOGLE_ID: "ID"
GOOGLE_KEY: "KEY"

然后 config/initializers/fog.rb

GoogleStorage = Fog::Storage.new(
  provider: 'Google',
  google_project: 'xxxxxxxxxxxxx',
  google_client_email: 'xxxxxxxxxxxxx-yyyyyyyyyyyyy@developer.gserviceaccount.com',
  google_key_location: Rails.root.join('private','google-cloud-service-key.p12'),
  google_storage_secret_access_key_id: ENV["GOOGLE_ID"],
  google_storage_secret_access_key: ENV["GOOGLE_KEY"]
)

事实证明,目前 fog-google gem 无法做到这一点。参见 this Github issue。当更新 gem 以处理此身份验证策略时,我将更新此答案。