oss.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package auth
  2. import (
  3. "context"
  4. "gd_admin/apis"
  5. "gd_admin/common.in/storage"
  6. "gd_admin/common.in/utils"
  7. "gd_admin/errors"
  8. "gd_admin/thirdparty/oss"
  9. "fmt"
  10. "github.com/astaxie/beego/orm"
  11. "go.uber.org/zap"
  12. )
  13. func AuthGetOSSInfo(ctx context.Context, req *apis.AuthGetOSSInfoReq, reply *apis.AuthGetOSSInfoReply) (err error) {
  14. // 捕获各个task中的异常并返回给调用者
  15. defer func() {
  16. if r := recover(); r != nil {
  17. err = fmt.Errorf("%+v", r)
  18. }
  19. }()
  20. // 参数检查
  21. if req.Uid == 0 {
  22. l.Error("argument",
  23. zap.String("call", "check"),
  24. zap.String("fields", utils.MarshalJsonString(req)))
  25. return errors.ArgsError
  26. }
  27. // 检查用户是否合法
  28. checkUserBaseTask := func(db orm.Ormer) error {
  29. var admin_name string
  30. sql := fmt.Sprintf("select admin_name from t_gd_admin where id = %d ", req.Uid)
  31. if err = db.Raw(sql).QueryRow(&admin_name); err != nil {
  32. l.Error("mysql",
  33. zap.String("sql", sql),
  34. zap.String("error", err.Error()))
  35. if err == orm.ErrNoRows {
  36. return errors.AdminNotExist
  37. } else {
  38. return errors.DataBaseError
  39. }
  40. }
  41. return nil
  42. }
  43. // 更新用户基本信息状态
  44. getOSSInfoTask := func(db orm.Ormer) error {
  45. if resp, err := oss.AssumeRole(ctx); err != nil {
  46. l.Error("thirdparty",
  47. zap.String("call", "oss.AssumeRole"),
  48. zap.String("params", utils.MarshalJsonString(req)),
  49. zap.String("error", err.Error()))
  50. return errors.VendorError
  51. } else {
  52. reply.AccessKeyId = resp.Credentials.AccessKeyId
  53. reply.AccessKeySecret = resp.Credentials.AccessKeySecret
  54. reply.SecurityToken = resp.Credentials.SecurityToken
  55. reply.Expiration = resp.Credentials.Expiration.Unix()
  56. }
  57. return nil
  58. }
  59. // 这里使用事务处理
  60. tasks := []storage.DbaTasker{}
  61. tasks = append(tasks, storage.GenerateDbaTask(checkUserBaseTask))
  62. tasks = append(tasks, storage.GenerateDbaTask(getOSSInfoTask))
  63. storage.ExecTrans(tasks...)
  64. return nil
  65. }