From 95bdd2419ec84c808dd3acb9123d1f336586bbe1 Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Mon, 29 Nov 2021 21:56:01 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E9=9B=86=E6=88=90=E5=8D=8E=E4=B8=BA?= =?UTF-8?q?=E4=BA=91obs=20=E5=AF=B9=E8=B1=A1=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.docker.yaml | 10 +++- server/config.yaml | 10 +++- server/config/config.go | 6 +- server/config/oss.go | 32 ----------- server/config/oss_aliyun.go | 10 ++++ server/config/oss_huawei.go | 9 +++ server/config/oss_local.go | 5 ++ server/config/oss_qiniu.go | 11 ++++ server/config/oss_tencent.go | 10 ++++ server/go.mod | 1 + server/go.sum | 2 + server/utils/upload/obs.go | 65 ++++++++++++++++++++++ server/utils/upload/upload.go | 20 +++---- web/src/view/systemTools/system/system.vue | 20 +++++++ 14 files changed, 161 insertions(+), 50 deletions(-) delete mode 100644 server/config/oss.go create mode 100644 server/config/oss_aliyun.go create mode 100644 server/config/oss_huawei.go create mode 100644 server/config/oss_local.go create mode 100644 server/config/oss_qiniu.go create mode 100644 server/config/oss_tencent.go create mode 100644 server/utils/upload/obs.go diff --git a/server/config.docker.yaml b/server/config.docker.yaml index 4ae96ec2..04b79b77 100644 --- a/server/config.docker.yaml +++ b/server/config.docker.yaml @@ -94,7 +94,6 @@ qiniu: secret-key: '' use-cdn-domains: false - # aliyun oss configuration aliyun-oss: endpoint: 'yourEndpoint' @@ -112,11 +111,18 @@ tencent-cos: base-url: 'https://gin.vue.admin' path-prefix: 'github.com/flipped-aurora/gin-vue-admin/server' +# huawei obs configuration +hua-wei-obs: + path: 'you-path' + bucket: 'you-bucket' + endpoint: 'you-endpoint' + access-key: 'you-access-key' + secret-key: 'you-secret-key' + # excel configuration excel: dir: './resource/excel/' - # timer task db clear table Timer: start: true diff --git a/server/config.yaml b/server/config.yaml index fa93b4bf..b94bdd06 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -115,7 +115,6 @@ qiniu: secret-key: '' use-cdn-domains: false - # aliyun oss configuration aliyun-oss: endpoint: 'yourEndpoint' @@ -134,11 +133,18 @@ tencent-cos: base-url: 'https://gin.vue.admin' path-prefix: 'github.com/flipped-aurora/gin-vue-admin/server' +# huawei obs configuration +hua-wei-obs: + path: 'you-path' + bucket: 'you-bucket' + endpoint: 'you-endpoint' + access-key: 'you-access-key' + secret-key: 'you-secret-key' + # excel configuration excel: dir: './resource/excel/' - # timer task db clear table Timer: start: true diff --git a/server/config/config.go b/server/config/config.go index b3267cce..cbf017cc 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -17,7 +17,9 @@ type Server struct { Local Local `mapstructure:"local" json:"local" yaml:"local"` Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"` AliyunOSS AliyunOSS `mapstructure:"aliyun-oss" json:"aliyunOSS" yaml:"aliyun-oss"` + HuaWeiObs HuaWeiObs `mapstructure:"hua-wei-obs" json:"huaWeiObs" yaml:"hua-wei-obs"` TencentCOS TencentCOS `mapstructure:"tencent-cos" json:"tencentCOS" yaml:"tencent-cos"` - Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"` - Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"` + + Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"` + Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"` } diff --git a/server/config/oss.go b/server/config/oss.go deleted file mode 100644 index da657586..00000000 --- a/server/config/oss.go +++ /dev/null @@ -1,32 +0,0 @@ -package config - -type Local struct { - Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件路径 -} - -type Qiniu struct { - Zone string `mapstructure:"zone" json:"zone" yaml:"zone"` // 存储区域 - Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` // 空间名称 - ImgPath string `mapstructure:"img-path" json:"imgPath" yaml:"img-path"` // CDN加速域名 - UseHTTPS bool `mapstructure:"use-https" json:"useHttps" yaml:"use-https"` // 是否使用https - AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"` // 秘钥AK - SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` // 秘钥SK - UseCdnDomains bool `mapstructure:"use-cdn-domains" json:"useCdnDomains" yaml:"use-cdn-domains"` // 上传是否使用CDN上传加速 -} - -type AliyunOSS struct { - Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"` - AccessKeyId string `mapstructure:"access-key-id" json:"accessKeyId" yaml:"access-key-id"` - AccessKeySecret string `mapstructure:"access-key-secret" json:"accessKeySecret" yaml:"access-key-secret"` - BucketName string `mapstructure:"bucket-name" json:"bucketName" yaml:"bucket-name"` - BucketUrl string `mapstructure:"bucket-url" json:"bucketUrl" yaml:"bucket-url"` - BasePath string `mapstructure:"base-path" json:"basePath" yaml:"base-path"` -} -type TencentCOS struct { - Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` - Region string `mapstructure:"region" json:"region" yaml:"region"` - SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"` - SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` - BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"` - PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"` -} diff --git a/server/config/oss_aliyun.go b/server/config/oss_aliyun.go new file mode 100644 index 00000000..519f89fc --- /dev/null +++ b/server/config/oss_aliyun.go @@ -0,0 +1,10 @@ +package config + +type AliyunOSS struct { + Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"` + AccessKeyId string `mapstructure:"access-key-id" json:"accessKeyId" yaml:"access-key-id"` + AccessKeySecret string `mapstructure:"access-key-secret" json:"accessKeySecret" yaml:"access-key-secret"` + BucketName string `mapstructure:"bucket-name" json:"bucketName" yaml:"bucket-name"` + BucketUrl string `mapstructure:"bucket-url" json:"bucketUrl" yaml:"bucket-url"` + BasePath string `mapstructure:"base-path" json:"basePath" yaml:"base-path"` +} diff --git a/server/config/oss_huawei.go b/server/config/oss_huawei.go new file mode 100644 index 00000000..9462c025 --- /dev/null +++ b/server/config/oss_huawei.go @@ -0,0 +1,9 @@ +package config + +type HuaWeiObs struct { + Path string `mapstructure:"path" json:"path" yaml:"path"` + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` + Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"` + AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"` + SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` +} diff --git a/server/config/oss_local.go b/server/config/oss_local.go new file mode 100644 index 00000000..5ff1f324 --- /dev/null +++ b/server/config/oss_local.go @@ -0,0 +1,5 @@ +package config + +type Local struct { + Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件路径 +} diff --git a/server/config/oss_qiniu.go b/server/config/oss_qiniu.go new file mode 100644 index 00000000..af554b78 --- /dev/null +++ b/server/config/oss_qiniu.go @@ -0,0 +1,11 @@ +package config + +type Qiniu struct { + Zone string `mapstructure:"zone" json:"zone" yaml:"zone"` // 存储区域 + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` // 空间名称 + ImgPath string `mapstructure:"img-path" json:"imgPath" yaml:"img-path"` // CDN加速域名 + UseHTTPS bool `mapstructure:"use-https" json:"useHttps" yaml:"use-https"` // 是否使用https + AccessKey string `mapstructure:"access-key" json:"accessKey" yaml:"access-key"` // 秘钥AK + SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` // 秘钥SK + UseCdnDomains bool `mapstructure:"use-cdn-domains" json:"useCdnDomains" yaml:"use-cdn-domains"` // 上传是否使用CDN上传加速 +} diff --git a/server/config/oss_tencent.go b/server/config/oss_tencent.go new file mode 100644 index 00000000..0a7e190b --- /dev/null +++ b/server/config/oss_tencent.go @@ -0,0 +1,10 @@ +package config + +type TencentCOS struct { + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` + Region string `mapstructure:"region" json:"region" yaml:"region"` + SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"` + SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"` + BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"` + PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"` +} diff --git a/server/go.mod b/server/go.mod index 5a52a65e..02feb483 100644 --- a/server/go.mod +++ b/server/go.mod @@ -16,6 +16,7 @@ require ( github.com/go-redis/redis/v8 v8.11.0 github.com/go-sql-driver/mysql v1.5.0 github.com/gookit/color v1.3.1 + github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 github.com/mojocn/base64Captcha v1.3.1 github.com/natefinch/lumberjack v2.0.0+incompatible diff --git a/server/go.sum b/server/go.sum index 7e9a9f43..00f68f1f 100644 --- a/server/go.sum +++ b/server/go.sum @@ -234,6 +234,8 @@ github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0m github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible h1:3kDd8PIWAdU+qGs/+0QUgsMI2ZSiJPt45Xn0su+x/Q0= +github.com/huaweicloud/huaweicloud-sdk-go-obs v3.21.8+incompatible/go.mod h1:l7VUhRbTKCzdOacdT4oWCwATKyvZqUOlOqr0Ous3k4s= github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= diff --git a/server/utils/upload/obs.go b/server/utils/upload/obs.go new file mode 100644 index 00000000..2b4efad2 --- /dev/null +++ b/server/utils/upload/obs.go @@ -0,0 +1,65 @@ +package upload + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs" + "github.com/pkg/errors" + "mime/multipart" +) + +var HuaWeiObs = new(_obs) + +type _obs struct{} + +func NewHuaWeiObsClient() (client *obs.ObsClient, err error) { + return obs.New(global.GVA_CONFIG.HuaWeiObs.AccessKey, global.GVA_CONFIG.HuaWeiObs.SecretKey, global.GVA_CONFIG.HuaWeiObs.Endpoint) +} + +func (o *_obs) UploadFile(file *multipart.FileHeader) (filename string, filepath string, err error) { + var open multipart.File + open, err = file.Open() + if err != nil { + return filename, filepath, err + } + filename = file.Filename + input := &obs.PutObjectInput{ + PutObjectBasicInput: obs.PutObjectBasicInput{ + ObjectOperationInput: obs.ObjectOperationInput{ + Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket, + Key: filename, + }, + ContentType: file.Header.Get("content-type"), + }, + Body: open, + } + + var client *obs.ObsClient + client, err = NewHuaWeiObsClient() + if err != nil { + return filepath, filename, errors.Wrap(err, "获取华为对象存储对象失败!") + } + + _, err = client.PutObject(input) + if err != nil { + return filepath, filename, errors.Wrap(err, "文件上传失败!") + } + filepath = global.GVA_CONFIG.HuaWeiObs.Path + "/" + filename + return filepath, filename, err +} + +func (o *_obs) DeleteFile(key string) error { + client, err := NewHuaWeiObsClient() + if err != nil { + return errors.Wrap(err, "获取华为对象存储对象失败!") + } + input := &obs.DeleteObjectInput{ + Bucket: global.GVA_CONFIG.HuaWeiObs.Bucket, + Key: key, + } + var output *obs.DeleteObjectOutput + output, err = client.DeleteObject(input) + if err != nil { + return errors.Wrapf(err, "删除对象(%s)失败!, output: %v", key, output) + } + return nil +} diff --git a/server/utils/upload/upload.go b/server/utils/upload/upload.go index d8015220..f36f5a0f 100644 --- a/server/utils/upload/upload.go +++ b/server/utils/upload/upload.go @@ -6,23 +6,17 @@ import ( "github.com/flipped-aurora/gin-vue-admin/server/global" ) -//@author: [ccfish86](https://github.com/ccfish86) -//@author: [SliverHorn](https://github.com/SliverHorn) -//@interface_name: OSS -//@description: OSS接口 - +// OSS 对象存储接口 +// Author [SliverHorn](https://github.com/SliverHorn) +// Author [ccfish86](https://github.com/ccfish86) type OSS interface { UploadFile(file *multipart.FileHeader) (string, string, error) DeleteFile(key string) error } -//@author: [ccfish86](https://github.com/ccfish86) -//@author: [SliverHorn](https://github.com/SliverHorn) -//@function: NewOss -//@description: OSS接口 -//@description: OSS的实例化方法 -//@return: OSS - +// NewOss OSS的实例化方法 +// Author [SliverHorn](https://github.com/SliverHorn) +// Author [ccfish86](https://github.com/ccfish86) func NewOss() OSS { switch global.GVA_CONFIG.System.OssType { case "local": @@ -33,6 +27,8 @@ func NewOss() OSS { return &TencentCOS{} case "aliyun-oss": return &AliyunOSS{} + case "huawei-obs": + return HuaWeiObs default: return &Local{} } diff --git a/web/src/view/systemTools/system/system.vue b/web/src/view/systemTools/system/system.vue index e11e482b..bd0e2e44 100644 --- a/web/src/view/systemTools/system/system.vue +++ b/web/src/view/systemTools/system/system.vue @@ -22,6 +22,7 @@ + @@ -258,6 +259,24 @@ + @@ -352,6 +371,7 @@ export default { qiniu: {}, tencentCOS: {}, aliyunOSS: {}, + huaWeiObs: {}, captcha: {}, zap: {}, local: {},