/
s3.go
165 lines (137 loc) · 4.42 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright 2020 KubeSphere Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package s3
import (
"fmt"
"io"
"math"
"time"
fakes3 "kubesphere.io/kubesphere/pkg/simple/client/s3/fake"
"code.cloudfoundry.org/bytefmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"k8s.io/klog/v2"
)
type Client struct {
s3Client *s3.S3
s3Session *session.Session
bucket string
}
const (
DefaultPartSize = 5 * bytefmt.MEGABYTE
// MinConcurrency is the minimum concurrency when uploading a part to Amazon S3,
// it's also the default value of Concurrency in aws-sdk-go.
MinConcurrency = 5
// MaxConcurrency is the maximum concurrency to limit the goroutines.
MaxConcurrency = 128
fakeS3Host = "FAKE"
)
// calculateConcurrency calculates the concurrency for better performance,
// make the concurrency in range [5, 128].
func calculateConcurrency(size int) int {
if size <= 0 {
return MinConcurrency
}
c := int(math.Ceil(float64(size) / float64(DefaultPartSize)))
if c < MinConcurrency {
return MinConcurrency
} else if c > MaxConcurrency {
return MaxConcurrency
}
return c
}
// Upload use Multipart upload to upload a single object as a set of parts.
// If the data length is known to be large, it is recommended to pass in the data length,
// it will helps to calculate concurrency. Otherwise, `size` can be 0,
// use 5 as default upload concurrency, same as aws-sdk-go.
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpuoverview.html for more details.
func (s *Client) Upload(key, fileName string, body io.Reader, size int) error {
uploader := s3manager.NewUploader(s.s3Session, func(uploader *s3manager.Uploader) {
uploader.PartSize = DefaultPartSize
uploader.LeavePartsOnError = true
uploader.Concurrency = calculateConcurrency(size)
})
_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
Body: body,
ContentDisposition: aws.String(fmt.Sprintf("attachment; filename=\"%s\"", fileName)),
})
return err
}
func (s *Client) Read(key string) ([]byte, error) {
downloader := s3manager.NewDownloader(s.s3Session)
writer := aws.NewWriteAtBuffer([]byte{})
_, err := downloader.Download(writer,
&s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
})
if err != nil {
return nil, err
}
return writer.Bytes(), nil
}
func (s *Client) GetDownloadURL(key string, fileName string) (string, error) {
req, _ := s.s3Client.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
ResponseContentDisposition: aws.String(fmt.Sprintf("attachment; filename=\"%s\"", fileName)),
})
return req.Presign(5 * time.Minute)
}
func (s *Client) Delete(key string) error {
_, err := s.s3Client.DeleteObject(
&s3.DeleteObjectInput{Bucket: aws.String(s.bucket),
Key: aws.String(key),
})
if err != nil {
return err
}
return nil
}
func NewS3Client(options *Options) (Interface, error) {
if options.Endpoint == fakeS3Host {
return fakes3.NewFakeS3(), nil
}
cred := credentials.NewStaticCredentials(options.AccessKeyID, options.SecretAccessKey, options.SessionToken)
config := aws.Config{
Region: aws.String(options.Region),
Endpoint: aws.String(options.Endpoint),
DisableSSL: aws.Bool(options.DisableSSL),
S3ForcePathStyle: aws.Bool(options.ForcePathStyle),
Credentials: cred,
}
s, err := session.NewSession(&config)
if err != nil {
klog.Error(err)
return nil, err
}
var c Client
c.s3Client = s3.New(s)
c.s3Session = s
c.bucket = options.Bucket
return &c, nil
}
func (s *Client) Client() *s3.S3 {
return s.s3Client
}
func (s *Client) Session() *session.Session {
return s.s3Session
}
func (s *Client) Bucket() *string {
return aws.String(s.bucket)
}