2025-12-11 11:26:50 +08:00
|
|
|
|
package minio
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"fmt"
|
2026-02-24 15:42:36 +08:00
|
|
|
|
"gitea.com/red-future/common/utils"
|
2025-12-30 16:17:57 +08:00
|
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
|
"net/http"
|
2025-12-11 11:26:50 +08:00
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
"github.com/minio/minio-go/v7"
|
|
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-30 16:17:57 +08:00
|
|
|
|
// IoConfig 映射 YAML 中的 minio 配置节点
|
|
|
|
|
|
type IoConfig struct {
|
2026-01-26 11:40:57 +08:00
|
|
|
|
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"` // 区域
|
2025-12-11 11:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 全局 MinIO 客户端(初始化一次,避免重复创建)
|
|
|
|
|
|
var minioClient *minio.Client
|
2025-12-30 16:17:57 +08:00
|
|
|
|
var minioCfg IoConfig
|
2025-12-11 11:26:50 +08:00
|
|
|
|
|
2025-12-30 16:17:57 +08:00
|
|
|
|
// initMinIO 初始化 MinIO 客户端。
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
if !g.Cfg().MustGet(ctx, "minio").IsEmpty() {
|
|
|
|
|
|
// 加载 MinIO 配置(可从配置文件/环境变量读取,这里硬编码示例)
|
|
|
|
|
|
minioCfg = IoConfig{
|
2026-01-26 11:52:28 +08:00
|
|
|
|
FilePrefix: g.Cfg().MustGet(ctx, "filePrefix").String(),
|
2026-01-26 11:40:57 +08:00
|
|
|
|
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(),
|
2025-12-30 16:17:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 创建 MinIO 客户端
|
|
|
|
|
|
var err error
|
|
|
|
|
|
if minioClient, err = minio.New(minioCfg.Endpoint, &minio.Options{
|
|
|
|
|
|
Creds: credentials.NewStaticV4(minioCfg.AccessKey, minioCfg.SecretKey, ""),
|
|
|
|
|
|
Secure: minioCfg.Secure,
|
|
|
|
|
|
Region: minioCfg.Region,
|
|
|
|
|
|
}); err != nil {
|
|
|
|
|
|
glog.Errorf(ctx, "初始化 MinIO 客户端失败: %v", err)
|
2025-12-11 11:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 16:17:57 +08:00
|
|
|
|
func UploadFile(ctx context.Context, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) {
|
|
|
|
|
|
return uploadFile(ctx, getBucketName(ctx), fileHeader)
|
2025-12-22 15:11:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-30 16:17:57 +08:00
|
|
|
|
func uploadFile(ctx context.Context, bucketName string, fileHeader *ghttp.UploadFile) (imagesUrl string, err error) {
|
|
|
|
|
|
// 检查/创建桶
|
|
|
|
|
|
exists, err := minioClient.BucketExists(ctx, bucketName)
|
2025-12-11 11:26:50 +08:00
|
|
|
|
if err != nil {
|
2025-12-30 16:17:57 +08:00
|
|
|
|
glog.Errorf(ctx, "检查桶是否存在失败: %v", err)
|
|
|
|
|
|
return
|
2025-12-11 11:26:50 +08:00
|
|
|
|
}
|
2025-12-30 16:17:57 +08:00
|
|
|
|
if !exists {
|
|
|
|
|
|
if err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{Region: minioCfg.Region}); err != nil {
|
|
|
|
|
|
glog.Errorf(ctx, "创建桶失败: %v", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
glog.Infof(ctx, "成功创建 MinIO 桶: %s", bucketName)
|
2025-12-22 15:11:07 +08:00
|
|
|
|
}
|
2025-12-11 11:26:50 +08:00
|
|
|
|
// 打开文件,获取 io.Reader(*os.File 实现了 io.Reader)
|
|
|
|
|
|
file, err := fileHeader.Open()
|
2025-12-30 16:17:57 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
glog.Errorf(ctx, "打开文件失败: %v", err)
|
|
|
|
|
|
return
|
2025-12-11 11:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
defer file.Close() // 必须关闭,避免文件句柄泄露
|
2025-12-30 16:17:57 +08:00
|
|
|
|
// 获取文件类型
|
|
|
|
|
|
buffer := make([]byte, 512)
|
|
|
|
|
|
_, err = file.Read(buffer)
|
2025-12-11 11:26:50 +08:00
|
|
|
|
if err != nil {
|
2025-12-30 16:17:57 +08:00
|
|
|
|
glog.Errorf(ctx, "读取文件头失败: %v", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
contentType := http.DetectContentType(buffer)
|
|
|
|
|
|
// 重置文件读取位置,否则后续 PutObject 会从第512字节开始上传
|
|
|
|
|
|
if _, err = file.Seek(0, 0); err != nil {
|
|
|
|
|
|
glog.Errorf(ctx, "重置文件读取位置失败: %v", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
// 生成唯一的 MinIO 对象名(避免覆盖)
|
|
|
|
|
|
fileExt := filepath.Ext(fileHeader.Filename) // 原文件后缀(如 .jpg)
|
|
|
|
|
|
uniqueID := uuid.New().String()[:32] // 32位随机UUID
|
|
|
|
|
|
timestamp := time.Now().Format("2006-01-02") // 日期目录(便于管理)
|
|
|
|
|
|
objectName := fmt.Sprintf("/%s/%s%s", timestamp, uniqueID, fileExt) // 存储路径:20251209/abc12345.jpg
|
|
|
|
|
|
// 设置存储桶公共读权限
|
|
|
|
|
|
policy := `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::` + bucketName + `/*"]}]}`
|
|
|
|
|
|
if err = minioClient.SetBucketPolicy(ctx, bucketName, policy); err != nil {
|
|
|
|
|
|
glog.Errorf(ctx, "设置存储桶权限失败: %v", err)
|
|
|
|
|
|
return
|
2025-12-11 11:26:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 执行图片上传
|
|
|
|
|
|
_, err = minioClient.PutObject(
|
2025-12-22 15:11:07 +08:00
|
|
|
|
ctx,
|
2025-12-30 16:17:57 +08:00
|
|
|
|
bucketName,
|
2025-12-11 11:26:50 +08:00
|
|
|
|
objectName,
|
|
|
|
|
|
file,
|
|
|
|
|
|
fileHeader.Size,
|
|
|
|
|
|
minio.PutObjectOptions{
|
2025-12-30 16:17:57 +08:00
|
|
|
|
ContentType: contentType, // 关键:指定图片MIME类型,S3会根据此类型处理
|
2025-12-11 11:26:50 +08:00
|
|
|
|
// 若需要图片可公开访问,添加如下配置(根据需求选择)
|
|
|
|
|
|
//ACL: minio.ACLPublicRead,
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
2025-12-30 16:17:57 +08:00
|
|
|
|
glog.Errorf(ctx, "上传图片失败: %v", err)
|
|
|
|
|
|
return
|
2025-12-11 11:26:50 +08:00
|
|
|
|
}
|
2025-12-30 16:17:57 +08:00
|
|
|
|
return objectName, err
|
2025-12-11 11:26:50 +08:00
|
|
|
|
}
|
2025-12-24 13:55:05 +08:00
|
|
|
|
|
2026-01-26 11:40:57 +08:00
|
|
|
|
// GetFileAddressPrefix 拼接图片前缀地址
|
|
|
|
|
|
func GetFileAddressPrefix(ctx context.Context) (imageUrl string) {
|
2025-12-24 13:55:05 +08:00
|
|
|
|
// 拼接图片前缀地址
|
|
|
|
|
|
var url = "http://"
|
2025-12-30 16:17:57 +08:00
|
|
|
|
if minioCfg.Secure {
|
2025-12-24 13:55:05 +08:00
|
|
|
|
url = "https://"
|
|
|
|
|
|
}
|
2026-01-26 11:40:57 +08:00
|
|
|
|
imgAddressPrefix := url + minioCfg.FilePrefix + "/" + getBucketName(ctx)
|
2025-12-24 13:55:05 +08:00
|
|
|
|
return imgAddressPrefix
|
|
|
|
|
|
}
|
2025-12-30 16:17:57 +08:00
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|