Max Williams
Max Williams

Reputation: 32945

Rails3 - how to get at aws-s3's yml config data in the app?

Specifically, i have a file config/amazon_s3.yml which is used by the aws-s3 gem to set up some s3 config settings like secret keys etc. I also write some of this data into a var in ENV in a different file in initializers so i can reference them in calls to the "has_attached_file" method used by paperclip.

It would be smarter to get the file in initializers to read them out of the s3 config yml, or some config settings for the classes used by the gem, eg something like AWS::S3::Base.connection.secret_access_key (this doesn't work).

Any ideas?

Upvotes: 2

Views: 5104

Answers (1)

Max Williams
Max Williams

Reputation: 32945

I found the answer here How to use YML values in a config/initalizer

First i load in the yaml in and stick it in a constant.

#config/initializers/constants.rb
S3_CONFIG = YAML.load_file("#{::Rails.root}/config/amazon_s3.yml")

Then, when i set up paperclip for a model, pull in these values, making sure i refer to the current environment:

class Entry < ActiveRecord::Base
  has_attached_file :media,
    :styles => { 
      :medium => "300x300>", 
      :thumb => "110x110>" 
    },
    :storage => :s3,
    :bucket =>S3_CONFIG[::Rails.env]["bucket"],
    :s3_credentials => {
      :access_key_id => S3_CONFIG[::Rails.env]["access_key_id"],
      :secret_access_key => S3_CONFIG[::Rails.env]["secret_access_key"]
    }        
end

Upvotes: 9

Related Questions