package utils import ( "context" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "sync" "time" ) const ( DefaultExpiredTime = 30 * time.Minute ) var ( once sync.Once minioClient *MinioOss ) type MinioOss struct { bucketName string ObjectName string minioClient *minio.Client ExpiredTime time.Duration } func (mo *MinioOss) oss() *minio.Client { return mo.minioClient } func (mo *MinioOss) GetObject(objectName string, filePath string) (err error) { err = minioClient.oss().FGetObject(context.Background(), mo.GetBucketName(), objectName, filePath, minio.GetObjectOptions{}) return } func (mo *MinioOss) SaveObject(objectName string, filePath string) (err error) { _, err = minioClient.oss().FPutObject(context.Background(), mo.GetBucketName(), objectName, filePath, minio.PutObjectOptions{}) return } func (mo *MinioOss) GetState(objectName string) (state minio.ObjectInfo, err error) { state, err = minioClient.oss().StatObject(context.Background(), mo.GetBucketName(), objectName, minio.GetObjectOptions{}) return } func (mo *MinioOss) GetObjectExt(contentType string) (ext string) { return getFileExtensionFromContentType(contentType) } func getFileExtensionFromContentType(contentType string) string { switch contentType { case "image/jpeg": return ".jpg" case "image/png": return ".png" case "application/pdf": return ".pdf" case "application/msword": return ".doc" case "application/vnd.openxmlformats-officedocument.wordprocessingml.document": return ".docx" case "application/vnd.ms-excel": return ".xls" case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": return ".xlsx" case "text/plain": return ".txt" case "application/json": return ".json" case "application/octet-stream": return ".bin" default: return "" } } func DefaultClient() *MinioOss { once.Do(func() { if minioClient == nil { // 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者 endpoint := MinIoEndpoint if MinIoBackEndpoint != "" { endpoint = MinIoBackEndpoint } accessKeyID := MinIoAccessKeyId secretAccessKey := MinIoAccessKeySecret useSSL := false if MinIoUseSSL == "true" { useSSL = true } client, err := minio.New(endpoint, &minio.Options{ Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), Secure: useSSL, }) if err != nil { FileLog.Error("初始化minio客户端失败: %s", err) } bucketName := MinIoBucketname exists, errBucketExists := client.BucketExists(context.Background(), bucketName) if errBucketExists != nil || !exists { FileLog.Error("BucketExists: %v; err: %v", exists, errBucketExists) } minioClient = &MinioOss{ minioClient: client, bucketName: bucketName, ExpiredTime: DefaultExpiredTime, } } }) return minioClient } func (mo *MinioOss) GetBucketName() string { return mo.bucketName } func (mo *MinioOss) GetExpiredTime() time.Duration { return mo.ExpiredTime } // 生成预签名的PUT URL func (mo *MinioOss) GeneratePreSignedPutURLHandler(objectName string) (url string, err error) { // 生成预签名的PUT URL PreSignedURL, err := minioClient.oss().PresignedPutObject(context.Background(), minioClient.GetBucketName(), objectName, minioClient.GetExpiredTime()) if err != nil { FileLog.Error("生成预签名的PUT URL失败: %s", err) return } url = PreSignedURL.String() return }