From 167186fa7807fb5e059c58486e39a90ff117ce67 Mon Sep 17 00:00:00 2001 From: pixelqm <303176530@qq.com> Date: Wed, 4 Sep 2019 19:28:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=B7=E5=85=A5=E6=97=A5=E5=BF=97=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QMPlusServer/controller/support/upload.go | 42 +++++++++++++++++++++++ QMPlusServer/go.mod | 3 ++ 2 files changed, 45 insertions(+) create mode 100644 QMPlusServer/controller/support/upload.go diff --git a/QMPlusServer/controller/support/upload.go b/QMPlusServer/controller/support/upload.go new file mode 100644 index 00000000..7c7a76f1 --- /dev/null +++ b/QMPlusServer/controller/support/upload.go @@ -0,0 +1,42 @@ +package support + +import ( + "bytes" + "github.com/qiniu/api.v7/auth/qbox" + "github.com/qiniu/api.v7/storage" + "mime/multipart" +) + +var accessKey string = "25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ" // 你在七牛云的accessKey +var secretKey string = "pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY" // 你在七牛云的secretKey +var bucket string = "a303176530" // 你七牛云标准空间的名字 +func Upload(file multipart.File) (err error, path string) { + + putPolicy := storage.PutPolicy{ + Scope: bucket, + } + mac := qbox.NewMac(accessKey, secretKey) + upToken := putPolicy.UploadToken(mac) + cfg := storage.Config{} + // 空间对应的机房 + cfg.Zone = &storage.ZoneHuadong + // 是否使用https域名 + cfg.UseHTTPS = false + // 上传是否使用CDN上传加速 + cfg.UseCdnDomains = false + formUploader := storage.NewFormUploader(&cfg) + ret := storage.PutRet{} + putExtra := storage.PutExtra{ + Params: map[string]string{ + "x:name": "github logo", + }, + } + data := []byte("hello, this is qiniu cloud") + dataLen := int64(len(data)) + err := formUploader.Put(context.Background(), &ret, upToken, key, bytes.NewReader(data), dataLen, &putExtra) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(ret.Key, ret.Hash) +} diff --git a/QMPlusServer/go.mod b/QMPlusServer/go.mod index d543a550..a40b0cda 100644 --- a/QMPlusServer/go.mod +++ b/QMPlusServer/go.mod @@ -14,6 +14,8 @@ require ( github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect github.com/pkg/errors v0.8.1 + github.com/qiniu/api.v7 v7.2.5+incompatible + github.com/qiniu/x v7.0.8+incompatible // indirect github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.2.0 @@ -21,4 +23,5 @@ require ( github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/swag v1.5.1 github.com/tebeka/strftime v0.1.3 // indirect + qiniupkg.com/x v7.0.8+incompatible // indirect )