Paperclip and Amazon S3 how to do paths?
Try this:
has_attached_file :image,
:storage => :s3,
:bucket => 'mybucket',
:path => "/image/:id/:filename",
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
}
Change path of images uploaded on Amazon S3 using PaperClip in Rails
The problem is that you are trying to assign it a new name and succeeding, but you aren't telling it to do this in a format that s3 understands.
The thing you have to remember, is that s3 buckets work based on keys and objects. If you look at an s3 bucket, the folder structure is just for show, for the most part. The file path (including the file name) is essentially the key, and the object is the image stored there (in this case).
So, the key you are assigning the image is the default paperclip path (source:
paperclip docs), which ends in :file_name
in this block
has_attached_file :image, :styles => {:thumb => "200x200#"},
:storage => :s3,
You have a comma at the end of your has_attached_file, which I assume means you deleted things like bucket_name:
(which is fine, but next_time, replace any sensitive info with a placeholder name. it makes the problem easier to understand).You should have a path:
symbol associated with the key used to access the s3 object. Usually, paperclip auto-generates this for you, but here you want to manually assign it. So you should be able to add something like this:
has_attached_file :image, :styles => {:thumb => "200x200#"},
:storage => s3,
:path => "/gov_id/:class/:attachment/:style/:file_name"
If you would like the '000/000/001' then put :path => "/gov_id/:class/:attachment/:id_partition/:style/:file_name"
I assume you want to have style in there so that it will deal with both the :original and :thumb style appropriately.
Also, instead of using a before_save, you might want to look into Paperclip.interpolates
something like:
has_attached_file :image, :styles => {:thumb => "200x200#"},
:storage => s3,
:path => "/gov_id/:class/:attachment/:style/:replaced_file_name"
Paperclip.interpolates :replaced_file_name do
extension = File.extname(image_file_name).gsub(/^\.+/, '')
new_image_file_name = "gov_#{self.attachable.reference_code}.#{extension}"
new_image_file_name
end
Rails 4, Paperclip, Amazon S3 Config Amazon Path
If you're going to use S3, we've found that you have to include the S3 credentials in your actual model (not just the config files). Here's what we do:
Model
#Image Upload
Paperclip.options[:command_path] = 'C:\RailsInstaller\ImageMagick'
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "****",
:storage => :s3,
:bucket => '****',
:s3_credentials => S3_CREDENTIALS,
:url => "/:image/:id/:style/:basename.:extension",
:path => ":image/:id/:style/:basename.:extension"
config/application.rb # Paperclip (for Amazon) (we use EU servers)
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
config/s3.yml#Amazon AWS Config
development:
access_key_id: **********
secret_access_key: **************
bucket: ****
production:
access_key_id: ***********
secret_access_key: ***********
bucket: ****
Hope this helps? Setting URL/PATH correctly for AWS S3 by using Paperclip/Rails
In Paperclip, you can use interpolations for that.
Your has_attached_file
method would look somewhat like this;
has_attached_file :image, :default_url => "/pdf/:collection_id/:title/:basename.:extension"
Create an interpolation file, called paperclip.rb
or interpolations.rb
in the config/initializers
directory (rails picks up any script in that folder on startup), which contains code that looks somewhat like this;Paperclip.interpolates :collection_id do |attachment, style|
attachment.instance.collection_id
end
Add the :title
interpolation in the same way; add it to your has_attached_file
urls, and create a second interpolation for that.You can read more about this at https://github.com/thoughtbot/paperclip/wiki/Interpolations
In your case, I would suggest to also include the :id of the Letter in the URL, as it might be possible the user uploads two documents with the same title which might conflict.
has_attached_file :image, :default_url => "/pdf/:collection_id/:id/:title"
Paperclip uses the interpolations :basename
, :extension
and :style
by default to create a unique path for the file.:basename
is the base file name of the uploaded file:extension
is the extension of that uploaded file:style
is the "style" or size of that uploaded file
Read more about styles here; https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
Always try to keep the original file as it can be handy in the future; when your site/application layout changes and new thumbnail sizes are required. You can rebuild/regenerate your whole thumbnail library from the original version.
Read more about generating/regenerating thumbnails here; https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation#generatingregenerating-your-thumbnails
Paperclip and amazon s3 - image path includes bucket region
Didn't read far enough. Oops.
Added this to initializers/paperclip.rb:
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
Related Topics
Explanation of Ruby Code for Building Trie Data Structures
Can't Install Nokogiri for Ruby in Windows
How to Fix Difference in Behavior of Activesupport 3.0.0 Compare to 2.X
Automatically Logging Exceptions in Ruby
Changing The Reading Order of Rubygem Sources
Need Help on Reading Emails with "Mail" Gem in Ruby
Persistent Tcp Connection in Rails App
Robust Way to Deploy a Rack Application (Sinatra)
Ruby/Active Record: Custom Sorting Order
Bundler Using Wrong Ruby Version
Can Sunspot Search Inside Array
Understanding The Behaviour of Inject Used with a Lambda in Ruby
Ruby on Rails Private Link Sharing: Google Docs Style
How to Get Nokogiri to Parse and Return an Xml Document