golang multipart上传文件到远端(如上传微信临时素材)


原文链接: golang multipart上传文件到远端(如上传微信临时素材)

golang multipart上传文件到远端(如上传微信临时素材)

最近在开发一个关注之后通过客服消息推送一张海报给用户的功能,海报图片是本地生成好的,需要上传到微信临时素材之后通过客服消息推送给用户。
上传文件需要multipart/form-data格式的表单,所以golang默认的http.POST方法是实现不了的。需要自行实现body参数逻辑。
上传请求初始化

// 新建上传请求
func NewUploadRequest(link string, params map[string]string, name, path string) (*http.Request, error) {
	fp, err := os.Open(path) // 打开文件句柄
	if err != nil {
		return nil, err
	}
	defer fp.Close()
	body := &bytes.Buffer{} // 初始化body参数
	writer := multipart.NewWriter(body) // 实例化multipart
	part, err := writer.CreateFormFile(name, filepath.Base(path)) // 创建multipart 文件字段
	if err != nil {
		return nil, err
	}
	_, err = io.Copy(part, fp) // 写入文件数据到multipart
	for key, val := range params {
		_ = writer.WriteField(key, val) // 写入body中额外参数,比如七牛上传时需要提供token
	}
	err = writer.Close()
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequest("POST", link, body) // 新建请求
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "multipart/form-data") // 设置请求头,!!!非常重要,否则远端无法识别请求
	return req, nil
}

// 上传流程

func (m *Task) upload(appid string) (string, error) {
	filename, err := m.download() // 下载远端海报文件到本地路径
	if err != nil {
		return "", err
	}
	// 获取accessToken
	accessToken, err := m.passport.GetAccessToken(appid)
	if err != nil {
		return "", err
	}
	params := &url.Values{
		"access_token": []string{accessToken},
		"type":         []string{"image"},
	}
	req, err := util.NewUploadRequest("https://api.weixin.qq.com/cgi-bin/media/upload?"+params.Encode(), nil, "media", filename) // 上传到微信
	if err != nil {
		return "", err
	}
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	ret := make(map[string]interface{})
	if err := json.NewDecoder(resp.Body).Decode(&ret); err != nil {
		return "", err
	}
	if mediaId, ok := ret["media_id"]; ok {
		return mediaId.(string), nil
	} else if errmsg, ok := ret["errmsg"]; ok {
		return "", errors.New(errmsg.(string))
	} else {
		return "", errors.New("上传失败")
	}
}

给了一个例子,利用mime/multipart来实现client如何上传一个文件到server,然后server如何接受这个文件。

看server.go代码

package main

import (
    "io"
    "os"
    "fmt"
    "io/ioutil"
    "net/http"
)

func uploadHandler(w http.ResponseWriter, r *http.Request) {
    reader, err := r.MultipartReader()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    for {
        part, err := reader.NextPart()
        if err == io.EOF {
            break
        }

        fmt.Printf("FileName=[%s], FormName=[%s]\n", part.FileName(), part.FormName())
        if part.FileName() == "" {  // this is FormData
            data, _ := ioutil.ReadAll(part)
            fmt.Printf("FormData=[%s]\n", string(data))
        } else {    // This is FileData
            dst, _ := os.Create("./" + part.FileName() + ".upload")
            defer dst.Close()
            io.Copy(dst, part)
        }
    }
}

func main() {
    http.HandleFunc("/upload", uploadHandler)
    http.ListenAndServe(":8080", nil)
}

例子1:client 上传一个文件

package main

import (
    "io"
    "os"
    "log"
    "bytes"
    "io/ioutil"
    "net/http"
    "mime/multipart"
)

func main() {
    bodyBuffer := &bytes.Buffer{}
    bodyWriter := multipart.NewWriter(bodyBuffer)

    fileWriter, _ := bodyWriter.CreateFormFile("files", "file.txt")

    file, _ := os.Open("file.txt")
    defer file.Close()

    io.Copy(fileWriter, file)

    contentType := bodyWriter.FormDataContentType()
    bodyWriter.Close()

    resp, _ := http.Post("http://localhost:8080/upload", contentType, bodyBuffer)
    defer resp.Body.Close()

    resp_body, _ := ioutil.ReadAll(resp.Body)

    log.Println(resp.Status)
    log.Println(string(resp_body))
}

例子2:client上传多个文件

package main

import (
    "io"
    "os"
    "log"
    "bytes"
    "io/ioutil"
    "net/http"
    "mime/multipart"
)

func main() {
    bodyBuffer := &bytes.Buffer{}
    bodyWriter := multipart.NewWriter(bodyBuffer)

    // file1
    fileWriter1, _ := bodyWriter.CreateFormFile("files", "file1.txt")
    file1, _ := os.Open("file1.txt")
    defer file1.Close()
    io.Copy(fileWriter1, file1)

    // file2
    fileWriter2, _ := bodyWriter.CreateFormFile("files", "file2.txt")
    file2, _ := os.Open("file2.txt")
    defer file2.Close()
    io.Copy(fileWriter2, file2)

    contentType := bodyWriter.FormDataContentType()
    bodyWriter.Close()

    resp, _ := http.Post("http://localhost:8080/upload", contentType, bodyBuffer)
    defer resp.Body.Close()

    resp_body, _ := ioutil.ReadAll(resp.Body)

    log.Println(resp.Status)
    log.Println(string(resp_body))
}

例子3:上传其他自定义的Form数据

package main

import (
    "io"
    "os"
    "log"
    "bytes"
    "io/ioutil"
    "net/http"
    "mime/multipart"
)

func main() {
    bodyBuffer := &bytes.Buffer{}
    bodyWriter := multipart.NewWriter(bodyBuffer)

    // file1
    fileWriter1, _ := bodyWriter.CreateFormFile("files", "file1.txt")
    file1, _ := os.Open("file1.txt")
    defer file1.Close()
    io.Copy(fileWriter1, file1)

    // file2
    fileWriter2, _ := bodyWriter.CreateFormFile("files", "file2.txt")
    file2, _ := os.Open("file2.txt")
    defer file2.Close()
    io.Copy(fileWriter2, file2)

    // other form data
    extraParams := map[string]string{
        "title":       "My Document",
        "author":      "Matt Aimonetti",
        "description": "A document with all the Go programming language secrets",
    }
    for key, value := range extraParams {
        _ = bodyWriter.WriteField(key, value)
    }

    contentType := bodyWriter.FormDataContentType()
    bodyWriter.Close()

    resp, _ := http.Post("http://localhost:8080/upload", contentType, bodyBuffer)
    defer resp.Body.Close()

    resp_body, _ := ioutil.ReadAll(resp.Body)

    log.Println(resp.Status)
    log.Println(string(resp_body))
}

看server端的运行输出:

$ go build server.go && ./server
FileName=[file1.txt], FormName=[files]
FileName=[file2.txt], FormName=[files]
FileName=[], FormName=[description]
FormData=[A document with all the Go programming language secrets]
FileName=[], FormName=[title]
FormData=[My Document]
FileName=[], FormName=[author]
FormData=[Matt Aimonetti]

$ ls -l
total 25180
-rwxr-xr-x 1 ... 6179399 Jun 22 08:07 client
-rw-r--r-- 1 ...    1952 Jun 22 08:06 client.go
-rw-r--r-- 1 ...      15 Jun 22 07:11 file1.txt
-rw-r--r-- 1 ...      15 Jun 22 08:28 file1.txt.upload
-rw-r--r-- 1 ...      14 Jun 22 07:11 file2.txt
-rw-r--r-- 1 ...      14 Jun 22 08:28 file2.txt.upload
-rw-r--r-- 1 ...      15 Jun 22 07:56 file.txt
-rw-r--r-- 1 ...      15 Jun 22 08:10 file.txt.upload
-rwxr-xr-x 1 ... 6717437 Jun 22 07:59 server
-rw-r--r-- 1 ...    1580 Jun 22 07:58 server.go
`