Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access a YML Value

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

like image 397
Rachel D Roy Avatar asked Jul 14 '11 20:07

Rachel D Roy


People also ask

How do I access values from YAML file?

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.

How do I get value from application YAML?

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; // ... }

How do I find my yml config?

By default, config. yml is located in the IQ server installation directory, i.e. the same folder that contains the IQ Server . jar file.


2 Answers

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"]
like image 198
apneadiving Avatar answered Sep 23 '22 17:09

apneadiving


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.

like image 37
Dylan Markow Avatar answered Sep 26 '22 17:09

Dylan Markow