简体   繁体   English

大约50%的时间使用错误的S3 URL保存回形针附件

[英]Paperclip attachment saving with incorrect S3 URL about 50% of the time

Update: 更新:

So this is definitely related to the :hash_data option, specifically the :updated_at segment. 因此,这肯定与:hash_data选项有关,特别是与:updated_at段有关。 Somehow the files are being saved to the S3 bucket with a different :updated_at value than Paperclip uses to read the file. 以某种方式将文件保存到S3存储桶中,其值与Paperclip用于读取文件的方式不同::updated_at。 Could this be due to some race condition, considering that it occurs intermittently? 考虑到它是间歇性发生的,这是否可能是由于某些比赛条件引起的? As I mentioned below, this issue began after upgrading Paperclip to 4.2.1. 正如我在下面提到的,此问题是在将Paperclip升级到4.2.1之后开始的。

I will greatly appreciate any thoughts/tips you guys have on this. 我将非常感谢你们在此方面的任何想法/建议。 Thank you! 谢谢!


When uploading images via Paperclip to S3 bucket, it sometimes saves the files with a different filename than that returned by the attachment#url method. 通过Paperclip将图像上传到S3存储桶时,有时会使用与attachment#url方法返回的文件名不同的文件名保存文件。 For example, an image is saved to 例如,将图像保存到

main_event_photos_46_47fd4f3c2fea17fbb7a0bd27c648911557f9e12b_main.png

However calling @event.main_event_photo.url(:main) returns 但是调用@ event.main_event_photo.url(:main)返回

main_event_photos_46_15744de74a36207b672356b5ad4c6b30eb4ba85f_main.png

So as you can see, the :hash section of the interpolation does not match, and I have no way of finding the actual url besides opening the bucket in the S3 console. 如您所见,插值的:hash部分不匹配,除了在S3控制台中打开存储区之外,我没有其他方法可以找到实际的url。 This issue seems to occur about half the time. 此问题似乎发生一半左右。 Sometimes uploading the exact same file does save properly, and the url method accesses it correctly. 有时,上传完全相同的文件确实可以正确保存,并且url方法可以正确访问它。

This issue began occurring after we upgraded Rails/Ruby/Paperclip. 在升级Rails / Ruby / Paperclip之后,便开始出现此问题。 We're now using: 我们现在正在使用:

  • Ruby 2.1.5 Ruby 2.1.5
  • Rails 4.2.0 Rails 4.2.0
  • Paperclip 4.2.1 回形针4.2.1

Note that on development, files always save correctly (local filesystem). 请注意,在开发中,文件始终正确保存(本地文件系统)。 I have scoured Stackoverflow and Google to no avail. 我已经搜索了Stackoverflow和Google都没有用。 Please let me know if I can provide any additional information. 请让我知道是否可以提供其他信息。 Thank you! 谢谢!

EDIT: 编辑:

Model: 模型:

has_attached_file :main_event_photo, {
:styles        => {   :original => {:geometry => "1280x800#", :format => 'png'},
                      :main => {:geometry => "640x400#", :format => 'png'},
                      :thumb => {:geometry => "330x220#", :format => 'png'}
},
:convert_options => {:original    => '-quality 80',
                     :main    => '-quality 80',
                     :thumb    => '-quality 80'
},
:default_style => :main
}.merge!(PAPERCLIP_STORAGE_OPTIONS) # this is defined in the config/environments

validates_attachment_content_type :main_event_photo, :content_type => ['image/jpeg', 'image/png', 'image/gif', 'image/x-png', 'image/pjpeg']
validates_attachment_presence :main_event_photo

Form (basically): 形式(基本上):

<%= simple_form_for(@event, :url => { :action => @event.id.nil? ? "create" : "update" }) do |f| %>
  <%= f.file_field :main_event_photo %>
<% end %>

Note we have many models with Paperclip attachments, and the issue occurs on each. 请注意,我们有很多带有Paperclip附件的型号,并且每个型号都有问题。

So this turned out to be the result of a bug. 因此,事实证明这是错误的结果。 I upgraded Rails to 4.2.1.rc1 which was released last week, and the issue was resolved. 我将Rails升级到4.2.1.rc1发布的4.2.1.rc1 ,此问题已解决。 If anyone wants more information, check out the thread on Github: https://github.com/thoughtbot/paperclip/issues/1772 . 如果有人想了解更多信息,请查看Github上的线程: https : //github.com/thoughtbot/paperclip/issues/1772 It includes a workaround for those who can't upgrade Rails. 它为无法升级Rails的人提供了一种解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM