Ruby on Rails, Paperclip, Amazon Aws S3 & Heroku

Ruby on Rails, Paperclip, Amazon AWS S3 & Heroku

Had this problem before!

Solved it by putting the bucket vars in the model itself (live code):

 #app/models/image.rb
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "**********",
:storage => :s3,
:bucket => '*****',
:s3_credentials => S3_CREDENTIALS

#config/application.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}

#config/initializers/s3.rb
if Rails.env == "production"
# set credentials from ENV hash
S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "*****"}
else
# get credentials from YML file
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end

#config/application.yml ([figaro][1] gem)
S3_KEY: ********
S3_SECRET: **********

We also have this in our production.rb:

#app/environments/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"

The ENV['FOG_DIRECTORY'] is the bucket name, and there's also one for different regions. Here's a very good resource for you (the answer with 15 upvotes... not the accepted answer)

Paperclip + S3 + Heroku, but something went wrong

So, it was pretty simple.

It seems that Paperclip doesn't support more recent versions of aws-sdk. So I had to change my Gemfile in order to use aws-sdk 2.0 or older.

gem 'aws-sdk', '< 2.0'

That solved the problem.

Thanks for all the help. Using the environment variables of the AWS keys in development.rb helped a lot on debugging the code.

Paperclip does not show image but instead shows title with amazon s3 in rails?

I have followed the heroku guide and was successfully able to upload the user image to S3 using paperclip in development env and I can also see the image in users's profile.

My Gemfile:

gem 'paperclip'
gem 'aws-sdk-s3'

My development.rb

config.paperclip_defaults = {
storage: :s3,
s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com",
s3_protocol: :https,
s3_credentials: {
bucket: ENV.fetch('S3_BUCKET_NAME'),
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
s3_region: ENV.fetch('AWS_REGION'),
}
}

you have this (this might have caused an issue):

s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com",

I have this (a dot after s3):

s3_host_name: "s3.#{ENV['AWS_REGION']}.amazonaws.com",

My user.rb is same as yours

has_attached_file :avatar,
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => ":style/missing.jpg"

# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/

My show.html.erb

<%= image_tag @user.avatar.url(:medium) %>

I am using rails 5.2 version and with above code it works for me. Also do check whether your S3 bucket has public access to objects.

Ruby on Rails, Paperclip, Heroku, GitHub and AWS - securing keys

You need use the ENV variable from your heroku app.

If you do a heroku config you can have access to all of your ENV variable. You just add some and use it directly in your application.

With this trick you don't need update your code to change your configuration and the configuration if not define in your code base.

In your s3.yml you just need do :

access_key_id: <%= ENV['S3_ACCESS_KEY'] %>
secret_access_key: <%= ENV['S3_SECRET_KEY'] %>
bucket: <%= ENV['S3_BUCKET_NAME'] %>

And add this ENV VARIABLE in your heroku app

heroku config:add S3_ACCESS_KEY='your_key'
heroku config:add S3_SECRET_KEY='your_secret'
heroku config:add S3_BUCKET_NAME='your_nucket_name'


Related Topics



Leave a reply



Submit