package NatsServer import ( "ERP_user/Nats" "ERP_user/logs" "context" "fmt" "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 := Nats.Nats.Request("Qiniu_UploadToken", b, 3*time.Second) if err != nil { fmt.Println(err) } else { fmt.Printf("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.Println("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 { // fmt.Println(err) // } else { // fmt.Printf("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.Println("Qiniu_UploadFile =>", localFile, name) // 获取 七牛云的 配置信息 msg, err := Nats.Nats.Request("Qiniu_Token", []byte(""), 3*time.Second) if err != nil { return string(msg.Data), false } fmt.Printf("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.Println("七牛云", "上传文件失败"+localFile, err.Error()) return "", false } fmt.Println(ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name) return Qiniu_Url + name, true }