How to resolve “Missing Credentials” with Paperclip and s3 storage in rails 3


I have a pretty straightforward model and attachment

has_attached_file :upload,
    :storage => :s3,
       :bucket => 'bestofbauer',
       :s3_credentials => {
         :access_key_id => ENV['MyAccessKEY'],
         :secret_access_key => ENV['MySecretKey']

I have a bucket setup with s3 called bestofbauer.

I know I could refactor the credentials into an initializer but I haven't gotten this to save an attachment yet so I haven't worried about it.

When I run the save for the object and its attachement I get:

RuntimeError in RecommendationsController#create

Missing credentials

I have poured over: Credentials missing when uploading photos with Paperclip and Amazon s3 but that didn't resolve my issue.

I am using the following gems:

gem "paperclip"
gem "sws-sdk"
gem 'aws-s3'

Any other ideas?

Best Solution

You need to set your environment variables. Here's two different ways to do it:

  1. Every time you run rails server or any other command that accesses your S3 account you need to include your keys:

    $ MyAccessKEY=ACCESS_KEY MySecretKEY=SECRET_KEY rails server
  2. I'm assuming you're using bash so edit your ~/.bash_rc or ~/.bash_profile to set your environment variables

    export MyAccessKEY=ACCESS_KEY
    export MySecretKEY=SECRET_KEY

    Then open a new terminal window and double-check that they're set

    $ echo $MyAccessKey

If you're deploying to Heroku then you'll want to provide your environment variables there as well:

$ heroku config:add MyAccessKEY=ACCESS_KEY MySecretKEY=SECRET_KEY

You can review your Heroku config:

$ heroku config

It will list out all of the config variables you have for that app.

You'll probably want to put your S3 bucket name in an ENV setting as well so you don't mess up your bucket when testing locally.

Related Question