安装七牛 golang sdk
go get -u github.com/qiniu/go-sdk/v7
go get github.com/qiniu/go-sdk/v7/storage@v7.9.5
参考七牛官方文档:
https://developer.qiniu.com/kodo/sdk/go
zone 的可选值
参考这里
https://github.com/qiniu/go-sdk/blob/master/storage/zone.go
上传七牛
前端看到的上传格式:
Content-Disposition: form-data; name="file"; filename="some_point.png"
Content-Type: image/png
Golang Gin 的读取操作参考:
https://github.com/gin-gonic/gin#upload-files
示例:
// get file
file, _ := c.FormFile("file")
reader, _ := file.Open()
// qiniu upload
err := formUploader.Put(context.Background(), &ret, upToken, key, reader, file.Size, nil)
题外话,我发现对这些类型概念非常模糊,从网上找了一个非常浅显易懂的解释:
multipart.FileHeader, io.Reader, []byte 的区别
首先 Reader 是什么?
a Reader is any type that implements the Read method.
即实现了 Read 方法的类型,都是 Reader。
If you open a file for reading, the object returned is an os.File, which is a Reader (it implements the Read method):
var r io.Reader
var err error
r, err = os.Open("file.txt")
You can also make a Reader from a normal string using strings.NewReader:
var r io.Reader r = strings.NewReader("Read will return these bytes")
The body data from an http.Request is a Reader:
var r io.Reader
r = request.Body
A bytes.Buffer is a Reader:
var r io.Reader
var buf bytes.Buffer
r = &buf
ioutil.ReadAll
ReadAll 是从 Reader 读取 Byte Array,
b, err := ioutil.ReadAll(r)
io.Copy
从 io.Reader 复制到 io.Writer
n, err := io.Copy(w, r)
微信关注我哦 👍
我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式