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

94 lines
2.7 KiB
Go
Raw Normal View History

2021-05-26 19:15:25 +08:00
package apiThird
import (
"Open_IM/pkg/common/config"
2021-11-25 14:12:52 +08:00
"Open_IM/pkg/common/constant"
log2 "Open_IM/pkg/common/log"
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"
)
type paramsTencentCloudStorageCredential struct {
Token string `json:"token"`
OperationID string `json:"operationID"`
}
2021-07-15 11:23:20 +08:00
var lastTime int64
var lastRes *sts.CredentialResult
2021-05-26 19:15:25 +08:00
func TencentCloudStorageCredential(c *gin.Context) {
params := paramsTencentCloudStorageCredential{}
if err := c.BindJSON(&params); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": "Parameter parsing errorplease check the parameters and request service again"})
return
}
log2.Info(params.Token, params.OperationID, "api TencentUpLoadCredential call start...")
2021-07-15 11:23:20 +08:00
if time.Now().Unix()-lastTime < 10 && lastRes != nil {
c.JSON(http.StatusOK, gin.H{
"errCode": 0,
"errMsg": "",
"region": config.Config.Credential.Tencent.Region,
"bucket": config.Config.Credential.Tencent.Bucket,
"data": lastRes,
})
return
}
lastTime = time.Now().Unix()
2021-05-26 19:15:25 +08:00
cli := sts.NewClient(
config.Config.Credential.Tencent.SecretID,
config.Config.Credential.Tencent.SecretKey,
nil,
)
log2.Info(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential sts.NewClient cli = %v", cli)
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 + "/*",
},
},
},
},
}
log2.Info(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential sts.CredentialOptions opt = %v", opt)
res, err := cli.GetCredential(opt)
if err != nil {
log2.Error(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential cli.GetCredential err = %s", err.Error())
c.JSON(http.StatusOK, gin.H{
2021-11-25 14:12:52 +08:00
"errCode": constant.ErrTencentCredential.ErrCode,
2021-05-26 19:15:25 +08:00
"errMsg": err.Error(),
2021-06-28 15:28:07 +08:00
"bucket": "",
"region": "",
2021-05-26 19:15:25 +08:00
"data": res,
})
return
}
log2.Info(c.Request.Header.Get("token"), c.PostForm("optionID"), "api TencentUpLoadCredential cli.GetCredential success res = %v, res.Credentials = %v", res, res.Credentials)
2021-07-15 11:23:20 +08:00
lastRes = res
2021-05-26 19:15:25 +08:00
c.JSON(http.StatusOK, gin.H{
"errCode": 0,
"errMsg": "",
2021-06-28 15:28:07 +08:00
"region": config.Config.Credential.Tencent.Region,
"bucket": config.Config.Credential.Tencent.Bucket,
2021-05-26 19:15:25 +08:00
"data": res,
})
}