NatsQiniu.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package NatsServer
  2. import (
  3. "ColdVerify_server/lib"
  4. "context"
  5. "fmt"
  6. "github.com/beego/beego/v2/core/logs"
  7. "github.com/qiniu/go-sdk/v7/auth/qbox"
  8. "github.com/qiniu/go-sdk/v7/storage"
  9. "github.com/vmihailenco/msgpack/v5"
  10. "strings"
  11. "time"
  12. )
  13. func Qiniu_UploadToken(T_suffix string, T_MimeLimit string) (string, bool) {
  14. type Qiniu_UploadToken struct {
  15. T_suffix string
  16. T_MimeLimit string
  17. }
  18. b, err := msgpack.Marshal(&Qiniu_UploadToken{
  19. T_suffix: T_suffix,
  20. T_MimeLimit: T_MimeLimit,
  21. })
  22. if err != nil {
  23. panic(err)
  24. }
  25. // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  26. msg, err := lib.Nats.Request("Qiniu_UploadToken", b, 3*time.Second)
  27. if err != nil {
  28. logs.Error(err)
  29. } else {
  30. logs.Debug(fmt.Sprintf("Qiniu_UploadToken : %s\n", string(msg.Data)))
  31. return string(msg.Data), true
  32. }
  33. return string(msg.Data), false
  34. }
  35. //func Qiniu_UploadFile(localFile string, name string) (string, bool) {
  36. // logs.Info("Nats =>", lib.FuncName(), localFile, name)
  37. // type Qiniu_UploadFile struct {
  38. // File string
  39. // Name string
  40. // }
  41. // RR := Qiniu_UploadFile{File: localFile, Name: name}
  42. // println(":", RR.File)
  43. // b, err := msgpack.Marshal(&RR)
  44. // if err != nil {
  45. // panic(err)
  46. // }
  47. //
  48. // // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  49. // msg, err := lib.Nats.Request("Qiniu_UploadFile", b, 3*time.Second)
  50. // if err != nil {
  51. // logs.Error(err)
  52. // } else {
  53. // logs.Debug(fmt.Sprintf("Qiniu_UploadFile : %s\n", string(msg.Data)))
  54. // return string(msg.Data), true
  55. // }
  56. //
  57. // return string(msg.Data), false
  58. //}
  59. func Qiniu_UploadFile(localFile string, name string) (string, bool) {
  60. logs.Info("Qiniu_UploadFile =>", localFile, name)
  61. // 获取 七牛云的 配置信息
  62. msg, err := lib.Nats.Request("Qiniu_Token", []byte(""), 3*time.Second)
  63. if err != nil {
  64. return string(msg.Data), false
  65. }
  66. logs.Debug(fmt.Sprintf("Qiniu_Token : %s\n", string(msg.Data)))
  67. Qiniu_cs := strings.Split(string(msg.Data), "|")
  68. Qiniu_AccessKey := Qiniu_cs[0]
  69. Qiniu_SecretKey := Qiniu_cs[1]
  70. Qiniu_BUCKET := Qiniu_cs[2]
  71. Qiniu_Url := Qiniu_cs[3]
  72. // 开始上传 七牛云
  73. Qiniu := qbox.NewMac(Qiniu_AccessKey, Qiniu_SecretKey)
  74. // 自定义返回值结构体
  75. type MyPutRet struct {
  76. Key string
  77. Hash string
  78. Fsize int
  79. Bucket string
  80. Name string
  81. }
  82. //key := "your file save key"
  83. // 使用 returnBody 自定义回复格式
  84. putPolicy := storage.PutPolicy{
  85. Scope: Qiniu_BUCKET,
  86. ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
  87. }
  88. //mac := qbox.NewMac(conf.Qiniu_AccessKey, conf.Qiniu_SecretKey)
  89. upToken := putPolicy.UploadToken(Qiniu)
  90. cfg := storage.Config{}
  91. formUploader := storage.NewFormUploader(&cfg)
  92. ret := MyPutRet{}
  93. putExtra := storage.PutExtra{
  94. Params: map[string]string{
  95. "x:name": "github logo",
  96. },
  97. }
  98. err = formUploader.PutFile(context.Background(), &ret, upToken, name, localFile, &putExtra)
  99. if err != nil {
  100. logs.Info("七牛云", "上传文件失败 "+localFile, err.Error())
  101. return "", false
  102. }
  103. fmt.Println(ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name)
  104. return Qiniu_Url + name, true
  105. }