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...

79 lines
2.3 KiB

4 years ago
package apiThird
import (
3 years ago
api "Open_IM/pkg/base_info"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
3 years ago
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
3 years ago
"github.com/fatih/structs"
//"github.com/fatih/structs"
4 years ago
"github.com/gin-gonic/gin"
sts "github.com/tencentyun/qcloud-cos-sts-sdk/go"
"net/http"
"time"
)
func TencentCloudStorageCredential(c *gin.Context) {
3 years ago
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()})
4 years ago
return
}
3 years ago
var ok bool
var userID string
var errInfo string
ok, userID, errInfo = token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
3 years ago
if !ok {
3 years ago
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
log.NewError(req.OperationID, errMsg)
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
return
}
3 years ago
3 years ago
log.NewInfo(req.OperationID, "TencentCloudStorageCredential args ", userID)
4 years ago
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)
3 years ago
resp := api.TencentCloudStorageCredentialResp{}
4 years ago
if err != nil {
3 years ago
resp.ErrCode = constant.ErrTencentCredential.ErrCode
resp.ErrMsg = err.Error()
} else {
3 years ago
resp.CosData.Bucket = config.Config.Credential.Tencent.Bucket
resp.CosData.Region = config.Config.Credential.Tencent.Region
resp.CosData.CredentialResult = res
4 years ago
}
3 years ago
resp.Data = structs.Map(&resp.CosData)
3 years ago
log.NewInfo(req.OperationID, "TencentCloudStorageCredential return ", resp)
3 years ago
3 years ago
c.JSON(http.StatusOK, resp)
4 years ago
}