UpFile.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package controllers
  2. import (
  3. "ERP_user/conf"
  4. "git.baozhida.cn/ERP_libs/lib"
  5. beego "github.com/beego/beego/v2/server/web"
  6. "github.com/qiniu/go-sdk/v7/auth/qbox"
  7. "github.com/qiniu/go-sdk/v7/storage"
  8. uuid "github.com/satori/go.uuid"
  9. "strconv"
  10. "time"
  11. )
  12. type UpFileController struct {
  13. beego.Controller
  14. }
  15. // 列表 -
  16. func (c *UpFileController) UpFileToken() {
  17. var Qiniu *qbox.Mac
  18. Qiniu = qbox.NewMac(conf.Qiniu_AccessKey, conf.Qiniu_SecretKey)
  19. T_suffix := c.GetString("T_suffix")
  20. Tokey := strconv.FormatInt(time.Now().Unix(), 10) + uuid.NewV4().String()
  21. if len(T_suffix) == 0 {
  22. T_suffix = "png"
  23. }
  24. putPolicy := storage.PutPolicy{
  25. Scope: conf.Qiniu_BUCKET,
  26. InsertOnly: 1, //仅能以新增模式上传文件。
  27. Expires: 7200, //示例2小时有效期
  28. ReturnBody: `{"key":"` + conf.OssQiniu + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)"}`,
  29. ForceSaveKey: true,
  30. SaveKey: "UpImage/" + Tokey + "." + T_suffix,
  31. MimeLimit: "image/*;application/pdf;application/octet-stream",
  32. }
  33. upToken := putPolicy.UploadToken(Qiniu)
  34. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: upToken}
  35. c.ServeJSON()
  36. return
  37. }