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:
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:
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:
But now I see that I have a %3F encoding ("?") in the URL which messes it up.
Paperclip::Errors::InfiniteInterpolationError
error: If you try to change the CDN settings for paperclip you'll get the Infinite Interpolation Error unless you set thepath
attribute explicitly. You can't change the path or your existing assets won't resolve anymore, so you need to set it to the default path setting. For our paperclip version of4.3.6
, this value was:path: ":class/:attachment/:id_partition/:style/:filename"
– Polybasite