进行大文件下载并传递给客户端(代理)

huangapple go评论82阅读模式
英文:

Go huge file download and passing to client (proxifying)

问题

我有一个基于Martini的小应用程序,面临一个无法解决的问题。

我想添加一个应用程序功能,允许用户通过对HTTP头进行一些更改从第三方服务器获取文件。类似代理的功能。这些文件相当大(每个文件200+Mb),我希望这些文件能够“流式传输”到客户端。所谓“流式传输”,是指文件数据在应用程序接收到第一批字节后立即返回给客户端,而不需要将整个文件保存在磁盘和/或内存中。

在Martini和Go中是否可能实现这一点?

英文:

I have a small Martini-based application and am faced with an issue that I can't solve.

I want to add an application feature that would allow the user to get files from a 3rd server with some changes in HTTP headers. Some kind of proxy. The files are quite big (200+Mb each) and I want these files to be "streamed" to the client. By "stream" I mean that file data should start to return to the client right after the first bytes were recieved by the application without keeping the whole file on disk and/or in memory.

Is this possible with Martini and Go?

答案1

得分: 2

是的,一般情况下使用Go是可以实现的,但我对Martini并不太熟悉。调用远程文件后的HTTP响应返回一个Reader接口,而你的请求处理程序有一个Writer接口。这意味着你可以读取数据流并写入数据流。你只需要处理你想要的数据,并将转发的数据流“补丁”到请求流中。

Go甚至在标准库中内置了一个ReverseProxy实用程序:

http://golang.org/pkg/net/http/httputil/#ReverseProxy

如果你愿意,你可以混合使用Martini和标准的http库。

[编辑] 根据Martini文档的阅读,你可以像标准库一样添加原始的http处理程序,这意味着你确实可以这样做:https://github.com/codegangsta/martini#service-injection

英文:

Yes, it is possible in general with Go, I'm not that familiar with Martini specifically. The http response from calling the remote file returns a Reader interface, and your request handler has a Writer interface. This means you can read a stream of data, and write a stream of data. Making your responsibility only to manipulate what you want, and "patch" the forwarded stream to the request stream.

Go even has a ReverseProxy utility built into the standard library:

http://golang.org/pkg/net/http/httputil/#ReverseProxy

You can probably mix Martini and the standard http library if you want.

[EDIT] Reading the martini docs, you can add raw http handlers like the standrad library has, meaning you can indeed do that: https://github.com/codegangsta/martini#service-injection

huangapple
  • 本文由 发表于 2014年3月9日 22:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/22283505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定