package NatsServer import ( "context" "github.com/qiniu/go-sdk/v7/auth/qbox" "github.com/qiniu/go-sdk/v7/storage" "github.com/vmihailenco/msgpack/v5" "strings" "time" ) func (m *NatsImpl) 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 := m.nats.Request("Qiniu_UploadToken", b, 3*time.Second) if err != nil { return string(msg.Data), false } return string(msg.Data), true } func (m *NatsImpl) Qiniu_UploadFile(localFile string, name string) (string, bool) { // 获取 七牛云的 配置信息 msg, err := m.nats.Request("Qiniu_Token", []byte(""), 3*time.Second) if err != nil { return string(msg.Data), false } 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 } putPolicy := storage.PutPolicy{ Scope: Qiniu_BUCKET, ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`, } 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 { return "", false } return Qiniu_Url + name, true }