Files
open-im-server/internal/api/third/tencent_cloud_storage_credential.go
T

73 lines
2.1 KiB
Go
Raw Normal View History

2021-05-26 19:15:25 +08:00
package apiThird
import (
2022-01-17 18:00:25 +08:00
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config"
2021-11-25 14:12:52 +08:00
"Open_IM/pkg/common/constant"
2022-01-17 18:00:25 +08:00
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
2022-02-16 19:41:44 +08:00
"github.com/fatih/structs"
//"github.com/fatih/structs"
2021-05-26 19:15:25 +08:00
"github.com/gin-gonic/gin"
sts "github.com/tencentyun/qcloud-cos-sts-sdk/go"
"net/http"
"time"
)
func TencentCloudStorageCredential(c *gin.Context) {
2022-01-17 18:00:25 +08:00
req := api.TencentCloudStorageCredentialReq{}
if err := c.BindJSON(&req); err != nil {
log.NewError("0", "BindJSON failed ", err.Error())
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
2021-05-26 19:15:25 +08:00
return
}
2022-01-17 18:00:25 +08:00
ok, userID := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
if !ok {
log.NewError(req.OperationID, "GetUserIDFromToken false ", c.Request.Header.Get("token"))
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
2021-07-15 11:23:20 +08:00
return
}
2022-01-17 18:00:25 +08:00
log.NewInfo(req.OperationID, "TencentCloudStorageCredential args ", userID)
2021-07-15 11:23:20 +08:00
2021-05-26 19:15:25 +08:00
cli := sts.NewClient(
config.Config.Credential.Tencent.SecretID,
config.Config.Credential.Tencent.SecretKey,
nil,
)
opt := &sts.CredentialOptions{
DurationSeconds: int64(time.Hour.Seconds()),
Region: config.Config.Credential.Tencent.Region,
Policy: &sts.CredentialPolicy{
Statement: []sts.CredentialPolicyStatement{
{
Action: []string{
"name/cos:PostObject",
"name/cos:PutObject",
},
Effect: "allow",
Resource: []string{
"qcs::cos:" + config.Config.Credential.Tencent.Region + ":uid/" + config.Config.Credential.Tencent.AppID + ":" + config.Config.Credential.Tencent.Bucket + "/*",
},
},
},
},
}
res, err := cli.GetCredential(opt)
2022-01-17 18:00:25 +08:00
resp := api.TencentCloudStorageCredentialResp{}
2021-05-26 19:15:25 +08:00
if err != nil {
2022-01-17 18:00:25 +08:00
resp.ErrCode = constant.ErrTencentCredential.ErrCode
resp.ErrMsg = err.Error()
} else {
2022-02-16 19:41:44 +08:00
resp.CosData.Bucket = config.Config.Credential.Tencent.Bucket
resp.CosData.Region = config.Config.Credential.Tencent.Region
resp.CosData.CredentialResult = res
2021-05-26 19:15:25 +08:00
}
2022-02-16 19:41:44 +08:00
resp.Data = structs.Map(&resp.CosData)
2022-02-16 18:30:24 +08:00
log.NewInfo(req.OperationID, "TencentCloudStorageCredential return ", resp)
2022-02-16 19:41:44 +08:00
2022-01-17 18:00:25 +08:00
c.JSON(http.StatusOK, resp)
2021-05-26 19:15:25 +08:00
}