upload.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package api
  2. import (
  3. openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
  4. sts20150401 "github.com/alibabacloud-go/sts-20150401/v2/client"
  5. util "github.com/alibabacloud-go/tea-utils/v2/service"
  6. "github.com/alibabacloud-go/tea/tea"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func Upload(r *GinRouter) {
  10. r.POST("upload/policy", UploadPolicy)
  11. }
  12. const (
  13. STS_ADDR = "sts.cn-chengdu.aliyuncs.com"
  14. ROLE_ARN = "acs:ram::1187995103897985:role/ramoss"
  15. //自定义
  16. ROLE_SESSION_NAME = "baishuihu-admin"
  17. )
  18. func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *sts20150401.Client, _err error) {
  19. config := &openapi.Config{
  20. AccessKeyId: accessKeyId,
  21. AccessKeySecret: accessKeySecret,
  22. }
  23. // Endpoint 请参考 https://api.aliyun.com/product/Sts
  24. config.Endpoint = tea.String(STS_ADDR)
  25. _result = &sts20150401.Client{}
  26. _result, _err = sts20150401.NewClient(config)
  27. return _result, _err
  28. }
  29. func UploadPolicy(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  30. client, err := CreateClient(tea.String(apictx.Svc.Conf.Obs.AccessKeyId), tea.String(apictx.Svc.Conf.Obs.SecrateKey))
  31. if err != nil {
  32. return nil, err
  33. }
  34. assumeRoleRequest := &sts20150401.AssumeRoleRequest{
  35. RoleArn: tea.String(ROLE_ARN),
  36. RoleSessionName: tea.String(ROLE_SESSION_NAME),
  37. }
  38. runtime := &util.RuntimeOptions{}
  39. result, err := client.AssumeRoleWithOptions(assumeRoleRequest, runtime)
  40. return result, err
  41. }