Cannot get Paperclip to configure my S3 URLs correctly

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.

+7
ruby ruby-on-rails amazon-s3 paperclip
source share
2 answers

Ok, so as stated in the above update, so that the domain-style URLs are saved using 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 the problem is with recent gem updates, this creates URLs with encodings that won't work on their own.

So, in my views, I had to add a URI.unescape, for example

= image_tag URI.unescape (profile.avatar.url (: medium))

I could also set a callback for the model to replace% 3F with "?" before saving.

A strange problem with Paperclip ... I don't know what was happening. The first application I worked on ran into this problem.

+12
source share

At paperclip.rb

 Paperclip::Attachment.default_options[:s3_host_name] = 's3-ap-south-1.amazonaws.com' 

In production.rb

 config.paperclip_defaults = { storage: :s3, s3_protocol: :https, s3_credentials: { bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), s3_region: ENV.fetch('AWS_REGION') } 

This worked very well for me with image_tag. It should also work for you.

0
source share

All Articles