I use paperclip and aws-sdk gems in a Rails 4 application.
I define the: path option in my paperclip.rb configuration, without the: url option:
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
Saves my uploaded images as follows:
http://s3.amazonaws.com/mybucket-development/profiles/avatars/000/000/026/original/image_file_name.png?1420575189
Everything is fine, it is saved on S3. However, it does not allow me to read images for display, for example. = Profile.avatar.url (: environment). When I go to this URL in the browser, it tells me to reformat it with the name of the bucket as the domain. How:
http://mybucket-development.s3.amazonaws.com/profiles/avatars/000/000/026/original/image_file_name.png?1420575189
OK, no problem. I go to this URL, I can see the image. So now I need to figure out how to get Paperclip to automatically format URLs. I read in Paperclip docs that you just need to install
Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
And that I also need to set the path parameter, or I just get Paperclip :: Errors :: InfiniteInterpolationError.
So, I installed my configuration file together:
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename" Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
Doesn't work ... I'm trying to break paperclip.rb and put it in config / environment / * But no matter what I do, it still saves URLs without a domain with the name of the bucket in the path.
So, two questions:
1) How can I get Paperclip to automatically format saved domain-style URLs?
2) Or better yet, how can I get S3 to accept non-domain-style URLs, the one that Paperclip is currently generating?
EDIT
So, if I add the s3_host_name parameter, it will preserve the URL domain style. Therefore, I must 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 this:
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 the% 3F ("?") Encoding in the URL that messed it up.