12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package api
- import (
- openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
- sts20150401 "github.com/alibabacloud-go/sts-20150401/v2/client"
- util "github.com/alibabacloud-go/tea-utils/v2/service"
- "github.com/alibabacloud-go/tea/tea"
- "github.com/gin-gonic/gin"
- )
- func Upload(r *GinRouter) {
- r.POST("upload/policy", UploadPolicy)
- }
- const (
- STS_ADDR = "sts.cn-chengdu.aliyuncs.com"
- ROLE_ARN = "acs:ram::1187995103897985:role/ramoss"
- //自定义
- ROLE_SESSION_NAME = "baishuihu-admin"
- )
- func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *sts20150401.Client, _err error) {
- config := &openapi.Config{
- AccessKeyId: accessKeyId,
- AccessKeySecret: accessKeySecret,
- }
- // Endpoint 请参考 https://api.aliyun.com/product/Sts
- config.Endpoint = tea.String(STS_ADDR)
- _result = &sts20150401.Client{}
- _result, _err = sts20150401.NewClient(config)
- return _result, _err
- }
- func UploadPolicy(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- client, err := CreateClient(tea.String(apictx.Svc.Conf.Obs.AccessKeyId), tea.String(apictx.Svc.Conf.Obs.SecrateKey))
- if err != nil {
- return nil, err
- }
- assumeRoleRequest := &sts20150401.AssumeRoleRequest{
- RoleArn: tea.String(ROLE_ARN),
- RoleSessionName: tea.String(ROLE_SESSION_NAME),
- }
- runtime := &util.RuntimeOptions{}
- result, err := client.AssumeRoleWithOptions(assumeRoleRequest, runtime)
- return result, err
- }
|