RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)
Set up Ruby on Rails with Paperclip and S3 using AWS SDK
Uploading Files to S3 in Ruby with Paperclip
Paperclip requires the following gems added to your Gemfile.
If your paperclip version is 5.1.0 then we are using 'aws-sdk' version 2.3.
# Gemfile
gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
gem 'paperclip'
gem 'aws-sdk', '~> 2.3'
or our paperclip version is 4.1.0 then we need to use 'aws-sdk' version < 2 (note: add version less than 2.0 otherwise you will get paperclip error)
gem 'paperclip'
gem 'aws-sdk', '< 2.0'
Run bundle install and restart the Rails server after modifying the Gemfile.
gem 'aws-sdk', '< 2.0'
Run bundle install and restart the Rails server after modifying the Gemfile.
Then run the command :-
rails generate paperclip user image
Define the file attribute in the Model
class User < ActiveRecord::Base
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
Migrations: Now our migration file look like :-
class AddAvatarColumnsToUsers < ActiveRecord::Migration
def up
add_attachment :users, :image
end
def down
remove_attachment :users, :image
end
end
View Page :-
<%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
<%= form.file_field :image %>
<% end %>
Now in the controller :-
def create
@user = User.create( user_params )
end
private
def user_params
params.require(:user).permit(:image)
end
In our view page we can show the image using :-
<%= image_tag @user.image.url %>
<%= image_tag @user.image.url(:medium) %>
<%= image_tag @user.image.url(:thumb) %>
After that :-
S3 bucket Implementation:-
def up
add_attachment :users, :image
end
def down
remove_attachment :users, :image
end
end
View Page :-
<%= form_for @user, url: users_path, html: { multipart: true } do |form| %>
<%= form.file_field :image %>
<% end %>
Now in the controller :-
def create
@user = User.create( user_params )
end
private
def user_params
params.require(:user).permit(:image)
end
In our view page we can show the image using :-
<%= image_tag @user.image.url %>
<%= image_tag @user.image.url(:medium) %>
<%= image_tag @user.image.url(:thumb) %>
After that :-
S3 bucket Implementation:-
1. Go to Aws Console
2. Create the s3 bucket with bucket name and choose specific region.
3. Get the app_key and app_secret (link: https://www.multcloud.com/tutorials/s3-key.html)
We’ll also need to specify the AWS configuration variables for the development/production Environment.
# config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
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')
}
}
class User < ActiveRecord::Base
has_attached_file :image,:styles => { :icon => "50x50>", :small => "150x150", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/assets/icons/picture.png", :storage => :s3,:s3_credentials => "#{Rails.root}/config/aws_s3.yml",:url => ':s3_domain_url', :path=> ":attachment/:id/:style/:filename"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
# config/aws_s3.yml
development:
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')
WE CAN REFER :-
https://github.com/thoughtbot/paperclip
https://devcenter.heroku.com/articles/paperclip-s3#define-the-file-attribute-in-the-model
https://coderwall.com/p/vv1iwg/set-up-ruby-on-rails-with-paperclip-5-and-s3-using-aws-sdk-v2
http://www.korenlc.com/rails-uploading-photos-via-amazon-s3-and-paperclip/
2. Create the s3 bucket with bucket name and choose specific region.
3. Get the app_key and app_secret (link: https://www.multcloud.com/tutorials/s3-key.html)
We’ll also need to specify the AWS configuration variables for the development/production Environment.
# config/environments/production.rb
config.paperclip_defaults = {
storage: :s3,
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')
}
}
If we are getting problem with http or https s3 image path then you can do that :-
config.paperclip_defaults = { storage: :s3, s3_protocol: :http, s3_host_name: "s3.us-east-2.amazonaws.com", bucket: ENV.fetch('S3_BUCKET_NAME'), s3_credentials: { access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), s3_region: ENV.fetch('AWS_REGION') }, s3_permission: { :original => :public }}
OR IF YOU ARE CREATING NEW FILE IN CONFIG FOLDER FOR SAVING THE S3 CREDENTIALS THEN WE JUST HAVE TO CHANGE OUR MODEL SETTING AND WE CAN GET THE S3_CREDENTIALS FROM CONFIG FILE :-
Model :-class User < ActiveRecord::Base
has_attached_file :image,:styles => { :icon => "50x50>", :small => "150x150", :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/assets/icons/picture.png", :storage => :s3,:s3_credentials => "#{Rails.root}/config/aws_s3.yml",:url => ':s3_domain_url', :path=> ":attachment/:id/:style/:filename"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
# config/aws_s3.yml
development:
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')
WE CAN REFER :-
https://github.com/thoughtbot/paperclip
https://devcenter.heroku.com/articles/paperclip-s3#define-the-file-attribute-in-the-model
https://coderwall.com/p/vv1iwg/set-up-ruby-on-rails-with-paperclip-5-and-s3-using-aws-sdk-v2
http://www.korenlc.com/rails-uploading-photos-via-amazon-s3-and-paperclip/
Comments