Sinatra 上传流媒体
是否有可能让 sinatra 应用程序路由通过带有正文流的 POST 处理大文件上传,如下所示:
uri = URI('http://0.0.0.0:4567/files')
file = File.open("/path/to/1.iso")
req = Net::HTTP::Post.new(uri.path)
req.content_type = 'application/octet-stream'
req['Transfer-Encoding'] = 'chunked'
req.body_stream = file
Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
这是针对 Web 服务的,目前,我不知道如何处理此问题,我试图向我的实际路由发出此请求:
post '/file' do
File.open('/path/to/downloaded.iso', 'ab') do |file|
file << request.body.read
end
end
但它显然失败了:
`!! Unexpected error while processing request: closed stream`
也无法让非流版本工作。
非常感谢任何帮助,谢谢。
Is it possible to have a sinatra app route handling large files upload through a POST with body streaming like these:
uri = URI('http://0.0.0.0:4567/files')
file = File.open("/path/to/1.iso")
req = Net::HTTP::Post.new(uri.path)
req.content_type = 'application/octet-stream'
req['Transfer-Encoding'] = 'chunked'
req.body_stream = file
Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
This is for a web service, currently, I cannot see how to handle this and I was trying to issue this request to my actual route:
post '/file' do
File.open('/path/to/downloaded.iso', 'ab') do |file|
file << request.body.read
end
end
But it obviously fail with:
`!! Unexpected error while processing request: closed stream`
Also cant get a not streaming version to work.
Would really appreciate any help, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不会强制 Net::HTTP 使用分块编码进行上传,而只会让您的 sinatra 认为它是分块的。请参考这个要点来实现分块上传。
Excon 是一个很好的 HTTP Ruby 客户端,用于在此提交之后实现分块上传。
does not force Net::HTTP to use chunked encoding for uploads, but only makes your sinatra think it is chunked. Please refer this gist to implement chunked upload.
Excon is a nice HTTP Ruby client to implement chunked uploads after this commit.