添加 MinIO 文件前缀配置并优化文件地址拼接逻辑
This commit is contained in:
@@ -19,11 +19,12 @@ import (
|
|||||||
|
|
||||||
// IoConfig 映射 YAML 中的 minio 配置节点
|
// IoConfig 映射 YAML 中的 minio 配置节点
|
||||||
type IoConfig struct {
|
type IoConfig struct {
|
||||||
Endpoint string `yaml:"endpoint"` // MinIO API 地址
|
FilePrefix string `yaml:"filePrefix"` // 文件前缀
|
||||||
AccessKey string `yaml:"accessKey"` // AK
|
Endpoint string `yaml:"endpoint"` // MinIO API 地址
|
||||||
SecretKey string `yaml:"secretKey"` // SK
|
AccessKey string `yaml:"accessKey"` // AK
|
||||||
Secure bool `yaml:"secure"` // 是否启用 SSL
|
SecretKey string `yaml:"secretKey"` // SK
|
||||||
Region string `yaml:"region"` // 区域
|
Secure bool `yaml:"secure"` // 是否启用 SSL
|
||||||
|
Region string `yaml:"region"` // 区域
|
||||||
}
|
}
|
||||||
|
|
||||||
// 全局 MinIO 客户端(初始化一次,避免重复创建)
|
// 全局 MinIO 客户端(初始化一次,避免重复创建)
|
||||||
@@ -36,11 +37,12 @@ func init() {
|
|||||||
if !g.Cfg().MustGet(ctx, "minio").IsEmpty() {
|
if !g.Cfg().MustGet(ctx, "minio").IsEmpty() {
|
||||||
// 加载 MinIO 配置(可从配置文件/环境变量读取,这里硬编码示例)
|
// 加载 MinIO 配置(可从配置文件/环境变量读取,这里硬编码示例)
|
||||||
minioCfg = IoConfig{
|
minioCfg = IoConfig{
|
||||||
Endpoint: g.Cfg().MustGet(ctx, "minio.endpoint").String(),
|
FilePrefix: g.Cfg().MustGet(ctx, "filePrefix.endpoint").String(),
|
||||||
AccessKey: g.Cfg().MustGet(ctx, "minio.accessKey").String(),
|
Endpoint: g.Cfg().MustGet(ctx, "minio.endpoint").String(),
|
||||||
SecretKey: g.Cfg().MustGet(ctx, "minio.secretKey").String(),
|
AccessKey: g.Cfg().MustGet(ctx, "minio.accessKey").String(),
|
||||||
Secure: g.Cfg().MustGet(ctx, "minio.secure").Bool(),
|
SecretKey: g.Cfg().MustGet(ctx, "minio.secretKey").String(),
|
||||||
Region: g.Cfg().MustGet(ctx, "minio.region").String(),
|
Secure: g.Cfg().MustGet(ctx, "minio.secure").Bool(),
|
||||||
|
Region: g.Cfg().MustGet(ctx, "minio.region").String(),
|
||||||
}
|
}
|
||||||
// 创建 MinIO 客户端
|
// 创建 MinIO 客户端
|
||||||
var err error
|
var err error
|
||||||
@@ -123,14 +125,14 @@ func uploadFile(ctx context.Context, bucketName string, fileHeader *ghttp.Upload
|
|||||||
return objectName, err
|
return objectName, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetIFileAddressPrefix 拼接图片前缀地址
|
// GetFileAddressPrefix 拼接图片前缀地址
|
||||||
func GetIFileAddressPrefix(ctx context.Context) (imageUrl string) {
|
func GetFileAddressPrefix(ctx context.Context) (imageUrl string) {
|
||||||
// 拼接图片前缀地址
|
// 拼接图片前缀地址
|
||||||
var url = "http://"
|
var url = "http://"
|
||||||
if minioCfg.Secure {
|
if minioCfg.Secure {
|
||||||
url = "https://"
|
url = "https://"
|
||||||
}
|
}
|
||||||
imgAddressPrefix := url + minioCfg.Endpoint + "/" + getBucketName(ctx)
|
imgAddressPrefix := url + minioCfg.FilePrefix + "/" + getBucketName(ctx)
|
||||||
return imgAddressPrefix
|
return imgAddressPrefix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user