feat: 添加租户ID字段并重构文件上传逻辑
This commit is contained in:
@@ -11,7 +11,6 @@ import (
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
@@ -19,12 +18,11 @@ import (
|
||||
|
||||
// IoConfig 映射 YAML 中的 minio 配置节点
|
||||
type IoConfig struct {
|
||||
FilePrefix string `yaml:"filePrefix"` // 文件前缀
|
||||
Endpoint string `yaml:"endpoint"` // MinIO API 地址
|
||||
AccessKey string `yaml:"accessKey"` // AK
|
||||
SecretKey string `yaml:"secretKey"` // SK
|
||||
Secure bool `yaml:"secure"` // 是否启用 SSL
|
||||
Region string `yaml:"region"` // 区域
|
||||
Endpoint string `yaml:"endpoint"` // MinIO API 地址
|
||||
AccessKey string `yaml:"accessKey"` // AK
|
||||
SecretKey string `yaml:"secretKey"` // SK
|
||||
Secure bool `yaml:"secure"` // 是否启用 SSL
|
||||
Region string `yaml:"region"` // 区域
|
||||
}
|
||||
|
||||
// 全局 MinIO 客户端(初始化一次,避免重复创建)
|
||||
@@ -37,12 +35,11 @@ func init() {
|
||||
if !g.Cfg().MustGet(ctx, "minio").IsEmpty() {
|
||||
// 加载 MinIO 配置(可从配置文件/环境变量读取,这里硬编码示例)
|
||||
minioCfg = IoConfig{
|
||||
FilePrefix: g.Cfg().MustGet(ctx, "filePrefix").String(),
|
||||
Endpoint: g.Cfg().MustGet(ctx, "minio.endpoint").String(),
|
||||
AccessKey: g.Cfg().MustGet(ctx, "minio.accessKey").String(),
|
||||
SecretKey: g.Cfg().MustGet(ctx, "minio.secretKey").String(),
|
||||
Secure: g.Cfg().MustGet(ctx, "minio.secure").Bool(),
|
||||
Region: g.Cfg().MustGet(ctx, "minio.region").String(),
|
||||
Endpoint: g.Cfg().MustGet(ctx, "minio.endpoint").String(),
|
||||
AccessKey: g.Cfg().MustGet(ctx, "minio.accessKey").String(),
|
||||
SecretKey: g.Cfg().MustGet(ctx, "minio.secretKey").String(),
|
||||
Secure: g.Cfg().MustGet(ctx, "minio.secure").Bool(),
|
||||
Region: g.Cfg().MustGet(ctx, "minio.region").String(),
|
||||
}
|
||||
// 创建 MinIO 客户端
|
||||
var err error
|
||||
@@ -57,10 +54,15 @@ func init() {
|
||||
}
|
||||
|
||||
func UploadFile(ctx context.Context, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) {
|
||||
return uploadFile(ctx, getBucketName(ctx), fileHeader)
|
||||
return uploadFile(ctx, fileHeader)
|
||||
}
|
||||
|
||||
func uploadFile(ctx context.Context, bucketName string, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) {
|
||||
func uploadFile(ctx context.Context, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) {
|
||||
bucketName, err := utils.GetBucketName(ctx)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "获取桶名称失败: %v", err)
|
||||
return
|
||||
}
|
||||
// 检查/创建桶
|
||||
exists, err := minioClient.BucketExists(ctx, bucketName)
|
||||
if err != nil {
|
||||
@@ -124,23 +126,3 @@ func uploadFile(ctx context.Context, bucketName string, fileHeader *ghttp.Upload
|
||||
}
|
||||
return objectName, err
|
||||
}
|
||||
|
||||
// GetFileAddressPrefix 拼接图片前缀地址
|
||||
func GetFileAddressPrefix(ctx context.Context) (imageUrl string) {
|
||||
// 拼接图片前缀地址
|
||||
var url = "http://"
|
||||
if minioCfg.Secure {
|
||||
url = "https://"
|
||||
}
|
||||
imgAddressPrefix := url + minioCfg.FilePrefix + "/" + getBucketName(ctx)
|
||||
return imgAddressPrefix
|
||||
}
|
||||
|
||||
func getBucketName(ctx context.Context) (bucketName string) {
|
||||
user, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
glog.Errorf(ctx, "获取用户信息失败: %v", err)
|
||||
return
|
||||
}
|
||||
return "tenantid-" + gconv.String(user.TenantId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user