英文:
Transfer a big file in golang
问题
客户端发送文件,大小可能超过5G,发送到从服务器,然后从服务器发送到主服务器。
从服务器会将临时文件保存在自己吗?我不希望这种情况发生,因为它会降低上传速度并浪费从服务器的内存。
有没有办法避免这种情况?在golang中传输大文件的最佳方式是什么?
英文:
Client send file, the size may be more than 5G, to slave server, and than slave send to master server.
Will the slave save temp file to itself? I do not want it happen because it will slow the upload speed and waste the slave's memory.
Any way to avoid this? And what is the best way to transfer a big file in golang?
答案1
得分: 7
是的,有一种标准的方法可以避免存储和转发的方式:一旦客户端连接到从服务器,后者应该打开到主服务器的连接,然后只需将数据从客户端流式传输到主服务器。通常使用io.Copy()
函数来实现这一点。由于Go语言出色的鸭子类型使用接口,这适用于TCP连接和HTTP请求/响应。
(要获得更好的解释,您需要缩小问题的范围。)
解决方案的一部分甚至在stackoverflow提供的类似问题中出现了——在这里。
英文:
Yes, there's a standard way to avoid store-and-forward approach: as soon as a client connects the slave server the latter should open a connection to the master server and then just stream the data from the client there. Typically this is done using the io.Copy()
function. Thanks to Go's excellent duck typing using interfaces, this works for TCP connections and HTTP requests/responses.
(To get better explanation(s) you have to narrow your question down.)
A part of the solution does even appear in the similar questions suggested by stackoverflow—here it is.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论