NatsQiniu.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package NatsServer
  2. import (
  3. "ERP_user/Nats"
  4. "ERP_user/logs"
  5. "context"
  6. "fmt"
  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 := Nats.Nats.Request("Qiniu_UploadToken", b, 3*time.Second)
  27. if err != nil {
  28. fmt.Println(err)
  29. } else {
  30. fmt.Printf("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.Println("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. // fmt.Println(err)
  52. // } else {
  53. // fmt.Printf("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.Println("Qiniu_UploadFile =>", localFile, name)
  61. // 获取 七牛云的 配置信息
  62. msg, err := Nats.Nats.Request("Qiniu_Token", []byte(""), 3*time.Second)
  63. if err != nil {
  64. return string(msg.Data), false
  65. }
  66. fmt.Printf("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.Println("七牛云", "上传文件失败"+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. }