qiniu_job.go 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. fmt.Println("bucket", bucket)
  22. putPolicy := storage.PutPolicy{
  23. Scope: bucket,
  24. ReturnBody: `{"url":"$(key)","hash":"$(etag)","state":"SUCCESS"}`,
  25. }
  26. fmt.Println("putpolicy", putPolicy)
  27. putPolicy.Expires = 7200
  28. mac := qbox.NewMac(accessKey, secretKey)
  29. upToken := putPolicy.UploadToken(mac)
  30. fmt.Println("new qiniu token: ", upToken)
  31. redisClient := service.RedisClient()
  32. defer redisClient.Close()
  33. redisClient.Set("qn_token", upToken, 0)
  34. }
  35. func BeginRerequestQNTokenCronJob() {
  36. qnTokenCronJob.Start()
  37. }