mirror of
				https://github.com/openimsdk/open-im-server.git
				synced 2025-11-04 19:32:17 +08:00 
			
		
		
		
	新增支持ali oss
This commit is contained in:
		
							parent
							
								
									6f56d19938
								
							
						
					
					
						commit
						c6e8c80007
					
				@ -59,13 +59,13 @@ func main() {
 | 
			
		||||
		groupRouterGroup.POST("/transfer_group", group.TransferGroupOwner)                          //1
 | 
			
		||||
		groupRouterGroup.POST("/get_recv_group_applicationList", group.GetRecvGroupApplicationList) //1
 | 
			
		||||
		groupRouterGroup.POST("/get_user_req_group_applicationList", group.GetUserReqGroupApplicationList)
 | 
			
		||||
		groupRouterGroup.POST("/get_groups_info", group.GetGroupsInfo)                              //1
 | 
			
		||||
		groupRouterGroup.POST("/kick_group", group.KickGroupMember)                                 //1
 | 
			
		||||
		groupRouterGroup.POST("/get_group_member_list", group.GetGroupMemberList)                   //no use
 | 
			
		||||
		groupRouterGroup.POST("/get_group_all_member_list", group.GetGroupAllMemberList)            //1
 | 
			
		||||
		groupRouterGroup.POST("/get_group_members_info", group.GetGroupMembersInfo)                 //1
 | 
			
		||||
		groupRouterGroup.POST("/invite_user_to_group", group.InviteUserToGroup)                     //1
 | 
			
		||||
		groupRouterGroup.POST("/get_joined_group_list", group.GetJoinedGroupList)                   //1
 | 
			
		||||
		groupRouterGroup.POST("/get_groups_info", group.GetGroupsInfo)                   //1
 | 
			
		||||
		groupRouterGroup.POST("/kick_group", group.KickGroupMember)                      //1
 | 
			
		||||
		groupRouterGroup.POST("/get_group_member_list", group.GetGroupMemberList)        //no use
 | 
			
		||||
		groupRouterGroup.POST("/get_group_all_member_list", group.GetGroupAllMemberList) //1
 | 
			
		||||
		groupRouterGroup.POST("/get_group_members_info", group.GetGroupMembersInfo)      //1
 | 
			
		||||
		groupRouterGroup.POST("/invite_user_to_group", group.InviteUserToGroup)          //1
 | 
			
		||||
		groupRouterGroup.POST("/get_joined_group_list", group.GetJoinedGroupList)        //1
 | 
			
		||||
	}
 | 
			
		||||
	//certificate
 | 
			
		||||
	authRouterGroup := r.Group("/auth")
 | 
			
		||||
@ -77,6 +77,7 @@ func main() {
 | 
			
		||||
	thirdGroup := r.Group("/third")
 | 
			
		||||
	{
 | 
			
		||||
		thirdGroup.POST("/tencent_cloud_storage_credential", apiThird.TencentCloudStorageCredential)
 | 
			
		||||
		thirdGroup.POST("/ali_oss_credential", apiThird.AliOSSCredential)
 | 
			
		||||
		thirdGroup.POST("/minio_storage_credential", apiThird.MinioStorageCredential)
 | 
			
		||||
	}
 | 
			
		||||
	//Message
 | 
			
		||||
 | 
			
		||||
@ -94,6 +94,16 @@ credential: #腾讯cos,发送图片、视频、文件时需要,请自行申
 | 
			
		||||
    endpoint: http://127.0.0.1:9000
 | 
			
		||||
    accessKeyID: user12345
 | 
			
		||||
    secretAccessKey: key12345
 | 
			
		||||
  ali: # ali oss
 | 
			
		||||
    regionID: "oss-cn-beijing"
 | 
			
		||||
    accessKeyID: ""
 | 
			
		||||
    accessKeySecret: ""
 | 
			
		||||
    stsEndpoint: "sts.cn-beijing.aliyun.com"
 | 
			
		||||
    ossEndpoint: "oss-cn-beijing.aliyuncs.com"
 | 
			
		||||
    bucket: "bucket1"
 | 
			
		||||
    finalHost: "http://bucket1.oss-cn-beijing.aliyuncs.com"
 | 
			
		||||
    stsDurationSeconds: 3600
 | 
			
		||||
    OssRoleArn: "acs:ram::xxx:role/xxx"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
rpcport: #rpc服务端口 默认即可
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@ -7,6 +7,7 @@ require (
 | 
			
		||||
	github.com/Shopify/toxiproxy v2.1.4+incompatible // indirect
 | 
			
		||||
	github.com/alibabacloud-go/darabonba-openapi v0.1.11
 | 
			
		||||
	github.com/alibabacloud-go/dysmsapi-20170525/v2 v2.0.8
 | 
			
		||||
	github.com/alibabacloud-go/sts-20150401 v1.1.0
 | 
			
		||||
	github.com/alibabacloud-go/tea v1.1.17
 | 
			
		||||
	github.com/antonfisher/nested-logrus-formatter v1.3.0
 | 
			
		||||
	github.com/bwmarrin/snowflake v0.3.0
 | 
			
		||||
@ -52,6 +53,7 @@ require (
 | 
			
		||||
	google.golang.org/protobuf v1.27.1
 | 
			
		||||
	gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
 | 
			
		||||
	gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
 | 
			
		||||
	gopkg.in/ini.v1 v1.66.2 // indirect
 | 
			
		||||
	gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
 | 
			
		||||
	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
 | 
			
		||||
	sigs.k8s.io/yaml v1.2.0 // indirect
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										7
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								go.sum
									
									
									
									
									
								
							@ -50,6 +50,7 @@ github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWso
 | 
			
		||||
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
 | 
			
		||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 | 
			
		||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
 | 
			
		||||
github.com/alibabacloud-go/darabonba-openapi v0.1.7/go.mod h1:6FV1Bt1AItYIlC2rVopPTumrRNtkfPBmrPVAZ8v2bLk=
 | 
			
		||||
github.com/alibabacloud-go/darabonba-openapi v0.1.11 h1:w59gtSA0s87p0U5NNG/N/PIHsRP3rtj7qCP9hx9+GL8=
 | 
			
		||||
github.com/alibabacloud-go/darabonba-openapi v0.1.11/go.mod h1:MPJMxv7HYrFm5m9uOZWkDYsAWyZztEgnBRfk9Fg0eIU=
 | 
			
		||||
github.com/alibabacloud-go/darabonba-string v1.0.0/go.mod h1:93cTfV3vuPhhEwGGpKKqhVW4jLe7tDpo3LUM0i0g6mA=
 | 
			
		||||
@ -59,8 +60,11 @@ github.com/alibabacloud-go/dysmsapi-20170525/v2 v2.0.8 h1:KXMiCg99Jx7B6V+DlRFbWw
 | 
			
		||||
github.com/alibabacloud-go/dysmsapi-20170525/v2 v2.0.8/go.mod h1:8aL6tSyQIWJygF7W/Vqxdf/QDbN2S+u57k36bEA8hD8=
 | 
			
		||||
github.com/alibabacloud-go/endpoint-util v1.1.0 h1:r/4D3VSw888XGaeNpP994zDUaxdgTSHBbVfZlzf6b5Q=
 | 
			
		||||
github.com/alibabacloud-go/endpoint-util v1.1.0/go.mod h1:O5FuCALmCKs2Ff7JFJMudHs0I5EBgecXXxZRyswlEjE=
 | 
			
		||||
github.com/alibabacloud-go/openapi-util v0.0.8/go.mod h1:sQuElr4ywwFRlCCberQwKRFhRzIyG4QTP/P4y1CJ6Ws=
 | 
			
		||||
github.com/alibabacloud-go/openapi-util v0.0.9 h1:Z0DP4LFzkM/rW2nxOMiiLoQVZSeE3jVc5jrZ9Fd/UX0=
 | 
			
		||||
github.com/alibabacloud-go/openapi-util v0.0.9/go.mod h1:sQuElr4ywwFRlCCberQwKRFhRzIyG4QTP/P4y1CJ6Ws=
 | 
			
		||||
github.com/alibabacloud-go/sts-20150401 v1.1.0 h1:1yVyKz02ES6aKo3xVjmoPLBH1OAmmSqPkhKRdjEkmYs=
 | 
			
		||||
github.com/alibabacloud-go/sts-20150401 v1.1.0/go.mod h1:QW4O/c7Hp4krHYt+6xwnoG8EyZW3V9GYkl6EgIBmxJc=
 | 
			
		||||
github.com/alibabacloud-go/tea v1.1.0/go.mod h1:IkGyUSX4Ba1V+k4pCtJUc6jDpZLFph9QMy2VUPTwukg=
 | 
			
		||||
github.com/alibabacloud-go/tea v1.1.7/go.mod h1:/tmnEaQMyb4Ky1/5D+SE1BAsa5zj/KeGOFfwYm3N/p4=
 | 
			
		||||
github.com/alibabacloud-go/tea v1.1.8/go.mod h1:/tmnEaQMyb4Ky1/5D+SE1BAsa5zj/KeGOFfwYm3N/p4=
 | 
			
		||||
@ -904,8 +908,9 @@ gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AW
 | 
			
		||||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
 | 
			
		||||
gopkg.in/ini.v1 v1.56.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 | 
			
		||||
gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 | 
			
		||||
gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c=
 | 
			
		||||
gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 | 
			
		||||
gopkg.in/ini.v1 v1.66.2 h1:XfR1dOYubytKy4Shzc2LHrrGhU0lDCfDGG1yLPmpgsI=
 | 
			
		||||
gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 | 
			
		||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 h1:VpOs+IwYnYBaFnrNAeB8UUWtL3vEUnzSCL1nVjPhqrw=
 | 
			
		||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
 | 
			
		||||
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										95
									
								
								internal/api/third/ali_oss_credential.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								internal/api/third/ali_oss_credential.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,95 @@
 | 
			
		||||
package apiThird
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	api "Open_IM/pkg/base_info"
 | 
			
		||||
	"Open_IM/pkg/common/config"
 | 
			
		||||
	"Open_IM/pkg/common/constant"
 | 
			
		||||
	"Open_IM/pkg/common/log"
 | 
			
		||||
	"Open_IM/pkg/common/token_verify"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	openapi "github.com/alibabacloud-go/darabonba-openapi/client"
 | 
			
		||||
	sts20150401 "github.com/alibabacloud-go/sts-20150401/client"
 | 
			
		||||
	"github.com/alibabacloud-go/tea/tea"
 | 
			
		||||
	"github.com/fatih/structs"
 | 
			
		||||
 | 
			
		||||
	//"github.com/fatih/structs"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var stsClient *sts20150401.Client
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 使用AK&SK初始化账号Client
 | 
			
		||||
 * @param accessKeyId
 | 
			
		||||
 * @param accessKeySecret
 | 
			
		||||
 * @return Client
 | 
			
		||||
 * @throws Exception
 | 
			
		||||
 */
 | 
			
		||||
func getStsClient() *sts20150401.Client {
 | 
			
		||||
	if stsClient != nil {
 | 
			
		||||
		return stsClient
 | 
			
		||||
	}
 | 
			
		||||
	conf := &openapi.Config{
 | 
			
		||||
		// 您的AccessKey ID
 | 
			
		||||
		AccessKeyId: tea.String(config.Config.Credential.Ali.AccessKeyID),
 | 
			
		||||
		// 您的AccessKey Secret
 | 
			
		||||
		AccessKeySecret: tea.String(config.Config.Credential.Ali.AccessKeySecret),
 | 
			
		||||
		// Endpoint
 | 
			
		||||
		Endpoint: tea.String(config.Config.Credential.Ali.StsEndpoint),
 | 
			
		||||
	}
 | 
			
		||||
	result, err := sts20150401.NewClient(conf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.NewError("", "alists client初始化失败 ", err)
 | 
			
		||||
	}
 | 
			
		||||
	stsClient = result
 | 
			
		||||
	return stsClient
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func AliOSSCredential(c *gin.Context) {
 | 
			
		||||
	req := api.OSSCredentialReq{}
 | 
			
		||||
	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()})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	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"})
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	log.NewInfo(req.OperationID, "AliOSSCredential args ", userID)
 | 
			
		||||
 | 
			
		||||
	stsResp, err := getStsClient().AssumeRole(&sts20150401.AssumeRoleRequest{
 | 
			
		||||
		DurationSeconds: tea.Int64(config.Config.Credential.Ali.StsDurationSeconds),
 | 
			
		||||
		Policy:          nil,
 | 
			
		||||
		RoleArn:         tea.String(config.Config.Credential.Ali.OssRoleArn),
 | 
			
		||||
		RoleSessionName: tea.String(fmt.Sprintf("%s-%d", userID, time.Now().Unix())),
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	resp := api.OSSCredentialResp{}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		resp.ErrCode = constant.ErrTencentCredential.ErrCode
 | 
			
		||||
		resp.ErrMsg = err.Error()
 | 
			
		||||
	} else {
 | 
			
		||||
		resp = api.OSSCredentialResp{
 | 
			
		||||
			CommResp: api.CommResp{},
 | 
			
		||||
			OssData: api.OSSCredentialRespData{
 | 
			
		||||
				Endpoint:        config.Config.Credential.Ali.OssEndpoint,
 | 
			
		||||
				AccessKeyId:     *stsResp.Body.Credentials.AccessKeyId,
 | 
			
		||||
				AccessKeySecret: *stsResp.Body.Credentials.AccessKeySecret,
 | 
			
		||||
				Token:           *stsResp.Body.Credentials.SecurityToken,
 | 
			
		||||
				Bucket:          config.Config.Credential.Ali.Bucket,
 | 
			
		||||
				FinalHost:       config.Config.Credential.Ali.FinalHost,
 | 
			
		||||
			},
 | 
			
		||||
			Data: nil,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resp.Data = structs.Map(&resp.OssData)
 | 
			
		||||
	log.NewInfo(req.OperationID, "AliOSSCredential return ", resp)
 | 
			
		||||
 | 
			
		||||
	c.JSON(http.StatusOK, resp)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										22
									
								
								pkg/base_info/oss_api_struct.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								pkg/base_info/oss_api_struct.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,22 @@
 | 
			
		||||
package base_info
 | 
			
		||||
 | 
			
		||||
type OSSCredentialReq struct {
 | 
			
		||||
	OperationID string `json:"operationID"`
 | 
			
		||||
	Filename    string `json:"filename"`
 | 
			
		||||
	FileType    string `json:"file_type"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type OSSCredentialRespData struct {
 | 
			
		||||
	Endpoint        string `json:"endpoint"`
 | 
			
		||||
	AccessKeyId     string `json:"access_key_id"`
 | 
			
		||||
	AccessKeySecret string `json:"access_key_secret"`
 | 
			
		||||
	Token           string `json:"token"`
 | 
			
		||||
	Bucket          string `json:"bucket"`
 | 
			
		||||
	FinalHost       string `json:"final_host"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type OSSCredentialResp struct {
 | 
			
		||||
	CommResp
 | 
			
		||||
	OssData OSSCredentialRespData  `json:"-"`
 | 
			
		||||
	Data    map[string]interface{} `json:"data"`
 | 
			
		||||
}
 | 
			
		||||
@ -45,6 +45,17 @@ type config struct {
 | 
			
		||||
			SecretID  string `yaml:"secretID"`
 | 
			
		||||
			SecretKey string `yaml:"secretKey"`
 | 
			
		||||
		}
 | 
			
		||||
		Ali struct {
 | 
			
		||||
			RegionID           string `yaml:"regionID"`
 | 
			
		||||
			AccessKeyID        string `yaml:"accessKeyID"`
 | 
			
		||||
			AccessKeySecret    string `yaml:"accessKeySecret"`
 | 
			
		||||
			StsEndpoint        string `yaml:"stsEndpoint"`
 | 
			
		||||
			OssEndpoint        string `yaml:"ossEndpoint"`
 | 
			
		||||
			Bucket             string `yaml:"bucket"`
 | 
			
		||||
			FinalHost          string `yaml:"finalHost"`
 | 
			
		||||
			StsDurationSeconds int64  `yaml:"stsDurationSeconds"`
 | 
			
		||||
			OssRoleArn         string `yaml:"OssRoleArn"`
 | 
			
		||||
		}
 | 
			
		||||
		Minio struct {
 | 
			
		||||
			Bucket          string `yaml:"bucket"`
 | 
			
		||||
			Location        string `yaml:"location"`
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user