Qiniu.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package lib
  2. // 存储相关功能的引入包只有这两个,后面不再赘述
  3. // 存储相关功能的引入包只有这两个,后面不再赘述
  4. import (
  5. "Yunlot/conf"
  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. var Qiniu *qbox.Mac
  13. func init() {
  14. Qiniu = qbox.NewMac(conf.Qiniu_AccessKey, conf.Qiniu_SecretKey)
  15. }
  16. func UploadToken(T_suffix string) string {
  17. Tokey := strconv.FormatInt(time.Now().Unix(), 10) + uuid.NewV4().String()
  18. if len(T_suffix) == 0 {
  19. T_suffix = ".png"
  20. }
  21. putPolicy := storage.PutPolicy{
  22. Scope: conf.Qiniu_BUCKET,
  23. InsertOnly: 1, // 仅能以新增模式上传文件。
  24. Expires: 7200, //示例2小时有效期
  25. ReturnBody: `{"key":"` + conf.Qiniu_Url + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)"}`,
  26. //{"key":"github-x.png","hash":"FqKXVdTvIx_mPjOYdjDyUSy_H1jr","fsize":6091,"bucket":"if-pbl","name":"github logo"}
  27. //{"key":"` + conf.Oss + `/$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}
  28. ForceSaveKey: true,
  29. SaveKey: conf.Qiniu_Dir + "/" + Tokey + "." + T_suffix,
  30. FsizeLimit: 1024 * 1024 * 10, // 10M
  31. //MimeLimit: "image/*",
  32. }
  33. upToken := putPolicy.UploadToken(Qiniu)
  34. return upToken
  35. }