Skip to content

Commit 9364b10

Browse files
committed
feat(upload): 增强上传文件配置与服务支持
- 在UploadFile结构体中新增Provider字段,支持选择上传服务商(qiniu、oss、s3)。 - 添加GetUploadProvider方法,根据配置返回当前有效的上传服务商。 - 更新上传服务的初始化逻辑,使用provider参数创建对应的上传服务实例。 - 兼容旧的NewUploadFile方法,确保向后兼容性。
1 parent 9c42cb2 commit 9364b10

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

config/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,25 @@ type RateLimitConfig struct {
117117
}
118118

119119
type UploadFile struct {
120+
// Provider 上传服务商: qiniu(七牛), oss(阿里云OSS), s3(亚马逊S3,预留)
121+
Provider string `mapstructure:"provider" json:"provider"`
120122
Accesskey string `mapstructure:"access-key" json:"accesskey"`
121123
Secretkey string `mapstructure:"secret-key" json:"secretkey"`
122124
Bucket string `mapstructure:"bucket" json:"bucket"`
123125
Endpoint string `mapstructure:"endpoint" json:"endpoint"`
124126
}
125127

128+
// GetUploadProvider 返回当前生效的上传服务商。若未配置 provider 则按 system.enable-oss 兼容:true=oss, false=qiniu
129+
func (u *UploadFile) GetUploadProvider(enableOss bool) string {
130+
if u != nil && u.Provider != "" {
131+
return u.Provider
132+
}
133+
if enableOss {
134+
return "oss"
135+
}
136+
return "qiniu"
137+
}
138+
126139
type JobsConfig struct {
127140
Enabled bool `mapstructure:"enabled" json:"enabled"`
128141
List map[string]JobConfig `mapstructure:"list" json:"list"`

config/config.tmp.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ system:
1818
cdn-domain: https://cdn.example.com/
1919
# 是否启动数据迁移
2020
enable-migrate: true
21-
# 文件上传默认使用oss
22-
enable-oss: false
21+
2322

2423
logs:
2524
# 日志等级(-1:Debug, 0:Info, 1:Warn, 2:Error, 3:DPanic, 4:Panic, 5:Fatal, -1<=level<=5, 参照zap.level源码)
@@ -84,11 +83,13 @@ rate-limit:
8483

8584
# 上传文件配置
8685
upload-file:
87-
# 访问密钥 (请填写您的OSS访问密钥)
86+
# 上传服务商: qiniu(七牛) / oss(阿里云OSS) / s3(亚马逊S3,预留)
87+
provider: qiniu
88+
# 访问密钥 (请填写您的OSS/七牛/S3访问密钥)
8889
access-key: your_access_key_here
89-
# 秘密密钥 (请填写您的OSS秘密密钥)
90+
# 秘密密钥 (请填写您的OSS/七牛/S3秘密密钥)
9091
secret-key: your_secret_key_here
91-
# 端点 (七牛云不用填写)
92+
# 端点 (七牛云可不填;OSS/S3 填写对应 endpoint)
9293
endpoint: your_endpoint_here
9394
# 存储桶名称
9495
bucket: your_bucket_name_here

internal/app/controller/resource_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,13 @@ func (pc ResourceController) UploadResources(c *gin.Context) {
166166
response.ValidationFail(c, "上传资源过大")
167167
return
168168
}
169-
upload, err := upload.NewUploadFile(
169+
provider := config.Conf.UploadFile.GetUploadProvider(config.Conf.System.EnableOss)
170+
upload, err := upload.NewService(
171+
provider,
170172
config.Conf.UploadFile.Endpoint,
171173
config.Conf.UploadFile.Accesskey,
172174
config.Conf.UploadFile.Secretkey,
173175
config.Conf.UploadFile.Bucket,
174-
config.Conf.System.EnableOss,
175176
)
176177
if err != nil {
177178
response.InternalServerError(c, "初始化上传服务失败:"+err.Error())

internal/app/repository/resource_repository.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ func (rr ResourceRepository) DeleteResourceByID(id string) error {
9797
return err
9898
}
9999
// 删除 cdn 文件
100-
upload, err := upload.NewUploadFile(
100+
provider := config.Conf.UploadFile.GetUploadProvider(config.Conf.System.EnableOss)
101+
upload, err := upload.NewService(
102+
provider,
101103
config.Conf.UploadFile.Endpoint,
102104
config.Conf.UploadFile.Accesskey,
103105
config.Conf.UploadFile.Secretkey,
104106
config.Conf.UploadFile.Bucket,
105-
config.Conf.System.EnableOss,
106107
)
107108
if err != nil {
108109
return err

pkg/util/upload/uploader.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package upload
22

33
import (
4+
"errors"
45
"mime/multipart"
56
"os"
7+
"strings"
68
)
79

810
// Uploader 定义上传接口
@@ -17,23 +19,43 @@ type UploadResource struct {
1719
Domain string
1820
}
1921

22+
// 支持的上传服务商
23+
const (
24+
ProviderQiniu = "qiniu"
25+
ProviderOSS = "oss"
26+
ProviderS3 = "s3"
27+
)
28+
2029
// Service 提供上传和删除文件的功能
2130
type Service struct {
2231
uploader Uploader
2332
}
2433

25-
// NewService 创建一个新的 Service 实例
26-
func NewUploadFile(endpoint, accessKeyId, accessKeySecret, bucketName string, enableOss bool) (*Service, error) {
34+
// NewService 根据 provider 创建对应的上传服务实例
35+
// provider: qiniu(七牛) / oss(阿里云OSS) / s3(亚马逊S3,预留,当前返回未实现错误)
36+
func NewService(provider, endpoint, accessKeyId, accessKeySecret, bucketName string) (*Service, error) {
37+
provider = strings.ToLower(strings.TrimSpace(provider))
2738
var uploader Uploader
28-
if enableOss {
39+
switch provider {
40+
case ProviderOSS:
2941
uploader = NewOSS(endpoint, accessKeyId, accessKeySecret, bucketName)
30-
} else {
42+
case ProviderQiniu:
3143
uploader = NewQiniu(accessKeyId, accessKeySecret, bucketName)
44+
case ProviderS3:
45+
return nil, errors.New("s3 上传尚未实现,请先使用 qiniu 或 oss")
46+
default:
47+
return nil, errors.New("不支持的上传服务商: " + provider + ",可选: qiniu, oss, s3")
3248
}
49+
return &Service{uploader: uploader}, nil
50+
}
3351

34-
return &Service{
35-
uploader: uploader,
36-
}, nil
52+
// NewUploadFile 兼容旧用法:根据 enableOss 选择 oss 或 qiniu。新代码请使用 NewService(provider, ...)
53+
func NewUploadFile(endpoint, accessKeyId, accessKeySecret, bucketName string, enableOss bool) (*Service, error) {
54+
provider := ProviderQiniu
55+
if enableOss {
56+
provider = ProviderOSS
57+
}
58+
return NewService(provider, endpoint, accessKeyId, accessKeySecret, bucketName)
3759
}
3860

3961
// UploadFile 公用上传文件方法

0 commit comments

Comments
 (0)