Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http,mime/multipart: Post big file by http client #28774

Closed
jliu666 opened this issue Nov 13, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@jliu666
Copy link

commented Nov 13, 2018

What version of Go are you using (go version)?

$ go version
go version go1.11.2 darwin/amd64
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/liujichun/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/liujichun/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/hm/fqwy8lhn6kv_zsq3qh08p5g00000gn/T/go-build968675952=/tmp/go-build -gno-record-gcc-switches -fno-common"

My test code:

package client

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"os"
	"testing"
	"time"
)

func TestDo(t *testing.T) {

	// 启动服务端
	server.StartHttp2()
	time.Sleep(time.Duration(4) * time.Second)

	// 创建临时文件
	tmpFile, _ := ioutil.TempFile("/tmp", "godfs_test_1.tmp")
	ioutil.WriteFile(tmpFile.Name(), []byte("abc"), 600)
	tmpFile.Close()

	for i := 0; i < 10; i++ {
		go func() {
			for i := 0; i < 10; i++ {
				bodyBuf := &bytes.Buffer{}
				bodyWriter := multipart.NewWriter(bodyBuf)

				fileWriter, _ := bodyWriter.CreateFormFile("file", tmpFile.Name())
				fh, _ := os.Open(tmpFile.Name())

				io.Copy(fileWriter, fh)
				contentType := bodyWriter.FormDataContentType()
				bodyWriter.Close()
				defer fh.Close()
				fmt.Println(contentType)
				url := "https://127.0.0.1:8090/file"
				rsp, _ := Http2Client.Post(url, contentType, bodyBuf)

				defer func() {
					rsp.Body.Close()
				}()
				body, _ := ioutil.ReadAll(rsp.Body)
				fmt.Println(string(body))
			}
		}()
		time.Sleep(time.Duration(1) * time.Second)
		if i == 4 {
			time.Sleep(time.Duration(100) * time.Second)
		}
	}
}

My problem is:
I want to post big file by http client. But the multipart will read all the file to memory. It looks not good.
What should to improve it? Maybe the multipart could support delayed read file.
Thanks!

@jliu666 jliu666 changed the title net/http,mime/multipart post big file by http client net/http,mime/multipart: Post big file by http client Nov 13, 2018

@mvdan

This comment has been minimized.

Copy link
Member

commented Nov 13, 2018

See https://golang.org/pkg/net/http/#Request.ParseMultipartForm and https://golang.org/pkg/net/http/#MaxBytesReader. I'm sure these have been covered in multiple blog posts and questions online.

In the future, see https://golang.org/wiki/Questions for asking questions.

@mvdan mvdan closed this Nov 13, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.