I have the following yml
/config/s3.yml
common: &common
access_key_id: asddasadsadsad
secret_access_key: adsasddasdasdsa+qlSn+dadadada
development:
<<: *common
bucket: XX_dev
test:
<<: *common
bucket: XX_test
production:
<<: *common
bucket: XX_prod
has_attached_file :photo,
:styles => { :thumb => "70x70>" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/assets/users/:id/:style/:basename.:extension",
:bucket => "????How to get this from the YML based on the ENV???",
And I want to use this in paperclip, how can I access the bucket? Thanks
Next, we need to load the YAML file using the safe_load function available in the PyYAML package. From the above code, we start by importing the yaml package. We then create a main function (any name works) and set the logic for reading the yaml file. Once the file is open and read, we call the main function.
You can use @Value annotation to get value from YAML file or Type-safe Configuration Properties to bind values, learning about the reference here, link. @Component public class MyBean { @Value("${name}") private String name; // ... }
By default, config. yml is located in the IQ server installation directory, i.e. the same folder that contains the IQ Server . jar file.
Create a file in your initializers and put this inside:
raw_config = File.read("#{Rails.root}/config/s3.yml")
APP_CONFIG = YAML.load(raw_config)
Then in your app, you'll access it's values this way:
APP_CONFIG[Rails.env]["bucket"]
You don't need to pass a :bucket
parameter, Paperclip will already know to use the bucket:
key for the current environment from your s3.yml
file.
Also note that if you manually compiled Ruby 1.9.2 via RVM, there is a chance that you are falling victim to a bug in the new Psych YAML engine. It doesn't like "DRY" yaml files (yet -- it's been filed as a bug and should be fixed in the next official release of Ruby). What happens is that each item that inherits from common
, only contains those inherited items (your access key and secret id), and doesn't actually include the additional stuff you add (in your case, your bucket name).
Try making a traditional yml
file instead without the common
section (i.e. repeat the access key and secret id for each environment). More info: Error when loading YAML config files in Rails
UPDATE
The newest release of Ruby that came out today (1.9.2-p290) includes a fix for this issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With