Skip to content

Commit 82950e7

Browse files
WqyJhpiexlmax
authored andcommitted
添加AWS S3上传功能
1 parent 8ce7bec commit 82950e7

File tree

7 files changed

+125
-1
lines changed

7 files changed

+125
-1
lines changed

server/config.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ tencent-cos:
153153
base-url: 'https://gin.vue.admin'
154154
path-prefix: 'github.com/flipped-aurora/gin-vue-admin/server'
155155

156+
# aws s3 configuration
157+
aws-s3:
158+
bucket: xxxxx-10005608
159+
region: ap-shanghai
160+
secret-id: xxxxxxxx
161+
secret-key: xxxxxxxx
162+
base-url: https://gin.vue.admin
163+
path-prefix: github.com/flipped-aurora/gin-vue-admin/server
164+
156165
# huawei obs configuration
157166
hua-wei-obs:
158167
path: 'you-path'

server/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Server struct {
2020
AliyunOSS AliyunOSS `mapstructure:"aliyun-oss" json:"aliyunOSS" yaml:"aliyun-oss"`
2121
HuaWeiObs HuaWeiObs `mapstructure:"hua-wei-obs" json:"huaWeiObs" yaml:"hua-wei-obs"`
2222
TencentCOS TencentCOS `mapstructure:"tencent-cos" json:"tencentCOS" yaml:"tencent-cos"`
23+
AwsS3 AwsS3 `mapstructure:"aws-s3" json:"awsS3" yaml:"aws-s3"`
2324

2425
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
2526
Timer Timer `mapstructure:"timer" json:"timer" yaml:"timer"`

server/config/oss_aws.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package config
2+
3+
type AwsS3 struct {
4+
Bucket string `mapstructure:"bucket" json:"bucket" yaml:"bucket"`
5+
Region string `mapstructure:"region" json:"region" yaml:"region"`
6+
SecretID string `mapstructure:"secret-id" json:"secretID" yaml:"secret-id"`
7+
SecretKey string `mapstructure:"secret-key" json:"secretKey" yaml:"secret-key"`
8+
BaseURL string `mapstructure:"base-url" json:"baseURL" yaml:"base-url"`
9+
PathPrefix string `mapstructure:"path-prefix" json:"pathPrefix" yaml:"path-prefix"`
10+
}

server/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.16
55
require (
66
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
77
github.com/aliyun/aliyun-oss-go-sdk v2.1.6+incompatible
8+
github.com/aws/aws-sdk-go v1.42.27
89
github.com/casbin/casbin/v2 v2.11.0
910
github.com/casbin/gorm-adapter/v3 v3.0.2
1011
github.com/dgrijalva/jwt-go v3.2.0+incompatible

server/go.sum

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ github.com/aliyun/aliyun-oss-go-sdk v2.1.6+incompatible/go.mod h1:T/Aws4fEfogEE9
3737
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
3838
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
3939
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
40+
github.com/aws/aws-sdk-go v1.42.27 h1:kxsBXQg3ee6LLbqjp5/oUeDgG7TENFrWYDmEVnd7spU=
41+
github.com/aws/aws-sdk-go v1.42.27/go.mod h1:OGr6lGMAKGlG9CVrYnWYDKIyb829c6EVBRjxqjmPepc=
4042
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA=
4143
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
4244
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
@@ -291,6 +293,10 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
291293
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
292294
github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
293295
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
296+
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
297+
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
298+
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
299+
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
294300
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
295301
github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 h1:pS0A6cr4aHYZnYwC7Uw+rwgb39+nzkm2QhwZ+S6Gn5I=
296302
github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
@@ -591,8 +597,9 @@ golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v
591597
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
592598
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
593599
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
594-
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 h1:4CSI6oo7cOjJKajidEljs9h+uP0rRZBPPPhcCbj5mw8=
595600
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
601+
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f h1:hEYJvxw1lSnWIl8X9ofsYMklzaDs90JI2az5YMd4fPM=
602+
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
596603
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
597604
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
598605
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=

server/utils/upload/aws_s3.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package upload
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"mime/multipart"
7+
"time"
8+
9+
"github.com/flipped-aurora/gin-vue-admin/server/global"
10+
11+
"github.com/aws/aws-sdk-go/aws"
12+
"github.com/aws/aws-sdk-go/aws/credentials"
13+
"github.com/aws/aws-sdk-go/aws/session"
14+
"github.com/aws/aws-sdk-go/service/s3"
15+
"github.com/aws/aws-sdk-go/service/s3/s3manager"
16+
"go.uber.org/zap"
17+
)
18+
19+
type AwsS3 struct{}
20+
21+
//@author: [WqyJh](https://github.com/WqyJh)
22+
//@object: *AwsS3
23+
//@function: UploadFile
24+
//@description: Upload file to Aws S3 using aws-sdk-go. See https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-basic-bucket-operations.html#s3-examples-bucket-ops-upload-file-to-bucket
25+
//@param: file *multipart.FileHeader
26+
//@return: string, string, error
27+
28+
func (*AwsS3) UploadFile(file *multipart.FileHeader) (string, string, error) {
29+
session := newSession()
30+
uploader := s3manager.NewUploader(session)
31+
32+
fileKey := fmt.Sprintf("%d%s", time.Now().Unix(), file.Filename)
33+
filename := global.GVA_CONFIG.AwsS3.PathPrefix + "/" + fileKey
34+
f, openError := file.Open()
35+
if openError != nil {
36+
global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
37+
return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
38+
}
39+
defer f.Close() // 创建文件 defer 关闭
40+
41+
_, err := uploader.Upload(&s3manager.UploadInput{
42+
Bucket: aws.String(global.GVA_CONFIG.AwsS3.Bucket),
43+
Key: aws.String(filename),
44+
Body: f,
45+
})
46+
if err != nil {
47+
global.GVA_LOG.Error("function uploader.Upload() Filed", zap.Any("err", err.Error()))
48+
return "", "", err
49+
}
50+
51+
return global.GVA_CONFIG.AwsS3.BaseURL + "/" + filename, fileKey, nil
52+
}
53+
54+
//@author: [WqyJh](https://github.com/WqyJh)
55+
//@object: *AwsS3
56+
//@function: DeleteFile
57+
//@description: Delete file from Aws S3 using aws-sdk-go. See https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/s3-example-basic-bucket-operations.html#s3-examples-bucket-ops-delete-bucket-item
58+
//@param: file *multipart.FileHeader
59+
//@return: string, string, error
60+
61+
func (*AwsS3) DeleteFile(key string) error {
62+
session := newSession()
63+
svc := s3.New(session)
64+
filename := global.GVA_CONFIG.AwsS3.PathPrefix + "/" + key
65+
bucket := global.GVA_CONFIG.AwsS3.Bucket
66+
67+
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
68+
Bucket: aws.String(bucket),
69+
Key: aws.String(filename),
70+
})
71+
if err != nil {
72+
global.GVA_LOG.Error("function svc.DeleteObject() Filed", zap.Any("err", err.Error()))
73+
return errors.New("function svc.DeleteObject() Filed, err:" + err.Error())
74+
}
75+
76+
_ = svc.WaitUntilObjectNotExists(&s3.HeadObjectInput{
77+
Bucket: aws.String(bucket),
78+
Key: aws.String(filename),
79+
})
80+
return nil
81+
}
82+
83+
// newSession Create S3 session
84+
func newSession() *session.Session {
85+
sess, _ := session.NewSession(&aws.Config{
86+
Region: aws.String(global.GVA_CONFIG.AwsS3.Region),
87+
Credentials: credentials.NewStaticCredentials(
88+
global.GVA_CONFIG.AwsS3.SecretID,
89+
global.GVA_CONFIG.AwsS3.SecretKey,
90+
"",
91+
),
92+
})
93+
return sess
94+
}

server/utils/upload/upload.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func NewOss() OSS {
2929
return &AliyunOSS{}
3030
case "huawei-obs":
3131
return HuaWeiObs
32+
case "aws-s3":
33+
return &AwsS3{}
3234
default:
3335
return &Local{}
3436
}

0 commit comments

Comments
 (0)