From 625ff317fcc471361f6192b70de94e110234b948 Mon Sep 17 00:00:00 2001 From: pixel <303176530@qq.com> Date: Sat, 27 Feb 2021 10:48:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=90=88=E5=B9=B6=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config/oss.go | 4 +--- server/utils/upload/tencent_cos.go | 17 ++++++++--------- server/utils/upload/upload.go | 2 ++ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/server/config/oss.go b/server/config/oss.go index 3b0a89a9..0a8af1cb 100644 --- a/server/config/oss.go +++ b/server/config/oss.go @@ -14,14 +14,13 @@ type Qiniu struct { UseCdnDomains bool `mapstructure:"use-cdn-domains" json:"useCdnDomains" yaml:"use-cdn-domains"` } - 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"` - +} type TencentCOS struct { Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"` Region string `mapstructure:"region" json:"region" yaml:"region"` @@ -29,5 +28,4 @@ type TencentCOS struct { 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/utils/upload/tencent_cos.go b/server/utils/upload/tencent_cos.go index 8b8afc41..26b25843 100644 --- a/server/utils/upload/tencent_cos.go +++ b/server/utils/upload/tencent_cos.go @@ -18,16 +18,15 @@ type TencentCOS struct{} // UploadFile upload file to COS func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error) { - c := NewClient() + client := NewClient() f, openError := file.Open() if openError != nil { global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error())) - return "", "", errors.New("function file.Open() Filed, err:" + openError.Error()) } fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename) - _, err := c.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil) + _, err := client.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil) if err != nil { panic(err) } @@ -36,9 +35,9 @@ func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error // DeleteFile delete file form COS func (*TencentCOS) DeleteFile(key string) error { - c := NewClient() + client := NewClient() name := global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + key - _, err := c.Object.Delete(context.Background(), name) + _, err := client.Object.Delete(context.Background(), name) if err != nil { global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error())) return errors.New("function bucketManager.Delete() Filed, err:" + err.Error()) @@ -48,13 +47,13 @@ func (*TencentCOS) DeleteFile(key string) error { // NewClient init COS client func NewClient() *cos.Client { - u, _ := url.Parse("https://" + global.GVA_CONFIG.TencentCOS.Bucket + ".cos." + global.GVA_CONFIG.TencentCOS.Region + ".myqcloud.com") - b := &cos.BaseURL{BucketURL: u} - c := cos.NewClient(b, &http.Client{ + urlStr, _ := url.Parse("https://" + global.GVA_CONFIG.TencentCOS.Bucket + ".cos." + global.GVA_CONFIG.TencentCOS.Region + ".myqcloud.com") + baseURL := &cos.BaseURL{BucketURL: urlStr} + client := cos.NewClient(baseURL, &http.Client{ Transport: &cos.AuthorizationTransport{ SecretID: global.GVA_CONFIG.TencentCOS.SecretID, SecretKey: global.GVA_CONFIG.TencentCOS.SecretKey, }, }) - return c + return client } diff --git a/server/utils/upload/upload.go b/server/utils/upload/upload.go index 9f4cd985..77cd42ce 100644 --- a/server/utils/upload/upload.go +++ b/server/utils/upload/upload.go @@ -30,6 +30,8 @@ func NewOss() OSS { return &Qiniu{} case "tencent-cos": return &TencentCOS{} + case "aliyun-cos": + return &AliyunOSS{} default: return &Local{} }