golang - 上传大文件的功能
golang - which function for uploading large files
在 go 中有不同的方法来读取以多部分形式发送的文件
r.FormFile("file")
r.MultipartReader()
r.ParseMultipartForm(size)
我部分理解它们之间的区别,但是哪个最适合处理大文件 (~1GB)?
而且,哪个使用的内存更少?
https://golang.org/pkg/net/http/#Request.ParseMultipartForm
ParseMultipartForm parses a request body as multipart/form-data. The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files. ParseMultipartForm calls ParseForm if necessary. After one call to ParseMultipartForm, subsequent calls have no effect.
因此您可以通过调用 r.ParseMultipartForm(max)
来控制最大内存使用量,并且您可以使用 http.MaxBytesReader
.
来控制从请求中读取的总量
另请参阅此答案:
在 go 中有不同的方法来读取以多部分形式发送的文件
r.FormFile("file")
r.MultipartReader()
r.ParseMultipartForm(size)
我部分理解它们之间的区别,但是哪个最适合处理大文件 (~1GB)?
而且,哪个使用的内存更少?
https://golang.org/pkg/net/http/#Request.ParseMultipartForm
ParseMultipartForm parses a request body as multipart/form-data. The whole request body is parsed and up to a total of maxMemory bytes of its file parts are stored in memory, with the remainder stored on disk in temporary files. ParseMultipartForm calls ParseForm if necessary. After one call to ParseMultipartForm, subsequent calls have no effect.
因此您可以通过调用 r.ParseMultipartForm(max)
来控制最大内存使用量,并且您可以使用 http.MaxBytesReader
.
另请参阅此答案: