123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- package NatsServer
- import (
- "Cold_Api/controllers/lib"
- "context"
- "fmt"
- "github.com/beego/beego/v2/core/logs"
- "github.com/qiniu/go-sdk/v7/auth/qbox"
- "github.com/qiniu/go-sdk/v7/storage"
- "github.com/vmihailenco/msgpack/v5"
- "strings"
- "time"
- )
- func Qiniu_UploadToken(T_suffix string, T_MimeLimit string) (string, bool) {
- type Qiniu_UploadToken struct {
- T_suffix string
- T_MimeLimit string
- }
- b, err := msgpack.Marshal(&Qiniu_UploadToken{
- T_suffix: T_suffix,
- T_MimeLimit: T_MimeLimit,
- })
- if err != nil {
- panic(err)
- }
- // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
- msg, err := lib.Nats.Request("Qiniu_UploadToken", b, 3*time.Second)
- if err != nil {
- logs.Error(err)
- } else {
- logs.Debug(fmt.Sprintf("Qiniu_UploadToken : %s\n", string(msg.Data)))
- return string(msg.Data), true
- }
- return string(msg.Data), false
- }
- //func Qiniu_UploadFile(localFile string, name string) (string, bool) {
- // logs.Info("Nats =>", lib.FuncName(), localFile, name)
- // type Qiniu_UploadFile struct {
- // File string
- // Name string
- // }
- // RR := Qiniu_UploadFile{File: localFile, Name: name}
- // println(":", RR.File)
- // b, err := msgpack.Marshal(&RR)
- // if err != nil {
- // panic(err)
- // }
- //
- // // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
- // msg, err := lib.Nats.Request("Qiniu_UploadFile", b, 3*time.Second)
- // if err != nil {
- // logs.Error(err)
- // } else {
- // logs.Debug(fmt.Sprintf("Qiniu_UploadFile : %s\n", string(msg.Data)))
- // return string(msg.Data), true
- // }
- //
- // return string(msg.Data), false
- //}
- func Qiniu_UploadFile(localFile string, name string) (string, bool) {
- logs.Info("Qiniu_UploadFile =>", localFile, name)
- // 获取 七牛云的 配置信息
- msg, err := lib.Nats.Request("Qiniu_Token", []byte(""), 3*time.Second)
- if err != nil {
- return string(msg.Data), false
- }
- logs.Debug(fmt.Sprintf("Qiniu_Token : %s\n", string(msg.Data)))
- Qiniu_cs := strings.Split(string(msg.Data), "|")
- Qiniu_AccessKey := Qiniu_cs[0]
- Qiniu_SecretKey := Qiniu_cs[1]
- Qiniu_BUCKET := Qiniu_cs[2]
- Qiniu_Url := Qiniu_cs[3]
- // 开始上传 七牛云
- Qiniu := qbox.NewMac(Qiniu_AccessKey, Qiniu_SecretKey)
- // 自定义返回值结构体
- type MyPutRet struct {
- Key string
- Hash string
- Fsize int
- Bucket string
- Name string
- }
- //key := "your file save key"
- // 使用 returnBody 自定义回复格式
- putPolicy := storage.PutPolicy{
- Scope: Qiniu_BUCKET,
- ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
- }
- //mac := qbox.NewMac(conf.Qiniu_AccessKey, conf.Qiniu_SecretKey)
- upToken := putPolicy.UploadToken(Qiniu)
- cfg := storage.Config{}
- formUploader := storage.NewFormUploader(&cfg)
- ret := MyPutRet{}
- putExtra := storage.PutExtra{
- Params: map[string]string{
- "x:name": "github logo",
- },
- }
- err = formUploader.PutFile(context.Background(), &ret, upToken, name, localFile, &putExtra)
- if err != nil {
- logs.Info("七牛云", "上传文件失败 "+localFile, err.Error())
- return "", false
- }
- logs.Debug(ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name)
- return Qiniu_Url + name, true
- }
|