Qiniu.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package lib
  2. // 存储相关功能的引入包只有这两个,后面不再赘述
  3. // 存储相关功能的引入包只有这两个,后面不再赘述
  4. import (
  5. "Cold_GoodsOrder/conf"
  6. "Cold_GoodsOrder/logs"
  7. "context"
  8. "github.com/qiniu/go-sdk/v7/auth/qbox"
  9. "github.com/qiniu/go-sdk/v7/storage"
  10. uuid "github.com/satori/go.uuid"
  11. "strconv"
  12. "time"
  13. )
  14. var Qiniu *qbox.Mac
  15. // var (
  16. //
  17. // //BUCKET是你在存储空间的名称
  18. // accessKey = "-8ezB_d-8-eUFTMvhOGbGzgeQRPeKQnaQ3DBcUxo"
  19. // secretKey = "KFhkYxTAJ2ZPN3ZS3euTsfWk8-C92rKgkhAMkDRN"
  20. // BUCKET = "bzdcoldoss"
  21. //
  22. // )
  23. func init() {
  24. Qiniu = qbox.NewMac(conf.Qiniu_AccessKey, conf.Qiniu_SecretKey)
  25. }
  26. // if !lib.Pload_qiniu("ofile/"+timeStr+".xlsx","ofile/"+timeStr+".xlsx"){
  27. // c.Data["json"] = lib.JSONS{Code: 203, Msg: "oss!"}
  28. // c.ServeJSON()
  29. // return
  30. // }
  31. func Pload_qiniu(localFile string, name string) bool {
  32. //localFile := "C:\\Users\\Administrator\\Downloads\\kodo-browser-Windows-x64-v1.0.15.zip"
  33. //key := "kodo-browser-Windows-x64-v1.0.15.zip"
  34. // 自定义返回值结构体
  35. type MyPutRet struct {
  36. Key string
  37. Hash string
  38. Fsize int
  39. Bucket string
  40. Name string
  41. }
  42. //key := "your file save key"
  43. // 使用 returnBody 自定义回复格式
  44. putPolicy := storage.PutPolicy{
  45. Scope: conf.Qiniu_BUCKET,
  46. ReturnBody: `{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}`,
  47. }
  48. upToken := putPolicy.UploadToken(Qiniu)
  49. cfg := storage.Config{}
  50. formUploader := storage.NewFormUploader(&cfg)
  51. ret := MyPutRet{}
  52. putExtra := storage.PutExtra{
  53. Params: map[string]string{
  54. "x:name": "github logo",
  55. },
  56. }
  57. err := formUploader.PutFile(context.Background(), &ret, upToken, name, localFile, &putExtra)
  58. if err != nil {
  59. logs.Println("七牛云上传文件失败", localFile, err)
  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. }