Qiniu.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package lib
  2. // 存储相关功能的引入包只有这两个,后面不再赘述
  3. // 存储相关功能的引入包只有这两个,后面不再赘述
  4. import (
  5. "ColdVerify_server/conf"
  6. "ColdVerify_server/logs"
  7. "ColdVerify_server/models/System"
  8. "context"
  9. "github.com/qiniu/go-sdk/v7/auth/qbox"
  10. "github.com/qiniu/go-sdk/v7/storage"
  11. uuid "github.com/satori/go.uuid"
  12. "strconv"
  13. "time"
  14. )
  15. var Qiniu *qbox.Mac
  16. // var (
  17. //
  18. // //BUCKET是你在存储空间的名称
  19. // accessKey = "-8ezB_d-8-eUFTMvhOGbGzgeQRPeKQnaQ3DBcUxo"
  20. // secretKey = "KFhkYxTAJ2ZPN3ZS3euTsfWk8-C92rKgkhAMkDRN"
  21. // BUCKET = "bzdcoldoss"
  22. //
  23. // )
  24. func init() {
  25. Qiniu = qbox.NewMac(conf.Qiniu_AccessKey, conf.Qiniu_SecretKey)
  26. }
  27. // if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx","ofile/"+timeStr+".xlsx"){
  28. // c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  29. // c.ServeJSON()
  30. // return
  31. // }
  32. func Pload_qiniu(localFile string, name string) bool {
  33. //localFile := "C:\\Users\\Administrator\\Downloads\\kodo-browser-Windows-x64-v1.0.15.zip"
  34. //key := "kodo-browser-Windows-x64-v1.0.15.zip"
  35. // 自定义返回值结构体
  36. type MyPutRet struct {
  37. Key string
  38. Hash string
  39. Fsize int
  40. Bucket string
  41. Name string
  42. }
  43. //key := "your file save key"
  44. // 使用 returnBody 自定义回复格式
  45. putPolicy := storage.PutPolicy{
  46. Scope: conf.Qiniu_BUCKET,
  47. ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
  48. }
  49. upToken := putPolicy.UploadToken(Qiniu)
  50. cfg := storage.Config{}
  51. formUploader := storage.NewFormUploader(&cfg)
  52. ret := MyPutRet{}
  53. putExtra := storage.PutExtra{
  54. Params: map[string]string{
  55. "x:name": "github logo",
  56. },
  57. }
  58. err := formUploader.PutFile(context.Background(), &ret, upToken, name, localFile, &putExtra)
  59. if err != nil {
  60. logs.Error(FuncName(), err)
  61. System.Add_Logs("七牛云", "上传文件失败"+localFile, err.Error())
  62. return false
  63. }
  64. logs.Println(ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name)
  65. return true
  66. }
  67. func UploadToken(T_suffix string) string {
  68. Tokey := strconv.FormatInt(time.Now().Unix(), 10) + uuid.NewV4().String()
  69. if len(T_suffix) == 0 {
  70. T_suffix = ".png"
  71. }
  72. putPolicy := storage.PutPolicy{
  73. Scope: conf.Qiniu_BUCKET,
  74. InsertOnly: 1, // 仅能以新增模式上传文件。
  75. Expires: 7200, //示例2小时有效期
  76. ReturnBody: `{"key":"` + conf.OssQiniu + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)"}`,
  77. //{"key":"github-x.png","hash":"FqKXVdTvIx_mPjOYdjDyUSy_H1jr","fsize":6091,"bucket":"if-pbl","name":"github logo"}
  78. //{"key":"` + conf.Oss + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}
  79. ForceSaveKey: true,
  80. SaveKey: "UpImage/" + Tokey + "." + T_suffix,
  81. FsizeLimit: 1024 * 1024 * 30,
  82. MimeLimit: "image/*;application/pdf",
  83. }
  84. upToken := putPolicy.UploadToken(Qiniu)
  85. return upToken
  86. }