如何避免处理非常大的文件

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

How to avoid very large files

问题

大家好,我需要你们的帮助,因为我在使用Golang的表单时遇到了一个问题。假设我在HTML表单中有这样一个字段:

<input type="file" name="file" id="file">

我想在Golang中接收这个文件,我可以使用以下代码来实现:

func index(w http.ResponseWriter, r *http.Request) {
    err := r.ParseMultipartForm(0)

    if err != nil {
      log.Print("Error")
    }

    file, _, _ := r.FormFile("file")

    log.Print(file)
}

到目前为止,一切都很好,我认为我已经得到了这个文件。但是现在我的问题是,在保存文件之前,我如何查看文件的大小?我的意思是,如果我理解正确的话,Go文档上说:

整个请求体被解析,最多maxMemory字节的文件部分存储在内存中,其余部分存储在临时文件中。

我的理解是(如果我理解错了,请告诉我),文件被保存在服务器上,也就是计算机上。问题是,如果我的计算机空间非常有限,一个非常大的文件可能会暂时占满我的计算机空间并导致服务器崩溃。

我该如何避免这个问题?我如何在实际保存文件之前查看文件的大小?或者,我如何在没有实际文件的情况下限制上传到服务器的文件大小?

希望我表达清楚了,我重申一遍,如果我理解错了什么,请告诉我。提前感谢:D。

英文:

Hello everyone I ask for your help since I have a question in golang using forms. Suppose I have this field in an html form:

&lt;input type=&quot;file&quot; name=&quot;file&quot; id=&quot;file&quot;&gt;

And I want to receive the file in golang, which I do with the following code:

func index(w http.ResponseWriter, r *http.Request) {
    err := r.ParseMultipartForm(0)

    if err != nil {
      log.Print(&quot;Error&quot;)
    }

    file, _, _ := r.FormFile(&quot;file&quot;)

    log.Print(file)
}

So far so good, and I think I really have the file. But now my question is, how can I see the weight of the file before having it saved? I mean, if I understand correctly the go documentation says:

> 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.

What I understand (please tell me if I'm wrong) is that the file is saved on the server, I mean on the computer, the problem is, if I have a very limited space on my computer, a very large file could not fill my computer space temporarily and crash the server?

How can I avoid this problem? How can I see the size of the file without actually having the file? Or, how can I limit the size of the file that is uploaded to the server without having the file?

I hope I have made myself understood, and I repeat, if I have misunderstood something please tell me. Thanks in advance :D.

答案1

得分: 4

你可以使用MaxBytesReader来避免接受过大的文件。

r.Body = http.MaxBytesReader(w, r.Body, MaxAllowedSize)
err := r.ParseMultipartForm(MaxAllowedSize)
if err != nil {
    fmt.Fprintf(w, "无法处理大于%dmb的文件,错误信息:%s", MaxAllowedSize/(1024*1024), err)
    return
}

示例处理程序

英文:

You can use MaxBytesReader and avoid accepting larger files.

r.Body = http.MaxBytesReader(w, r.Body, MaxAllowedSize)
err := r.ParseMultipartForm(MaxAllowedSize)
if err != nil {
		fmt.Fprintf(w, &quot;Can not handle files bigger than %dmb, failed with %s&quot;, MaxAllowedSize/(1024*1024), err)
		return
}

example handler

huangapple
  • 本文由 发表于 2022年8月20日 11:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/73423917.html
匿名

发表评论

匿名网友

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

确定