-
Notifications
You must be signed in to change notification settings - Fork 18
/
client.go
229 lines (202 loc) · 6.47 KB
/
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package minioclient
import (
"bytes"
"context"
"encoding/xml"
"errors"
"io"
"net/http"
minio "github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/signer"
"github.com/zeebo/errs"
)
// MinioError is class for minio errors.
var MinioError = errs.Class("minio")
// Config is the setup for a particular client.
type Config struct {
S3Gateway string
Satellite string
AccessKey string
SecretKey string
APIKey string
EncryptionKey string
NoSSL bool
ConfigDir string
}
// Client is the common interface for different implementations.
type Client interface {
MakeBucket(ctx context.Context, bucket, region string) error
RemoveBucket(ctx context.Context, bucket string) error
GetBucketLocation(ctx context.Context, bucket string) (string, error)
ListBuckets(ctx context.Context) ([]string, error)
ListBucketsAttribution(ctx context.Context) ([]string, error)
Upload(ctx context.Context, bucket, objectName string, data []byte) error
Download(ctx context.Context, bucket, objectName string, buffer []byte) ([]byte, error)
Delete(ctx context.Context, bucket, objectName string) error
ListObjects(ctx context.Context, bucket, prefix string) []string
}
// Minio implements basic S3 Client with minio.
type Minio struct {
API *minio.Client
config Config
httpClient *http.Client
}
// NewMinio creates new Client.
func NewMinio(conf Config) (Client, error) {
client, err := minio.New(conf.S3Gateway, &minio.Options{
Creds: credentials.NewStaticV4(conf.AccessKey, conf.SecretKey, ""),
Secure: !conf.NoSSL,
})
if err != nil {
return nil, MinioError.Wrap(err)
}
return &Minio{client, conf, &http.Client{}}, nil
}
// MakeBucket makes a new bucket.
func (client *Minio) MakeBucket(ctx context.Context, bucket, region string) error {
err := client.API.MakeBucket(ctx, bucket, minio.MakeBucketOptions{Region: region})
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// RemoveBucket removes a bucket.
func (client *Minio) RemoveBucket(ctx context.Context, bucket string) error {
err := client.API.RemoveBucket(ctx, bucket)
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// GetBucketLocation returns the bucket's location.
func (client *Minio) GetBucketLocation(ctx context.Context, bucket string) (string, error) {
location, err := client.API.GetBucketLocation(ctx, bucket)
return location, MinioError.Wrap(err)
}
// ListBuckets lists all buckets.
func (client *Minio) ListBuckets(ctx context.Context) ([]string, error) {
buckets, err := client.API.ListBuckets(ctx)
if err != nil {
return nil, MinioError.Wrap(err)
}
names := []string{}
for _, bucket := range buckets {
names = append(names, bucket.Name)
}
return names, nil
}
// Hex encoded string of nil sha256sum bytes.
var emptySHA256Hex = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
type listAllMyBucketsResult struct {
XMLName xml.Name `xml:"ListAllMyBucketsResult"`
Owner struct {
ID string `xml:"ID"`
DisplayName string `xml:"DisplayName"`
} `xml:"Owner"`
Buckets struct {
Bucket []struct {
CreationDate string `xml:"CreationDate"`
Name string `xml:"Name"`
Attribution string `xml:"Attribution"`
} `xml:"Bucket"`
} `xml:"Buckets"`
}
// ListBucketsAttribution lists all buckets with attribution.
func (client *Minio) ListBucketsAttribution(ctx context.Context) ([]string, error) {
scheme := "https"
if client.config.NoSSL {
scheme = "http"
}
endpointURLStr := scheme + "://" + client.config.S3Gateway + "/?attribution"
req, err := http.NewRequestWithContext(ctx, "GET", endpointURLStr, nil)
if err != nil {
return nil, MinioError.Wrap(err)
}
req.Header.Set("X-Amz-Content-Sha256", emptySHA256Hex)
sreq := signer.SignV4(*req, client.config.AccessKey, client.config.SecretKey, "", "us-east-1")
res, err := client.httpClient.Do(sreq)
if err != nil {
return nil, MinioError.Wrap(err)
}
d := xml.NewDecoder(res.Body)
listAllMyBucketsResult := listAllMyBucketsResult{}
err = d.Decode(&listAllMyBucketsResult)
if err != nil {
return nil, MinioError.Wrap(err)
}
if res.Body.Close() != nil {
return nil, MinioError.Wrap(err)
}
attributions := []string{}
for _, bucket := range listAllMyBucketsResult.Buckets.Bucket {
attributions = append(attributions, bucket.Attribution)
}
return attributions, nil
}
// Upload uploads object data to the specified path.
func (client *Minio) Upload(ctx context.Context, bucket, objectName string, data []byte) error {
_, err := client.API.PutObject(
ctx, bucket, objectName,
bytes.NewReader(data), int64(len(data)),
minio.PutObjectOptions{ContentType: "application/octet-stream"})
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// UploadMultipart uses multipart uploads, has hardcoded threshold.
func (client *Minio) UploadMultipart(ctx context.Context, bucket, objectName string, data []byte, partSize int, threshold int, metadata map[string]string) error {
_, err := client.API.PutObject(
ctx, bucket, objectName,
bytes.NewReader(data), -1,
minio.PutObjectOptions{
ContentType: "application/octet-stream",
PartSize: uint64(partSize),
UserMetadata: metadata,
})
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// Download downloads object data.
func (client *Minio) Download(ctx context.Context, bucket, objectName string, buffer []byte) ([]byte, error) {
reader, err := client.API.GetObject(ctx, bucket, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, MinioError.Wrap(err)
}
defer func() { _ = reader.Close() }()
n, err := reader.Read(buffer[:cap(buffer)])
if !errors.Is(err, io.EOF) {
rest, err := io.ReadAll(reader)
if errors.Is(err, io.EOF) {
err = nil
}
if err != nil {
return nil, MinioError.Wrap(err)
}
buffer = append(buffer, rest...)
n = len(buffer)
}
buffer = buffer[:n]
return buffer, nil
}
// Delete deletes object.
func (client *Minio) Delete(ctx context.Context, bucket, objectName string) error {
err := client.API.RemoveObject(ctx, bucket, objectName, minio.RemoveObjectOptions{})
if err != nil {
return MinioError.Wrap(err)
}
return nil
}
// ListObjects lists objects.
func (client *Minio) ListObjects(ctx context.Context, bucket, prefix string) []string {
var names []string
for message := range client.API.ListObjects(ctx, bucket, minio.ListObjectsOptions{Prefix: prefix}) {
names = append(names, message.Key)
}
return names
}