Qiniu.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package lib
  2. // 存储相关功能的引入包只有这两个,后面不再赘述
  3. // 存储相关功能的引入包只有这两个,后面不再赘述
  4. import (
  5. "bzd_server/conf"
  6. "bzd_server/logs"
  7. "bzd_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. // //BUCKET是你在存储空间的名称
  18. // accessKey = "-8ezB_d-8-eUFTMvhOGbGzgeQRPeKQnaQ3DBcUxo"
  19. // secretKey = "KFhkYxTAJ2ZPN3ZS3euTsfWk8-C92rKgkhAMkDRN"
  20. // BUCKET = "bzdcoldoss"
  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: 203, 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.Println(err)
  59. System.Add_Logs("七牛云", "上传文件失败"+localFile, err.Error())
  60. return false
  61. }
  62. logs.Println(ret.Bucket, ret.Key, ret.Fsize, ret.Hash, ret.Name)
  63. return true
  64. }
  65. func UploadToken(T_suffix string) string {
  66. Tokey := strconv.FormatInt(time.Now().Unix(), 10) + uuid.NewV4().String()
  67. if len(T_suffix) == 0{
  68. T_suffix = ".png"
  69. }
  70. putPolicy := storage.PutPolicy{
  71. Scope: conf.Qiniu_BUCKET,
  72. InsertOnly: 1, // 仅能以新增模式上传文件。
  73. Expires: 7200, //示例2小时有效期
  74. ReturnBody: `{"key":"` + conf.OssQiniu + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)"}`,
  75. //{"key":"github-x.png","hash":"FqKXVdTvIx_mPjOYdjDyUSy_H1jr","fsize":6091,"bucket":"if-pbl","name":"github logo"}
  76. //{"key":"` + conf.Oss + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}
  77. ForceSaveKey: true,
  78. SaveKey: "UpImage/" + Tokey + "." +T_suffix,
  79. //FsizeLimit: 1024 * 1024 * 1,
  80. MimeLimit: "image/*;application/pdf",
  81. }
  82. upToken := putPolicy.UploadToken(Qiniu)
  83. return upToken
  84. }