You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/api/third/tencent_cloud_storage_crede...

94 lines
2.7 KiB

4 years ago
package apiThird
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
log2 "Open_IM/pkg/common/log"
4 years ago
"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"`
}
var lastTime int64
var lastRes *sts.CredentialResult
4 years ago
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...")
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()
4 years ago
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{
"errCode": constant.ErrTencentCredential.ErrCode,
4 years ago
"errMsg": err.Error(),
3 years ago
"bucket": "",
"region": "",
4 years ago
"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)
lastRes = res
4 years ago
c.JSON(http.StatusOK, gin.H{
"errCode": 0,
"errMsg": "",
3 years ago
"region": config.Config.Credential.Tencent.Region,
"bucket": config.Config.Credential.Tencent.Bucket,
4 years ago
"data": res,
})
}