Sinatra 上传流媒体

发布于 2024-12-28 21:29:39 字数 738 浏览 0 评论 0原文

是否有可能让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花似海 2025-01-04 21:29:39
req['Transfer-Encoding']

不会强制 Net::HTTP 使用分块编码进行上传,而只会让您的 sinatra 认为它是分块的。请参考这个要点来实现分块上传。

Excon 是一个很好的 HTTP Ruby 客户端,用于在此提交之后实现分块上传。

req['Transfer-Encoding']

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文