我试图弄清楚在开发中设置我的凭据的最佳方法是什么?由于heroku的配置变量,我能够在生产中设置凭据......但如果您只是在本地测试应用程序,则需要帮助。
我在development.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['BUCKET_NAME'],
:access_key_id => ENV['ACCESS_KEY_ID'],
:secret_access_key => ENV['SECRET_ACCESS_KEY']
}
}
但是,我不确定如何引用这些ENV变量?
我在 / config 文件夹
中创建了此aws.yml
文件
development:
BUCKET_NAME: "somename"
ACCESS_KEY_ID: "4205823951412980"
SECRET_ACCESS_KEY: "123141ABNCEFEHUDSL2309489850"
我想如果我匹配ENV名称,它会起作用吗?我猜那不是这样的......
我确保将aws.yml
包含在我的.ignore
文件
/config/aws.yml
答案 0 :(得分:4)
您正在寻找dotenv宝石。然后安装gem gem 'dotenv-rails', :groups => [:development, :test]
只需创建和`.env'文件并将变量放入其中。
S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE
答案 1 :(得分:1)
我喜欢在 ./ config 目录中创建 config.yml 。
然后我告诉 ./ config / application.rb 我有一个加载变量的小块,如
config.before_initialize do
dev = File.join(Rails.root, 'config', 'config.yml')
YAML.load(File.open(dev)).each do |key,value|
ENV[key.to_s] = value
end if File.exists?(dev)
end
这是我的 ./ config / config.yml
BUCKET_NAME: "somename"
ACCESS_KEY_ID: "4205823951412980"
SECRET_ACCESS_KEY: "123141ABNCEFEHUDSL2309489850"
这是我的#config / application.rb
的副本#config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Appname
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
initializer 'setup_asset_pipeline', :group => :all do |app|
# We don't want the default of everything that isn't js or css, because it pulls too many things in
app.config.assets.precompile.shift
# Explicitly register the extensions we are interested in compiling
app.config.assets.precompile.push(Proc.new do |path|
File.extname(path).in? [
'.html', '.erb', '.haml', # Templates
'.png', '.gif', '.jpg', '.jpeg', # Images
'.eot', '.otf', '.svc', '.woff', '.ttf', # Fonts
]
end)
end
I18n.enforce_available_locales = false
config.before_initialize do
dev = File.join(Rails.root, 'config', 'config.yml')
YAML.load(File.open(dev)).each do |key,value|
ENV[key.to_s] = value
end if File.exists?(dev)
end
end
end
答案 2 :(得分:1)
我喜欢在本地使用 Foreman ,并将所有变量存储在 .ENV 文件中。与Heroku合作很好,易于管理。在我的情况下,我有以下内容:
在 config / initiliazers / aws.rb
下require 'aws-sdk'
require 'aws-sdk-resources'
# Rails.configuration.aws is used by AWS, Paperclip, and S3DirectUpload
Rails.configuration.aws = YAML.load(ERB.new(File.read("#{Rails.root}/config/aws.yml")).result)[Rails.env].symbolize_keys!
AWS.config(logger: Rails.logger)
AWS.config(Rails.configuration.aws)
在config / initializers / paperclip.rb
下# https://devcenter.heroku.com/articles/paperclip-s3
if Rails.env.test?
Paperclip::Attachment.default_options.merge!(
path: ":rails_root/public/paperclip/:rails_env/:class/:attachment/:id_partition/:filename",
storage: :filesystem
)
else
Paperclip::Attachment.default_options.merge!(
url: ':s3_domain_url',
path: '/:class/:filename',
storage: :s3,
s3_credentials: Rails.configuration.aws,
s3_protocol: 'https'
)
end
在config / aws.yml
下defaults: &defaults
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
test:
<<: *defaults
bucket: <%= ENV['AWS_TEST_BUCKET'] %>
development:
<<: *defaults
bucket: <%= ENV['AWS_TEST_BUCKET'] %>
production:
<<: *defaults
bucket: <%= ENV['AWS_BUCKET'] %>
答案 3 :(得分:0)
如果其他一切都失败了,您只需使用命令行参数启动rails应用程序即可。很高兴知道基本情况,你确定你的params确实传递给了你的应用程序:
ACCESS_KEY_ID = ABCDEFG SECRET_ACCESS_KEY = secreTaccessKey BUCKET_NAME = dev-images rails s
我实际上更喜欢在env中设置别名:
alias railss =&#39; ACCESS_KEY_ID = ABCDEFG SECRET_ACCESS_KEY = secreTaccessKey BUCKET_NAME = dev-images rails s&#39;