I'm trying define a connection to an external storage provider. To open the connection, normally I would do
storage = Fog::Storage.new {lots more stuff here}
This means putting very secret credentials in the code every where I want to access the storage object. There has to be a better way?
If you define a constant in a file in config/initializers
, that file will be require
d at app start and the constant will be generally available. For instance:
config/initializers/config.rb
Storage = Fog::Storage.new { stuff }
app/models/mymodel.rb
do_something_with(Storage)
There is. Create a fog.yml
or something, and place it in your config
folder (you'll want to ignore this in version control).
Then set the credentials:
credentials: &credentials
username: yourname
password: yourpass
development:
<<: *credentials
test:
<<: *credentials
production:
<<: *credentials
(I don't use fog so I don't really know what information you need). Then make a new initializer file, fog.rb
or something, and place it in config/initializers
. Put this inside so it gets loaded when the app is initialized:
config = YAML.load_file("#{Rails.root}/config/fog.yml")[Rails.env]
FogStorage = Fog::Storage.new { config['username'], config['password'] }
Then you can use that global FogStorage
anywhere.
Again, be sure you ignore the credentials file for version control, and just make a symlink to it in deployment.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.