From f54a989bb800b2c49f1f397516e4095f346b43db Mon Sep 17 00:00:00 2001 From: fuyang Date: Mon, 29 Aug 2022 17:29:30 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(config):=20=E5=A2=9E=E5=8A=A0=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index 3e3bc0d..eda454a 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,8 @@ package config import ( - "github.com/cpyun/cpyun-admin-core/config/driver" "fmt" + "github.com/cpyun/cpyun-admin-core/config/driver" ) type Config struct { @@ -26,17 +26,15 @@ type Config struct { //Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` //DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"` //// oss - //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"` + Storage Storage `mapstructure:"storage" json:"mysql" yaml:"mysql"` + // //Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"` //Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"` // //// 跨域配置 //Cors CORS `mapstructure:"cors" json:"cors" yaml:"cors"` + Extend interface{} `yaml:"extend"` } // 多db改造 From 93229ec0102deb167a5abff2d5036f39b9552973 Mon Sep 17 00:00:00 2001 From: fuyang Date: Mon, 29 Aug 2022 18:04:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat(service):=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 2 +- config/storage.go | 24 ++++++++++++++++++++++++ sdk/filesystem/filesystem.go | 35 ++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 config/storage.go diff --git a/config/config.go b/config/config.go index eda454a..e88eb74 100644 --- a/config/config.go +++ b/config/config.go @@ -26,7 +26,7 @@ type Config struct { //Pgsql Pgsql `mapstructure:"pgsql" json:"pgsql" yaml:"pgsql"` //DBList []DB `mapstructure:"db-list" json:"db-list" yaml:"db-list"` //// oss - Storage Storage `mapstructure:"storage" json:"mysql" yaml:"mysql"` + Storage Storage `mapstructure:"storage" json:"mysql" yaml:"storage"` // //Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"` diff --git a/config/storage.go b/config/storage.go new file mode 100644 index 0000000..f039979 --- /dev/null +++ b/config/storage.go @@ -0,0 +1,24 @@ +package config + +type Storage struct { + FilesystemCloud string + Local interface{} + Minio Minio + Qiniu Qiniu + AliyunOSS AliyunOSS +} + +type Minio struct { + Endpoint string + AccessKeyID string `mapstructure:"access-key-id" json:"access-key-id" yaml:"access-key-id"` + SecretAccessKey string `mapstructure:"secret-access-key" json:"secret-access-key" yaml:"secret-access-key"` + Secure bool `mapstructure:"secure" json:"secure" yaml:"secure"` + Region string `mapstructure:"region" json:"region" yaml:"region"` + Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` +} + +type Qiniu struct { +} + +type AliyunOSS struct { +} diff --git a/sdk/filesystem/filesystem.go b/sdk/filesystem/filesystem.go index 9399a0b..40cf330 100644 --- a/sdk/filesystem/filesystem.go +++ b/sdk/filesystem/filesystem.go @@ -2,6 +2,7 @@ package filesystem import ( "context" + "github.com/cpyun/cpyun-admin-core/config" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "log" @@ -16,31 +17,39 @@ type FileSystem struct { } // -func New() FileSystem { +func New() *FileSystem { ctx := context.Background() + option := config.Settings.Storage.Minio - endpoint := "192.168.99.93:9090" - accessKeyID := "LaBY0JlcsRH4pac7uM17wxCB" - secretAccessKey := "neTSW#OrzWR7a^VrXvlp6QZ8bo!akWDMTy3b" - useSSL := false - bucketName := "notice" - - s3Client, err := minio.New(endpoint, &minio.Options{ - Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""), - Secure: useSSL, + s3Client, err := minio.New(option.Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(option.AccessKeyID, option.SecretAccessKey, ""), + Secure: option.Secure, }) if err != nil { - log.Fatalln(err) + //log.Fatalln(err) + log.Println(err) } - return FileSystem{ + return &FileSystem{ client: s3Client, ctx: ctx, - bucketName: bucketName, + bucketName: option.Bucket, } } +// 设置上下文 +func (f *FileSystem) SetContext(ctx context.Context) *FileSystem { + f.ctx = ctx + return f +} + +// 设置bucket +func (f *FileSystem) SetBucket(bucketName string) *FileSystem { + f.bucketName = bucketName + return f +} + // 保存文件 func (f FileSystem) PutFile(path string, file *multipart.FileHeader, rule string) (minio.UploadInfo, error) { name := generateHashName(rule) + filepath.Ext(file.Filename)