I'm using paperclip and aws-sdk gems in a Rails 4 app.
I define the :path option in my paperclip.rb config, with no :url option:
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
It saves my uploaded images like such:
http://s3.amazonaws.com/mybucket-development/profiles/avatars/000/000/026/original/image_file_name.png?1420575189
All fine, it gets saved to S3. However it refuses to let me read the images for display, e.g. =profile.avatar.url(:medium). When I go to that URL in the browser it tells me to re-format it with the bucket name as a domain. Like:
http://mybucket-development.s3.amazonaws.com/profiles/avatars/000/000/026/original/image_file_name.png?1420575189
OK, not a problem either. I go to that URL, I can view my image. So now I need to figure out how to get Paperclip to format the URLs like this automatically. I read in the Paperclip docs that you just have to set
Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
And that I also have to set the :path parameter or I will just get a Paperclip::Errors::InfiniteInterpolationError.
So I set my config file with both combined:
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
Not working... I try scrapping the paperclip.rb and putting it in config/environments/* But no matter what I do, it still saves the URLs without the domain with the bucket name in the path.
So two questions:
1) How can I get Paperclip to automatically format the saved URLs in domain style?
2) Or even better, how can I get S3 to accept the non-domain style URLs, the one that Paperclip is currently generating?
EDIT
So, if I add in the s3_host_name option then it saves the URLs domain style. So I have to have all 3 of:
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
And it will save my URLs on the model like so:
http://mybucket-development.s3-us-west-2.amazonaws.com/profiles/avatars/000/000/026/original/image_file_name.png%3F1420580224
But now I see that I have a %3F encoding ("?") in the URL which messes it up.
You can simply do an HTTP head request to check whether the url exist.
Alright, so as mentioned in the above update, to get the domain-style URLs to be saved by Paperclip I have to include all 3 of the following in my paperclip.rb:
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
Paperclip::Attachment.default_options[:s3_host_name] = 's3-us-west-2.amazonaws.com'
I believe there is a related issue from recent gem upgrades, this produces URLs with encodings that won't work on their own.
So in my views I have had to add URI.unescape, such as
=image_tag URI.unescape(profile.avatar.url(:medium))
I could also set a callback on the model to replace the %3F with the "?" before save.
Strange issue with Paperclip... not sure what was going on. First app I've worked on where I encountered that issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With