Paperclip Amazon S3 setup with Heroku - ruby-on-rails

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

Related

AWS::S3::Errors::PermanentRedirect on Heroku

I'm getting following error on heroku after uploading file through paperclip.
AWS::S3::Errors::PermanentRedirect (The bucket you are attempting to
access must be addressed using the specified endpoint. Please send all
future requests to this endpoint.)
This is my settings in the model
has_attached_file :profile_image,
:styles => { :myrecipes => "260x180#"},
:storage => :s3,
:s3_region => 'us-west-1',
:s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
:path => "/images/:id/:style.:extension",
:url => ":s3_domain_url"
This is working on development and store image on S3 but while I'm trying on production (Heroku) I'm getting error.
To Provide the endpoint you have to do add this into your paperclip_defaults
:s3_host_name => "s3-eu-west-1.amazonaws.com"
Or you can do like this
s3_host_name: "s3-#{ENV['AWS_REGION']}.amazonaws.com"
Ref: paperclip issue

Paperclip + AWS S3, Prefixing remote path with my local path

I'm using Paperclip with a Rails 4 app and Amazon S3 storage. On my development machine, the site is running at
/Users/Jeff/Sites/example.com/web
When I upload a file with Paperclip to S3, the remote path in S3 inherits my local folder structure.
http://s3.amazonaws.com/example_com_bucket/Users/Jeff/Sites/example.com/web/public/assets/uploads/my_class/8/medium/some_image.png?1383060287
Why is this happening? How do I strip that part out? I tried changing the :path property but that only seemed to affect the "application" part of the path (e.g. after /assets/uploads) My site is still in development, so I don't care about having to preserve links.
My config is...
config.paperclip_defaults = {
:storage => :s3,
:path => '/:class/:attachment/:id_partition/:style/:filename',
:s3_credentials => {
:bucket => 'example_com_bucket',
:access_key_id => '...',
:secret_access_key => '...'
}
}
I had this exact same issue when I was using the :url parameter where I should have been using the :path parameter:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:url => '/product/:attachment/:id/:style/:filename'
I fixed it by changing my config to this:
has_attached_file :primary_photo,
:styles => ...,
:storage => :s3,
:s3_host_name => 's3-us-west-2.amazonaws.com',
:s3_credentials => 'config/s3.yml',
:path => '/product/:attachment/:id/:style/:filename'

Rails, PaperClip, S3, Heroku: Model icon fields not being saved

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"

How to use rails plus paperclip and fog in localhost

I'm trying to setup my development environment to store and fetch images in local host.
I've manage to save the images on the correct path, but I can't find a way to load the page and retrieve them from the assets pipeline.
I have this on my model:
has_attached_file :cover, :styles => {:small => '80x80'},
:storage => :fog,
:fog_credentials => {:provider => "Local",
:local_root => "#{Rails.root}/public"},
:fog_directory => 'system/migos',
:fog_host => "http://localhost:3000/assets",
:default_url => '/assets/missing/:attachment/missing_:class_:style.png',
:path => ':rails_env/:class/:attachment/:id_partition/:style/:filename'
and the file gets saved correctly to:
public/system/migos/development/workgroups/covers/000/000/011/small/logo.png
When loading the page, it tries to fetch the file from here:
/assets/localhost/development/workgroups/covers/000/000/011/small/logo.png?1346598225
and fails.
What am I missing here?
has_attached_file :photo,
:url => "/assets/vehicles/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/vehicles/:id/:style/:basename.:extension"
Try the code from above, check the URL and PATH, it will return the next image address:
http://localhost:3000/assets/vehicles/1/original/72854906.jpg?1346092386
the folder structure is assets/vehicles/1/original.

How to store files on amazon S3 conditionally based on what environment you are in

I'm using Paperclip, and this code, along with the aws-s3 gem, allows me to store file uploads with Amazon S3:
has_attached_file :photo,
:styles => {
:tiny => "25x25#",
:shown => "40x40#",
:thumbnail => "50x50#",
:small => "150x150>",
:medium => "300x300>" },
:default_url => "/images/default_:style.jpg",
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "profile/:attachment/:style/:id.:extension"
However I do not want to store files on Amazon S3 when I am in my development environment. How do I set that in my application?
you could probably do something like
:storage => Rails.env.production? ? :s3 : :whatever
In the end of environment.rb:
APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env]
In config/config.yml:
development:
use_amazon: false
test:
use_amazon: false
production:
use_amazon: true
And in your controller:
if APP_CONFIG['use_amazon']
#USING AMAZON S3
else
#USING SOMETHING ELSE
end
This should work.
Good Luck!

Resources