我在Rails 4应用程序中使用paperclip和aws-sdk gems .
我在paperclip.rb配置中定义了:path选项,没有:url选项:
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
它保存我上传的图像,如:
一切都很好,它被保存到S3 . 然而,它拒绝让我阅读图像以供显示,例如= profile.avatar.url(:中) . 当我在浏览器中转到该URL时,它告诉我使用存储桶名称作为域重新格式化它 . 喜欢:
好的,也不是问题 . 我转到那个URL,我可以查看我的图像 . 所以现在我需要弄清楚如何让Paperclip自动格式化这样的URL . 我在Paperclip文档中读到了你必须设置的内容
Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
而且我还必须设置:path参数,否则我将得到一个Paperclip :: Errors :: InfiniteInterpolationError .
所以我设置我的配置文件两者结合:
Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
不工作......我尝试废弃paperclip.rb并将其放入config / environments / *但是无论我做什么,它仍然保存URL,而不包含路径中存储桶名称的域 .
所以有两个问题:
1)如何让Paperclip自动格式化域样式中保存的URL?
2)或者甚至更好,我怎样才能让S3接受非域式URL,这是Paperclip当前正在生成的URL?
编辑
因此,如果我添加s3_host_name选项,则会保存URL域样式 . 所以我必须拥有所有3个:
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'
它会将我的URL保存在模型上,如下所示:
但是现在我看到我在URL中有一个%3F编码(“?”),这使得它变得混乱 .
2 回答
好了,正如上面的更新中所提到的,要获得Paperclip保存的域式URL,我必须在paperclip.rb中包含以下所有3个:
我相信最近的gem升级存在一个相关的问题,这会产生带有编码的URL,这些编码本身不起作用 .
所以在我看来,我不得不添加URI.unescape,例如
= image_tag URI.unescape(profile.avatar.url(:medium))
我还可以在模型上设置回调以用“?”替换%3F在保存之前 .
Paperclip的奇怪问题......不确定发生了什么 . 第一个应用程序我已经在我遇到该问题的地方工作过 .
在paperclip.rb中
在production.rb
使用image_tag,这对我来说非常有用 . 它也适合你 .