commit
3ee12221a4
@ -0,0 +1,48 @@
|
||||
package apiThird
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/minio/minio-go/v7/pkg/policy"
|
||||
url2 "net/url"
|
||||
)
|
||||
|
||||
func init() {
|
||||
minioUrl, err := url2.Parse(config.Config.Credential.Minio.Endpoint)
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "parse failed, please check config/config.yaml", err.Error())
|
||||
return
|
||||
}
|
||||
minioClient, err := minio.New(minioUrl.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(config.Config.Credential.Minio.AccessKeyID, config.Config.Credential.Minio.SecretAccessKey, ""),
|
||||
Secure: false,
|
||||
})
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "init minio client failed", err.Error())
|
||||
return
|
||||
}
|
||||
opt := minio.MakeBucketOptions{
|
||||
Region: config.Config.Credential.Minio.Location,
|
||||
ObjectLocking: false,
|
||||
}
|
||||
err = minioClient.MakeBucket(context.Background(), config.Config.Credential.Minio.Bucket, opt)
|
||||
if err != nil {
|
||||
exists, err := minioClient.BucketExists(context.Background(), config.Config.Credential.Minio.Bucket)
|
||||
if err == nil && exists {
|
||||
log.NewInfo("", utils.GetSelfFuncName(), "We already own %s\n", config.Config.Credential.Minio.Bucket)
|
||||
} else {
|
||||
log.NewError("", utils.GetSelfFuncName(), "create bucket failed and bucket not exists", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
err = minioClient.SetBucketPolicy(context.Background(), config.Config.Credential.Minio.Bucket, policy.BucketPolicyReadWrite)
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "SetBucketPolicy failed please set in ", err.Error())
|
||||
return
|
||||
}
|
||||
log.NewInfo("", utils.GetSelfFuncName(), "minio create and set policy success")
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package apiThird
|
||||
|
||||
import (
|
||||
apiStruct "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"
|
||||
"Open_IM/pkg/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/minio/minio-go/v7"
|
||||
cr "github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func MinioStorageCredential(c *gin.Context) {
|
||||
var (
|
||||
req apiStruct.MinioStorageCredentialReq
|
||||
resp apiStruct.MiniostorageCredentialResp
|
||||
)
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
//ok, _ := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"))
|
||||
//if !ok {
|
||||
// log.NewError("", utils.GetSelfFuncName(), "GetUserIDFromToken false ", c.Request.Header.Get("token"))
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": "GetUserIDFromToken failed"})
|
||||
// return
|
||||
//}
|
||||
var stsOpts cr.STSAssumeRoleOptions
|
||||
stsOpts.AccessKey = config.Config.Credential.Minio.AccessKeyID
|
||||
stsOpts.SecretKey = config.Config.Credential.Minio.SecretAccessKey
|
||||
stsOpts.DurationSeconds = constant.MinioDurationTimes
|
||||
li, err := cr.NewSTSAssumeRole(config.Config.Credential.Minio.Endpoint, stsOpts)
|
||||
if err != nil {
|
||||
log.NewError("", utils.GetSelfFuncName(), "NewSTSAssumeRole failed", err.Error(), stsOpts, config.Config.Credential.Minio.Endpoint)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
v, err := li.Get()
|
||||
if err != nil {
|
||||
log.NewError("0", utils.GetSelfFuncName(), "li.Get error", err.Error())
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
log.NewError("0", utils.GetSelfFuncName(), err.Error())
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
resp.SessionToken = v.SessionToken
|
||||
resp.SecretAccessKey = v.SecretAccessKey
|
||||
resp.AccessKeyID = v.AccessKeyID
|
||||
resp.BucketName = config.Config.Credential.Minio.Bucket
|
||||
resp.StsEndpointURL = config.Config.Credential.Minio.Endpoint
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data":resp})
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package base_info
|
||||
|
||||
type MinioStorageCredentialReq struct {
|
||||
OperationID string `json:"operationID"`
|
||||
}
|
||||
|
||||
type MiniostorageCredentialResp struct {
|
||||
SecretAccessKey string `json:"secretAccessKey"`
|
||||
AccessKeyID string `json:"accessKeyID"`
|
||||
SessionToken string `json:"sessionToken"`
|
||||
BucketName string `json:"bucketName"`
|
||||
StsEndpointURL string `json:"stsEndpointURL"`
|
||||
}
|
Loading…
Reference in new issue