大部分时间都可以工作,但偶尔会因此错误而失败。我正在使用CarrierWave将文件上传到远程S3存储桶,如果有所不同。
Edit:我已经在控制台上进行了一些挖掘,坦率地说,我更加困惑!我有两个用户记录,均带有个人资料图片通过载体波上传到S3。如果我只是隔离了
File.read
方法并使用两个记录都可以尝试使用,而一件记录则无效。检查URI,它们似乎几乎是相同的。,但是,我发现CarrierWave支持一个快捷方式来读取解决问题的文件。这是更新的代码:
attachments["#{@user.last_name.downcase}-#{@user.date_of_birth.strftime('%d%m%y')}.jpeg"] = @user.profile_picture.read
少于
10kb
的任何东西都以StringIO
File
。无法在文档中找到实际参考,因此这里是来源:StringMax = 10240
def <<(str)
@io << str
@size += str.length
# NOTE: once you pass 10kb mark, you get a tempfile.
# v
if StringIO === @io && StringMax < @size
require 'tempfile'
io = Tempfile.new('open-uri')
io.binmode
Meta.init io, @io if Meta === @io
io << @io.string
@io = io
end
end
https://github.com/ruby/ruby/blob/v3_2_0/lib/open-uri.rb#l398
LLET的测试:
>> require "open-uri"
>> File.write("public/small.txt", "a"*10240)
=> 10240
>> URI.open("http://localhost:3000/small.txt").class
=> StringIO
>> File.write("public/large.txt", "a"*10241))
=> 10241
>> URI.open("http://localhost:3000/large.txt").class
=> Tempfile
您可以File.read
Tempfile
File.read
修复是在
uri上致电
StringIO
::
read
https://rubyapi.org/3.2/o/openuri/openread#method-i-read