scrm-go

qiniu_job.go 968B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package jobcron
  2. import (
  3. "SCRM/service"
  4. "fmt"
  5. "github.com/astaxie/beego"
  6. "github.com/qiniu/api.v7/auth/qbox"
  7. "github.com/qiniu/api.v7/storage"
  8. "github.com/robfig/cron"
  9. )
  10. var qnTokenCronJob *cron.Cron
  11. func init() {
  12. qnTokenCronJob = cron.New()
  13. qnTokenCronJob.AddFunc("@every 1h50m", func() {
  14. go RequestQNToken()
  15. })
  16. }
  17. func RequestQNToken() {
  18. accessKey := beego.AppConfig.String("qiniu_accesskey")
  19. secretKey := beego.AppConfig.String("qiniu_secretkey")
  20. bucket := beego.AppConfig.String("qiniu_bucket")
  21. putPolicy := storage.PutPolicy{
  22. Scope: bucket,
  23. ReturnBody: `{"url":"$(key)","hash":"$(etag)","state":"SUCCESS"}`,
  24. }
  25. putPolicy.Expires = 7200
  26. mac := qbox.NewMac(accessKey, secretKey)
  27. upToken := putPolicy.UploadToken(mac)
  28. fmt.Println("new qiniu token: ", upToken)
  29. redisClient := service.RedisClient()
  30. defer redisClient.Close()
  31. redisClient.Set("qn_token", upToken, 0)
  32. }
  33. func BeginRerequestQNTokenCronJob() {
  34. qnTokenCronJob.Start()
  35. }