I'm using Paperclip to handle image uploads for my Rails app, and it's working great when I use system storage: multiple thumbnail sizes ("styles" in Paperclip parlance) are saved to file, and I can access any of them by passing the style name to the url method.
When I set up the app to store images on S3 (using the aws-s3 gem), however, only one image is stored in my S3 bucket. For what it's worth, only the last style listed is stored. So, if in my model, I've got:
has_attached_file :photo,
:styles => { :large => "1000x1000>",
:medium => "600x600>",
:thumb => "200x200>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => AppConstants.bucket,
:path => "pictures/:id/:filename"
Only the "thumb" size will be saved to S3.
Has anybody encountered a similar problem? How can I fix this?
I'm not sure why this works locally, but you didn't specify :style in your path declaration.
Related
Where I can find my images in rails app folders? I upload them by CKeditor and doesn't know where are they.
https://github.com/sferik/rails_admin
You'll need to create a model to handle the image uploads, typically with the likes of Paperclip or Carrierwave
From CKEditor's Github:
For files uploading support you need generage models for file storage.
Currently supported next backends:
ActiveRecord (paperclip, carrierwave, dragonfly)
Mongoid (paperclip, carrierwave, dragonfly)
Image Upload Model
You'll have a CKEditor model directory (with attachment_file.rb inside) - just add Paperclip options for it. Here's a question which will show you what to do: How exactly DO you integrate ckeditor with Paperclip so it can upload image files?
Yes you can. I assume that you have paperclip already set up for S3.
So you have only edit the picture.rb and attachement_file.rb in you
model directory (app/model/ckeditor/) and replace these lines
has_attached_file :data,
:url => "/ckeditor_assets/attachments/:id/:filename",
:path => ":rails_root/public/ckeditor_assets/attachments/:id/:filename" with
your papeclip version has_attached_file:
has_attached_file :data, :styles => { :content => '575>', :thumb =>
'80x80#' },
:storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml", :path => ":attachment/:id/:style.:extension",
:url => ":s3_domain_url" That's it. Btw: this is example from Rails 3.
I'm using the latest and greatest Mercury editor in my Rails 3.2.9 application.
I have adapted the Image model to allow uploading to s3:
has_attached_file :image,
:styles => { :medium => "200x115" },
:storage => :s3,
:s3_protocol => 'https',
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style/:filename",
:url => ":attachment/:id/:style/:filename",
:bucket => 'ps-wifi'
This is working fine.
I'm trying to figure out how I can allow the user to upload multiple images, each with different attributes.
For example, the above method works for a logo but we're need another, larger image at 600 x 350. Previously, I had another "has_attached_file" but am not sure how to achieve with Mercury.
I am using Rails 3.2 + Heroku + S3 + Paperclip to store an icon on my User model. The model is not saving the 4 icon fields though. The images are getting processed and saved on S3 correctly and no errors are occurring. I also have another model that has a document being stored via Paperclip and S3. That model works perfectly in all cases. The User icon works locally but not on Heroku.
production.rb relevant configuration
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_ACCESS_KEY_ID'],
:secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
}
}
User model code:
class User < ActiveRecord::Base
attr_accessible :icon
has_attached_file :icon, :url => "/system/:rails_env/:attachment/:style/:hash.:extension",
:hash_data => ":class/:attachment/:id",
:hash_secret => "superSecretThing",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/blank.png"
...
Controller code: (This code is kind of crazy because I am AJAXing files Base64 encoded.)
params[:user][:icon_data]
decoded_file = Base64.decode64(data)
begin
split_name = params[:user][:icon_file_name].split(".")
file = Tempfile.new([split_name[0..-2].join("."), ".#{split_name[-1]}"])
file.binmode
file.write(decoded_file)
file.close
#user.icon = open(file)
#user.icon_file_name = params[:user][:icon_file_name]
ensure
file.unlink
end
#user.save
I do an almost identical process on another model with a Paperclip attachment and it works flawlessly. In both cases the attachment is being saved correctly to S3 and no errors are being raised. This gist has example output for a controller action from the Heroku logs.
I am pretty baffled because the other model works fine. The only real difference is that the User attachment does image processing but that part appears to be working fine.
The problem is the same as this one, but the solution there does not apply.
Thoughts?
So the problem is that not including the :path argument makes it try to use the :url parameter for both the url and the path. The real fix is to include the :path parameter in addition to the url.
So for example a fixed configuration that works both locally and on Heroku:
has_attached_file :icon,
:url => "/system/:rails_env/:attachment/:style/:hash.:extension",
:path => "public/system/:rails_env/:attachment/:style/:hash.:extension",
:hash_data => ":class/:attachment/:id",
:hash_secret => "superDuperSecret",
:styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "/blank.png"
I am using Paperclip for uploading the Profile image in my application (rails)
My User model is having a
has_attached_file :avatar,
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
:styles => { :medium => "300x300>", :small => "100x100>", :thumb => "50x50>", :micro => "30x30>" }
In my VIew If i have a profile image uploaded then its pointing correctly as
/avatars/1/thumb/iamge
BUt if the image is not there if they didn't uploaded in that case its pointing as
/avatars/thumb/missing.png which doesn't have any image.
Please give suggestions what to do if the user didnt uploaded any profile image..
You can make a default image and put it there and name it missing.png. It is like extra functionality :)
has_attached_file :image, :storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", :path => "/:style/:filename"
I'm not sure what :path => "/:style/:filename" is.
I also want to to include the style for this attached image, is that what the :path is?
the style I want is this: :styles => { :medium => "275x275>", :thumb => "175x155>" }
Basically what's going on here is that I'm setting up on heroku and I'm having to use S3 which seems straightforward just not used to this attachment convention stuff.
Also, I just signed up for an S3 account... but heroku was spouting that its free or something. What's the deal with that?
The 'path' specifies the location on S3 where the files will be stored. Thus, if you specify an attachment as:
has_attached_file :image,
:styles => { :medium => "275x275>", :thumb => "175x155>" },
:storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
:path => "user/:attachment/:style/:id.:extension"
A sample URL will be:
http://s3.amazonaws.com/bucket/user/image/thumb/347853856.jpg
Finally, S3 is NOT free (Heroku simply states transfer / uploads are not counted in the usage based calculations). Heroku's documentation is excellent if you need further information.
Note that in Rails 3.1 and above, it should be Rails.root and not RAILS_ROOT