Ruby 在块中发送文件总是在第10个块时崩溃

Ruby 在块中发送文件总是在第10个块时崩溃,ruby,Ruby,我的超简单方法有一个奇怪的问题。它将一个4MB的文件发送到外部API。问题是,总是在第10块,外国API崩溃 def upload_chunk() file_to_send = File.open('file.mp4', 'rb') until file_to_send.eof? @content = file_to_send.read 4194304 # Get 4MB chunk upload_to_api(@content) # Line that produce

我的超简单方法有一个奇怪的问题。它将一个4MB的文件发送到外部API。问题是,总是在第10块,外国API崩溃

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end
无法调试API错误,但它显示:
指定的blob或块内容无效
(该API是Azure存储API,但目前并不重要,问题显然在我这边)

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end
因为它在第10个元素(第40个兆比特)崩溃,所以测试它很痛苦,“手动”调试它需要很多时间(部分原因是我的互联网连接速度差),我决定分享我的方法

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end

你能看到这个代码有什么错误吗?请记住,它总是在第10次崩溃,对于大小小于40 MB的文件非常有效。

我搜索了
ruby“指定的blob或block内容无效”
,并发现这是第二个链接(第一个是此页面):

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end
这包括:

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end
如果通过将blob拆分为块来上载blob,并且出现上述错误,请确保块的块ID具有相同的长度。如果块的块ID具有不同的长度,则会出现此错误

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end
因此,我的第一个猜测是,调用
upload\u to_api
是从1-9分配id,然后当它变为10时,id长度会增加,从而导致问题

def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end

如果您无法控制ID的生成方式,那么您可以将每次迭代读取的字节数设置为不超过文件总大小的1/9。

哦,天哪,我无法形容我现在有多傻。我预先假设将文件剪切成块的过程有问题,甚至没有检查错误的确切含义。非常感谢马特!
def upload_chunk()
  file_to_send = File.open('file.mp4', 'rb')

  until file_to_send.eof?
    @content = file_to_send.read 4194304 # Get 4MB chunk
    upload_to_api(@content) # Line that produces the error
  end
end