Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up Amazon S3, paperclip, and ENV variables

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.

I used the figaro gem, which created application.yml. In that file, I have:

S3_BUCKET_NAME "xxxxx"
AWS_ACCESS_KEY_ID: "AAAAAAAAA"
AWS_SECRET_ACCESS_KEY: "BBBbbbBBBB"

Not sure if there should be any quotation marks there or not, but right now, I have them in. I have tried without, also.

In my model (listing.rb), I have:

has_attached_file :image, 
  :styles => { :medium => "200x" , :thumb => "100x100" }, 
  :default_url => "default.png",
  :storage => :s3,
  :s3_credentials => Proc.new{|a| a.instance.s3_credentials }

def s3_credentials
  {:bucket => ENV["S3_BUCKET_NAME"], :access_key_id => ENV["AWS_ACCESS_KEY_ID"], 
  :secret_access_key => ENV["AWS_SECRET_ACCESS_KEY"]
end

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.

In paperclip.rb, I have:

Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'

I also have this code in both production.rb and development.rb:

config.paperclip_defaults = {
 :storage => :s3,
 :s3_credentials => {
 :bucket => ENV['S3_BUCKET_NAME'],
 :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
 :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
 }
}

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.

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.


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.

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:

AWS.config(
  :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
  :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
)

S3_BUCKET = AWS::S3.new.buckets[ENV['S3_BUCKET']]

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:

config.paperclip_defaults = {
 :storage => :s3,
 :bucket => "your_real_bucket_name_here_in_quotes",
 :s3_credentials => {
  :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
  :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
 }
}

Also note the switch to referring to the ENV bucket name as S3_BUCKET vs. S3_BUCKET_NAME.

And the code in my model (listing.rb) is now this:

has_attached_file :image, :styles => { :medium => "200x", :thumb  "100x100"}, :default_url => "default.png", :storage => :s3, :bucket => "your_real_bucket_name_here_in_quotes"

validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"] 
like image 844
David Avatar asked Mar 17 '23 02:03

David


1 Answers

You can do one thing:

You can set this configuration in your development.rb or production.rb

config.paperclip_defaults = {
  :storage => :s3,
  :s3_credentials => {
    :bucket => ENV['S3_BUCKET_NAME'],
    :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
    :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
  }
}

And If you want to set this environment variables into local then use this:

sudo nano ~/.profile

Then add your variables over here

export S3_BUCKET_NAME="your bucket name"
export AWS_ACCESS_KEY_ID="your access key id"
export AWS_SECRET_ACCESS_KEY="your secret access key"

And then reload your ~/.profile with . ~/.profile

Check added variable with echo $S3_BUCKET_NAME

And for Heroku

You can set your variable like:

heroku config:set S3_BUCKET_NAME="your bucket name"
heroku config:set AWS_ACCESS_KEY_ID="your access key id"
heroku config:set AWS_SECRET_ACCESS_KEY="your secret access key"

Check that variables added or not in heroku with heroku config

For more detail you can refer form here.

Let me know if you need me more..

like image 52
Sachin Gevariya Avatar answered Apr 01 '23 09:04

Sachin Gevariya