Qiniu.go 2.7 KB

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