title: How to set up Amazon S3, paperclip, and ENV variables
body: <p>I have tried many different ways to set up S3 using ENV variables for image uploads and cannot get it to work.  I know my keys and bucket name work, because when I put them straight into the code, my images upload correctly.  However, when I try to switch to ENV variables, things do not work.</p>
  
  <p>I used the figaro gem, which created application.yml.  In that file, I have:</p>
  
  <pre><code>S3_BUCKET_NAME "xxxxx"
  AWS_ACCESS_KEY_ID: "AAAAAAAAA"
  AWS_SECRET_ACCESS_KEY: "BBBbbbBBBB"
  </code></pre>
  
  <p>Not sure if there should be any quotation marks there or not, but right now, I have them in.  I have tried without, also.</p>
  
  <p>In my model (listing.rb), I have:</p>
  
  <pre><code>has_attached_file :image, 
    :styles =&gt; { :medium =&gt; "200x" , :thumb =&gt; "100x100" }, 
    :default_url =&gt; "default.png",
    :storage =&gt; :s3,
    :s3_credentials =&gt; Proc.new{|a| a.instance.s3_credentials }
  
  def s3_credentials
    {:bucket =&gt; ENV["S3_BUCKET_NAME"], :access_key_id =&gt; ENV["AWS_ACCESS_KEY_ID"], 
    :secret_access_key =&gt; ENV["AWS_SECRET_ACCESS_KEY"]
  end
  </code></pre>
  
  <p>Like I said, when I hard code the values into def s3_credentials, everything works fine.  It's just when I try to swap out ENV variables that things fall apart.</p>
  
  <p>In paperclip.rb, I have:</p>
  
  <pre><code>Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
  </code></pre>
  
  <p>I also have this code in both production.rb and development.rb:</p>
  
  <pre><code>config.paperclip_defaults = {
   :storage =&gt; :s3,
   :s3_credentials =&gt; {
   :bucket =&gt; ENV['S3_BUCKET_NAME'],
   :access_key_id =&gt; ENV['AWS_ACCESS_KEY_ID'],
   :secret_access_key =&gt; ENV['AWS_SECRET_ACCESS_KEY']
   }
  }
  </code></pre>
  
  <p>Here is the error message I get when uploading a new image: "The request signature we calculated does not match the signature you provided. Check your key and signing method."  On line: "if @listing.save".  The ones that were uploaded w/ the credentials hard-coded are still able to be seen in my app. </p>
  
  <p>I'm fairly new to rails and have looked here and other places, including the S3 and paperclip docs, and cannot find a solution that will work.  Please let me know if you need to see any other code.  I plan on deploying to heroku, if that matters, and saw that figaro is supposed to play nicely with heroku.  THANK YOU. </p>
  
  <hr>
  
  <p>EDIT/UPDATE: For anyone else reading this in the future, Sachin's answer below worked.  However, there was a '+' in one of my key IDs.  When trying to added the ENV variable via command line, all the characters after the '+' (and including it) were cut off.  Simply wrap them in "", and you should be good to go.  </p>
  
  <p>Also, I dropped use of the figaro gem, and set up an aws.rb initializer file (per Amazon's instructions).  Here are the contents of that file:</p>
  
  <pre><code>AWS.config(
    :access_key_id =&gt; ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key =&gt; ENV['AWS_SECRET_ACCESS_KEY']
  )
  
  S3_BUCKET = AWS::S3.new.buckets[ENV['S3_BUCKET']]
  </code></pre>
  
  <p>And I don't know if this made any difference, but my development.rb and production.rb files now have the following as paperclip defaults:</p>
  
  <pre><code>config.paperclip_defaults = {
   :storage =&gt; :s3,
   :bucket =&gt; "your_real_bucket_name_here_in_quotes",
   :s3_credentials =&gt; {
    :access_key_id =&gt; ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key =&gt; ENV['AWS_SECRET_ACCESS_KEY']
   }
  }
  </code></pre>
  
  <p>Also note the switch to referring to the ENV bucket name as S3_BUCKET vs. S3_BUCKET_NAME.</p>
  
  <p>And the code in my model (listing.rb) is now this:</p>
  
  <pre><code>has_attached_file :image, :styles =&gt; { :medium =&gt; "200x", :thumb  "100x100"}, :default_url =&gt; "default.png", :storage =&gt; :s3, :bucket =&gt; "your_real_bucket_name_here_in_quotes"
  
  validates_attachment_content_type :image, :content_type =&gt; ["image/jpg", "image/jpeg", "image/png", "image/gif"] 
  </code></pre>