-
Notifications
You must be signed in to change notification settings - Fork 168
/
s3_client.go
191 lines (176 loc) · 5.77 KB
/
s3_client.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2019-2021. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
package mc
import (
"context"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pydio/cells/v4/common"
"io"
"net/http"
"net/url"
minio "github.com/minio/minio-go/v7"
"github.com/pydio/cells/v4/common/nodes/models"
"github.com/pydio/cells/v4/common/proto/tree"
"github.com/pydio/cells/v4/common/sync/endpoints/cells/transport"
"github.com/pydio/cells/v4/common/sync/endpoints/cells/transport/oidc"
)
type S3Client struct {
config *transport.SdkConfig
s3config *transport.S3Config
}
func NewS3Client(config *transport.SdkConfig) *S3Client {
return &S3Client{
config: config,
s3config: &transport.S3Config{
Bucket: "data",
ApiKey: common.S3GatewayRootUser,
ApiSecret: common.S3GatewayRootPassword,
Region: common.S3GatewayDefaultRegion,
UsePydioSpecificHeader: false,
IsDebug: false,
Endpoint: config.Url,
},
}
}
func (g *S3Client) GetObject(ctx context.Context, node *tree.Node, requestData *models.GetRequestData) (io.ReadCloser, error) {
jwt, err := oidc.RetrieveToken(g.config)
if err != nil {
return nil, err
}
u, _ := url.Parse(g.s3config.Endpoint)
t := http.DefaultTransport
if g.config.SkipVerify {
t = transport.InsecureTransport
}
if g.config.CustomHeaders != nil && len(g.config.CustomHeaders) > 0 {
t = &customHeaderRoundTripper{rt: t, Headers: g.config.CustomHeaders}
}
opts := &minio.Options{
Creds: credentials.NewStaticV4(jwt, g.s3config.ApiSecret, ""),
Secure: u.Scheme == "https",
Transport: t,
}
mc, e := minio.NewCore(u.Host, opts)
if e != nil {
return nil, e
}
r, _, _, e := mc.GetObject(ctx, g.s3config.Bucket, node.Path, minio.GetObjectOptions{})
return r, e
}
func (g *S3Client) PutObject(ctx context.Context, node *tree.Node, reader io.Reader, requestData *models.PutRequestData) (models.ObjectInfo, error) {
jwt, err := oidc.RetrieveToken(g.config)
if err != nil {
return models.ObjectInfo{}, err
}
u, _ := url.Parse(g.s3config.Endpoint)
t := http.DefaultTransport
if g.config.SkipVerify {
t = transport.InsecureTransport
}
if g.config.CustomHeaders != nil && len(g.config.CustomHeaders) > 0 {
t = &customHeaderRoundTripper{rt: t, Headers: g.config.CustomHeaders}
}
opts := &minio.Options{
Creds: credentials.NewStaticV4(jwt, g.s3config.ApiSecret, ""),
Secure: u.Scheme == "https",
Transport: t,
}
mc, e := minio.NewCore(u.Host, opts)
if e != nil {
return models.ObjectInfo{}, e
}
partSize := 100 * 1024 * 1024
partSizeMultiple := int64(10 * 1024 * 1024)
maxPartsCount := float64(8000) // Official max is 10000, we add some security here
objectSize := float64(requestData.Size)
if objectSize > 0 && objectSize > maxPartsCount*float64(partSize) {
// Make sure that max parts count are not reached, and also that newSize is the closest multiple to 10MB (for cells server)
newSize := int64(objectSize / maxPartsCount)
newSize = newSize + partSizeMultiple/2
newSize = newSize - (newSize % partSizeMultiple)
}
if ui, e := mc.Client.PutObject(ctx, g.s3config.Bucket, node.Path, reader, requestData.Size, minio.PutObjectOptions{
UserMetadata: requestData.Metadata,
PartSize: uint64(partSize),
}); e == nil {
return models.ObjectInfo{
ETag: ui.ETag,
Key: ui.Key,
LastModified: ui.LastModified,
Size: ui.Size,
}, nil
} else {
return models.ObjectInfo{}, e
}
}
func (g *S3Client) CopyObject(ctx context.Context, from *tree.Node, to *tree.Node, requestData *models.CopyRequestData) (models.ObjectInfo, error) {
jwt, err := oidc.RetrieveToken(g.config)
if err != nil {
return models.ObjectInfo{}, err
}
u, _ := url.Parse(g.s3config.Endpoint)
t := http.DefaultTransport
if g.config.SkipVerify {
t = transport.InsecureTransport
}
if g.config.CustomHeaders != nil && len(g.config.CustomHeaders) > 0 {
t = &customHeaderRoundTripper{rt: t, Headers: g.config.CustomHeaders}
}
opts := &minio.Options{
Creds: credentials.NewStaticV4(jwt, g.s3config.ApiSecret, ""),
Secure: u.Scheme == "https",
Transport: t,
}
mc, e := minio.NewCore(u.Host, opts)
if e != nil {
return models.ObjectInfo{}, e
}
dst := minio.PutObjectOptions{
UserMetadata: requestData.Metadata,
}
src := minio.CopySrcOptions{
Bucket: g.s3config.Bucket,
Object: from.Path,
}
oi, er := mc.CopyObject(ctx, g.s3config.Bucket, from.Path, g.s3config.Bucket, to.Path, nil, src, dst)
if er != nil {
return models.ObjectInfo{}, er
}
return models.ObjectInfo{
ETag: oi.ETag,
Key: oi.Key,
LastModified: oi.LastModified,
Size: oi.Size,
ContentType: oi.ContentType,
Metadata: oi.Metadata,
StorageClass: oi.StorageClass,
Err: oi.Err,
}, nil
}
type customHeaderRoundTripper struct {
rt http.RoundTripper
Headers map[string]string
}
func (c customHeaderRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
for k, v := range c.Headers {
req.Header.Set(k, v)
}
return c.rt.RoundTrip(req)
}