where is temp file created by http server

442 views
Skip to first unread message

hao dong

unread,
Jul 16, 2020, 6:44:00 AM7/16/20
to golang-nuts
As I have learned, http server will store user-upload file on disk when its size exceed 32MB.
I read the source code, and find out the large file to store in os.TempDir(), however, after uploading a large file, I can not find the file in the temp dir.
Cloud someone enlight me, where is the temp file ?

Jesper Louis Andersen

unread,
Jul 16, 2020, 7:32:06 AM7/16/20
to hao dong, golang-nuts
They will be removed once the request finishes. The way to access them is usually by the FormFile method on *http.Request or to create a multipart reader and break it out yourself.

The reason the latter is interesting is because it can avoid storing data you are not prepared to handle.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0232db86-9bfb-42ae-b24b-49ea57a222ado%40googlegroups.com.


--
J.

Lutz Horn

unread,
Jul 16, 2020, 7:52:28 AM7/16/20
to golang-nuts
See the documentation of `os.TempDir` at https://golang.org/pkg/os/#TempDir

> On Windows, it uses GetTempPath, returning the first non-empty value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory.

Lutz

________________________________________
Von: golan...@googlegroups.com <golan...@googlegroups.com> im Auftrag von hao dong <hao.a...@gmail.com>
Gesendet: Donnerstag, 16. Juli 2020 12:44
An: golang-nuts
Betreff: [go-nuts] where is temp file created by http server

As I have learned, http server will store user-upload file on disk when its size exceed 32MB.
I read the source code, and find out the large file to store in os.TempDir(), however, after uploading a large file, I can not find the file in the temp dir.
Cloud someone enlight me, where is the temp file ?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com<mailto:golang-nuts...@googlegroups.com>.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0232db86-9bfb-42ae-b24b-49ea57a222ado%40googlegroups.com<https://groups.google.com/d/msgid/golang-nuts/0232db86-9bfb-42ae-b24b-49ea57a222ado%40googlegroups.com?utm_medium=email&utm_source=footer>.

Maxim Via-Net

unread,
Jul 16, 2020, 12:47:25 PM7/16/20
to hao dong, golang-nuts
Hello, it depends on OS. You can show it in your logs, with something
like that: log.Infof("Directory for storing large files: %s",
os.TempDir())
В Чт, 16/07/2020 в 03:44 -0700, hao dong пишет:
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to golang-nuts...@googlegroups.com.
> .
>

hao dong

unread,
Jul 16, 2020, 8:59:11 PM7/16/20
to golang-nuts
thx. Can you help me find the link or doc which describe the detail of " removed once the request finished" ?


On Thursday, July 16, 2020 at 7:32:06 PM UTC+8, Jesper Louis Andersen wrote:
They will be removed once the request finishes. The way to access them is usually by the FormFile method on *http.Request or to create a multipart reader and break it out yourself.

The reason the latter is interesting is because it can avoid storing data you are not prepared to handle.

On Thu, Jul 16, 2020 at 12:44 PM hao dong <hao....@gmail.com> wrote:
As I have learned, http server will store user-upload file on disk when its size exceed 32MB.
I read the source code, and find out the large file to store in os.TempDir(), however, after uploading a large file, I can not find the file in the temp dir.
Cloud someone enlight me, where is the temp file ?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.


--
J.

Jonathan Reiter

unread,
Jul 16, 2020, 9:06:22 PM7/16/20
to hao dong, golang-nuts
See the multipart function definition. Line 86 deals with the actual temp file writes.


Do note that under certain conditions, a multipart form might hold on closing and deleting the multipart files in temp. Debugging async code and judicious use of RemoveAll can help with this.




To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/53d14db3-1c23-43f0-9613-1c7ac7645e96o%40googlegroups.com.

hao dong

unread,
Jul 16, 2020, 10:32:32 PM7/16/20
to golang-nuts
thanks. I've noticed the code,too. And that's what I'm confused: when there is a create, there is no delete, and I can not find the file under os.TemDir()
Seems I have to debug the code to find the removing line.

Jake Montgomery

unread,
Jul 17, 2020, 12:03:52 PM7/17/20
to golang-nuts
On Thursday, July 16, 2020 at 10:32:32 PM UTC-4, hao dong wrote:
thanks. I've noticed the code,too. And that's what I'm confused: when there is a create, there is no delete, and I can not find the file under os.TemDir()
Seems I have to debug the code to find the removing line.

I believe the delete happens at the end of finishRequest: https://golang.org/src/net/http/server.go#L1612
if w.req.MultipartForm != nil {
     w.req.MultipartForm.RemoveAll()
}

Not verified, but that looks like it.

hao dong

unread,
Jul 20, 2020, 2:23:33 AM7/20/20
to golang-nuts
thx. I'll check it.
Reply all
Reply to author
Forward
0 new messages