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/minio_init.go

59 lines
2.0 KiB

3 years ago
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"
url2 "net/url"
)
3 years ago
var (
minioClient *minio.Client
)
3 years ago
func MinioInit() {
3 years ago
operationID := utils.OperationIDGenerator()
log.NewInfo(operationID, utils.GetSelfFuncName())
3 years ago
minioUrl, err := url2.Parse(config.Config.Credential.Minio.Endpoint)
if err != nil {
3 years ago
log.NewError(operationID, utils.GetSelfFuncName(), "parse failed, please check config/config.yaml", err.Error())
3 years ago
return
}
3 years ago
minioClient, err = minio.New(minioUrl.Host, &minio.Options{
3 years ago
Creds: credentials.NewStaticV4(config.Config.Credential.Minio.AccessKeyID, config.Config.Credential.Minio.SecretAccessKey, ""),
Secure: false,
})
if err != nil {
3 years ago
log.NewError(operationID, utils.GetSelfFuncName(), "init minio client failed", err.Error())
3 years ago
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 {
3 years ago
log.NewInfo(operationID, utils.GetSelfFuncName(), err.Error())
3 years ago
exists, err := minioClient.BucketExists(context.Background(), config.Config.Credential.Minio.Bucket)
if err == nil && exists {
3 years ago
log.NewInfo(operationID, utils.GetSelfFuncName(), "We already own %s\n", config.Config.Credential.Minio.Bucket)
3 years ago
} else {
3 years ago
if err != nil {
3 years ago
log.NewError(operationID, utils.GetSelfFuncName(), err.Error())
3 years ago
}
3 years ago
log.NewError(operationID, utils.GetSelfFuncName(), "create bucket failed and bucket not exists")
3 years ago
return
}
}
3 years ago
// 自动化桶public的代码
//err = minioClient.SetBucketPolicy(context.Background(), config.Config.Credential.Minio.Bucket, policy.BucketPolicyReadWrite)
//if err != nil {
3 years ago
// log.NewError("", utils.GetSelfFuncName(), "SetBucketPolicy failed please set in web", err.Error())
// return
//}
3 years ago
log.NewInfo(operationID, utils.GetSelfFuncName(), "minio create and set policy success")
3 years ago
}