Skip to content

Commit 4b03d2c

Browse files
author
奇淼(piexlmax
authored
Merge pull request flipped-aurora#381 from MUHM/develop
为OSS增加腾讯云对象存储支持
2 parents a0c36e0 + 43fae5f commit 4b03d2c

File tree

6 files changed

+83
-1
lines changed

6 files changed

+83
-1
lines changed

server/config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ qiniu:
7878
secret-key: 'pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY'
7979
use-cdn-domains: false
8080

81+
# tencent cos configuration
82+
tencent-cos:
83+
bucket: 'xxxxx-10005608'
84+
region: 'ap-shanghai'
85+
secret-id: 'xxxxxxxx'
86+
secret-key: 'xxxxxxxx'
87+
base-url: 'https://gin.vue.admin'
88+
path-prefix: 'gin-vue-admin'
89+
8190
# excel configuration
8291
excel:
8392
dir: './resource/excel/'

server/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ type Server struct {
1313
// oss
1414
Local Local `mapstructure:"local" json:"local" yaml:"local"`
1515
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
16+
TencentCOS TencentCOS `mapstructure:"tencent-cos" json:"tencentCOS" yaml:"tencent-cos"`
1617
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
1718
}

server/config/oss.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,12 @@ type Qiniu struct {
1313
SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"`
1414
UseCdnDomains bool `mapstructure:"use-cdn-domains" json:"useCdnDomains" yaml:"use-cdn-domains"`
1515
}
16+
17+
type TencentCOS struct {
18+
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
19+
Region string `mapstructure:"region" json:"region" yaml:"region"`
20+
SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"`
21+
SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"`
22+
BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"`
23+
PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"`
24+
}

server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ require (
4646
github.com/swaggo/gin-swagger v1.2.0
4747
github.com/swaggo/swag v1.6.7
4848
github.com/tebeka/strftime v0.1.3 // indirect
49+
github.com/tencentyun/cos-go-sdk-v5 v0.7.19
4950
github.com/unrolled/secure v1.0.7
5051
go.uber.org/zap v1.10.0
5152
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect

server/utils/upload/tencent_cos.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package upload
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"gin-vue-admin/global"
8+
"mime/multipart"
9+
"net/http"
10+
"net/url"
11+
"time"
12+
13+
"github.com/tencentyun/cos-go-sdk-v5"
14+
"go.uber.org/zap"
15+
)
16+
17+
type TencentCOS struct{}
18+
19+
// UploadFile upload file to COS
20+
func (*TencentCOS) UploadFile(file *multipart.FileHeader) (string, string, error) {
21+
c := NewClient()
22+
f, openError := file.Open()
23+
if openError != nil {
24+
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
25+
26+
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
27+
}
28+
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
29+
30+
_, err := c.Object.Put(context.Background(), global.GVA_CONFIG.TencentCOS.PathPrefix+"/"+fileKey, f, nil)
31+
if err != nil {
32+
panic(err)
33+
}
34+
return global.GVA_CONFIG.TencentCOS.BaseURL + "/" + global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + fileKey, fileKey, nil
35+
}
36+
37+
// DeleteFile delete file form COS
38+
func (*TencentCOS) DeleteFile(key string) error {
39+
c := NewClient()
40+
name := global.GVA_CONFIG.TencentCOS.PathPrefix + "/" + key
41+
_, err := c.Object.Delete(context.Background(), name)
42+
if err != nil {
43+
global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error()))
44+
return errors.New("function bucketManager.Delete() Filed, err:" + err.Error())
45+
}
46+
return nil
47+
}
48+
49+
// NewClient init COS client
50+
func NewClient() *cos.Client {
51+
u, _ := url.Parse("https://" + global.GVA_CONFIG.TencentCOS.Bucket + ".cos." + global.GVA_CONFIG.TencentCOS.Region + ".myqcloud.com")
52+
b := &cos.BaseURL{BucketURL: u}
53+
c := cos.NewClient(b, &http.Client{
54+
Transport: &cos.AuthorizationTransport{
55+
SecretID: global.GVA_CONFIG.TencentCOS.SecretID,
56+
SecretKey: global.GVA_CONFIG.TencentCOS.SecretKey,
57+
},
58+
})
59+
return c
60+
}

server/utils/upload/upload.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ func NewOss() OSS {
2828
return &Local{}
2929
case "qiniu":
3030
return &Qiniu{}
31+
case "tencent-cos":
32+
return &TencentCOS{}
3133
default:
3234
return &Local{}
3335
}
34-
}
36+
}

0 commit comments

Comments
 (0)