Skip to content

Commit ac8b8cd

Browse files
author
奇淼(piexlmax
authored
Merge pull request flipped-aurora#384 from lauyang/develop
增加阿里云OSS
2 parents 4b03d2c + 9fc2472 commit ac8b8cd

File tree

5 files changed

+98
-3
lines changed

5 files changed

+98
-3
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+
82+
# aliyun oss configuration
83+
aliyun-oss:
84+
endpoint: 'yourEndpoint'
85+
access-key-id: 'yourAccessKeyId'
86+
access-key-secret: 'yourAccessKeySecret'
87+
bucket-name: 'yourBucketName'
88+
bucket-url: 'yourBucketUrl'
89+
8190
# tencent cos configuration
8291
tencent-cos:
8392
bucket: 'xxxxx-10005608'

server/config/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ type Server struct {
99
System System `mapstructure:"system" json:"system" yaml:"system"`
1010
Captcha Captcha `mapstructure:"captcha" json:"captcha" yaml:"captcha"`
1111
// gorm
12-
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
12+
Mysql Mysql `mapstructure:"mysql" json:"mysql" yaml:"mysql"`
1313
// oss
14-
Local Local `mapstructure:"local" json:"local" yaml:"local"`
15-
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
14+
Local Local `mapstructure:"local" json:"local" yaml:"local"`
15+
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
16+
AliyunOSS AliyunOSS `mapstructure:"aliyun-oss" json:"aliyunOSS" yaml:"aliyun-oss"`
1617
TencentCOS TencentCOS `mapstructure:"tencent-cos" json:"tencentCOS" yaml:"tencent-cos"`
1718
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
1819
}

server/config/oss.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ type Qiniu struct {
1414
UseCdnDomains bool `mapstructure:"use-cdn-domains" json:"useCdnDomains" yaml:"use-cdn-domains"`
1515
}
1616

17+
18+
type AliyunOSS struct {
19+
Endpoint string `mapstructure:"endpoint" json:"endpoint" yaml:"endpoint"`
20+
AccessKeyId string `mapstructure:"access-key-id" json:"accessKeyId" yaml:"access-key-id"`
21+
AccessKeySecret string `mapstructure:"access-key-secret" json:"accessKeySecret" yaml:"access-key-secret"`
22+
BucketName string `mapstructure:"bucket-name" json:"bucketName" yaml:"bucket-name"`
23+
BucketUrl string `mapstructure:"bucket-url" json:"bucketUrl" yaml:"bucket-url"`
24+
1725
type TencentCOS struct {
1826
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
1927
Region string `mapstructure:"region" json:"region" yaml:"region"`
2028
SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"`
2129
SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"`
2230
BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"`
2331
PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"`
32+
2433
}

server/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ require (
66
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2
77
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
88
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
9+
github.com/aliyun/aliyun-oss-go-sdk v2.1.6+incompatible
10+
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
911
github.com/casbin/casbin v1.9.1
1012
github.com/casbin/casbin/v2 v2.11.0
1113
github.com/casbin/gorm-adapter/v3 v3.0.2

server/utils/upload/aliyun_oss.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package upload
2+
3+
import (
4+
"errors"
5+
"gin-vue-admin/global"
6+
"github.com/aliyun/aliyun-oss-go-sdk/oss"
7+
"go.uber.org/zap"
8+
"mime/multipart"
9+
"path/filepath"
10+
"time"
11+
)
12+
13+
type AliyunOSS struct{}
14+
15+
func (*AliyunOSS) UploadFile(file *multipart.FileHeader) (string, string, error) {
16+
bucket, err := NewBucket()
17+
if err != nil {
18+
global.GVA_LOG.Error("function AliyunOSS.NewBucket() Failed", zap.Any("err", err.Error()))
19+
return "", "", errors.New("function AliyunOSS.NewBucket() Failed, err:" + err.Error())
20+
}
21+
22+
// 读取本地文件。
23+
f, openError := file.Open()
24+
if openError != nil {
25+
global.GVA_LOG.Error("function file.Open() Failed", zap.Any("err", openError.Error()))
26+
return "", "", errors.New("function file.Open() Failed, err:" + openError.Error())
27+
}
28+
29+
//上传阿里云路径 文件名格式 自己可以改 建议保证唯一性
30+
yunFileTmpPath := filepath.Join("uploads", time.Now().Format("2006-01-02")) + "/" + file.Filename
31+
32+
// 上传文件流。
33+
err = bucket.PutObject(yunFileTmpPath, f)
34+
if err != nil {
35+
global.GVA_LOG.Error("function formUploader.Put() Failed", zap.Any("err", err.Error()))
36+
return "", "", errors.New("function formUploader.Put() Failed, err:" + err.Error())
37+
}
38+
39+
return global.GVA_CONFIG.AliyunOSS.BucketUrl + "/" + yunFileTmpPath, yunFileTmpPath, nil
40+
}
41+
42+
func (*AliyunOSS) DeleteFile(key string) error {
43+
bucket, err := NewBucket()
44+
if err != nil {
45+
global.GVA_LOG.Error("function AliyunOSS.NewBucket() Failed", zap.Any("err", err.Error()))
46+
return errors.New("function AliyunOSS.NewBucket() Failed, err:" + err.Error())
47+
}
48+
49+
// 删除单个文件。objectName表示删除OSS文件时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg。
50+
// 如需删除文件夹,请将objectName设置为对应的文件夹名称。如果文件夹非空,则需要将文件夹下的所有object删除后才能删除该文件夹。
51+
err = bucket.DeleteObject(key)
52+
if err != nil {
53+
global.GVA_LOG.Error("function bucketManager.Delete() Filed", zap.Any("err", err.Error()))
54+
return errors.New("function bucketManager.Delete() Filed, err:" + err.Error())
55+
}
56+
57+
return nil
58+
}
59+
60+
func NewBucket() (*oss.Bucket, error) {
61+
// 创建OSSClient实例。
62+
client, err := oss.New(global.GVA_CONFIG.AliyunOSS.Endpoint, global.GVA_CONFIG.AliyunOSS.AccessKeyId, global.GVA_CONFIG.AliyunOSS.AccessKeySecret)
63+
if err != nil {
64+
return nil, err
65+
}
66+
67+
// 获取存储空间。
68+
bucket, err := client.Bucket(global.GVA_CONFIG.AliyunOSS.BucketName)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
return bucket, nil
74+
}

0 commit comments

Comments
 (0)