4

I'm trying to get my refinery cms image storage to Amazon s3 and I'm following this guide:

http://refinerycms.com/guides/how-to-use-amazon-s3-for-storage

But I'm blocked here:

There are a number of ways to set these with your credentials, including unix variables or settings them manually through Ruby using ENV.

How do I define these credentials. Do I put something like :S3_KEY => "my_key" in my environments.rb file? I tried this and it didn't work. I also tried this:

AWS::S3::Base.establish_connection!(
 :access_key_id     => ENV['S3_KEY'] || 'key_goes_here',
 :secret_access_key => ENV['S3_SECRET'] || 's3_secret_key_here',
)

Can't figure out how to do this. Any ideas are greatly appreciated.

Lee McAlilly
  • 9,084
  • 12
  • 60
  • 94

1 Answers1

6

The safest way is to specify them as environment variables, so they aren't included in your source code. If you're the only one with access to the source, then specifying them as you describe should work.

You can specify them in your ~/.bashrc

export S3_KEY=mykey
export S3_SECRET=mysecret

Or if you're just testing locally you can prepend them to your rails command.

$ S3_KEY=mykey S3_SECRET=mysecret rails server

If you don't want to/can't use environment variables, another method is to use an initializer to load credentials from a yml file: config/initializers/s3_credentials.rb

# Load AWS::S3 configuration values
#
S3_CREDENTIALS = \
    YAML.load_file(File.join(Rails.root, 'config/s3_credentials.yml'))[Rails.env]

# Set the AWS::S3 configuration
#
AWS::S3::Base.establish_connection! S3_CREDENTIALS['connection']

config/s3_credentials.yml

development: &defaults
connection:
    :access_key_id: AAAAAA_your-key-here
    :secret_access_key: 4rpsi235js_your-secret-here
    :use_ssl: true
    bucket: project-development
    acl: public-read

production:
    <<: *defaults
    bucket: project
ghoppe
  • 21,452
  • 3
  • 30
  • 21
  • Thanks, it looks like loading a yaml file is a better way to do this, but I got it working by just adding these lines to my config/environment.rb ENV['S3_KEY']='XXXXXXXXXXXXXXXXXXX' ENV['S3_SECRET']='XXXXXXXXXXXXXXXXXXX' ENV['S3_BUCKET']='my_bucket_name_' – Lee McAlilly Apr 12 '11 at 13:51
  • Can you please tell can I type for 'mykey' and 'mysecret' anything I want, or I have to find these values somewhere? If I have to find them, where can I find them. Furtheremore, when all is set up, what comes next? Do I have to restart something, execute some command or something else? How will this function? My site should now have all the pictures... right? This is all very confusing. please help – Dantes Aug 11 '12 at 18:32
  • I hope you found this by now, but the key and secret values come from Amazon S3: https://portal.aws.amazon.com/gp/aws/securityCredentials – Dreyfuzz Apr 15 '13 at 01:47