无法使Paperclip正确设置我的S3 URL

rfi*_*535 8 ruby ruby-on-rails amazon-s3 paperclip

我在Rails 4应用程序中使用paperclip和aws-sdk gems.

我在paperclip.rb配置中定义了:path选项,没有:url选项:

Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
Run Code Online (Sandbox Code Playgroud)

它保存我上传的图像,如:

http://s3.amazonaws.com/mybucket-development/profiles/avatars/000/000/026/original/image_file_name.png?1420575189

一切都很好,它被保存到S3.但是它拒绝让我读取要显示的图像,例如= profile.avatar.url(:medium).当我在浏览器中转到该URL时,它告诉我使用存储桶名称作为域重新格式化它.喜欢:

http://mybucket-development.s3.amazonaws.com/profiles/avatars/000/000/026/original/image_file_name.png?1420575189

好的,也不是问题.我转到那个URL,我可以查看我的图像.所以现在我需要弄清楚如何让Paperclip自动格式化这样的URL.我在Paperclip文档中读到了你必须设置的内容

Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
Run Code Online (Sandbox Code Playgroud)

而且我还必须设置:path参数,否则我将得到一个Paperclip :: Errors :: InfiniteInterpolationError.

所以我设置我的配置文件两者结合:

Paperclip::Attachment.default_options[:path] = ":class/:attachment/:id_partition/:style/:filename"
Paperclip::Attachment.default_options[:url] = ":s3_domain_url"
Run Code Online (Sandbox Code Playgroud)

不工作......我尝试废弃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'
Run Code Online (Sandbox Code Playgroud)

它会像我这样在模型上保存我的URL:

http://mybucket-development.s3-us-west-2.amazonaws.com/profiles/avatars/000/000/026/original/image_file_name.png%3F1420580224

但现在我看到我在URL中有一个%3F编码("?"),这会使它混乱.

rfi*_*535 13

好吧,正如上面的更新中所提到的,要获得Paperclip保存的域式URL,我必须在paperclip.rb中包含以下所有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'
Run Code Online (Sandbox Code Playgroud)

我相信最近的宝石升级存在一个相关的问题,这会产生带有编码的URL,这些编码本身不起作用.

所以在我看来,我不得不添加URI.unescape,例如

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

我还可以在模型上设置回调,用"?"替换%3F 在保存之前.

Paperclip的奇怪问题......不确定发生了什么.第一个应用程序我已经在我遇到该问题的地方工作过.

  • 这是有效的,因为您正在设置:s3_host_name,它会覆盖其他设置.你实际上可以删除:url和:path它仍然有效.我今天查看了回形针的源代码,它似乎没有正确实现':s3_domain_url'逻辑 - 或者至少,它对我来说没有意义. (2认同)