-
Notifications
You must be signed in to change notification settings - Fork 0
/
teos3.go
387 lines (310 loc) · 9.79 KB
/
teos3.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright 2022-2023 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The TeoS3 package contains Golang functions that make it easy to use S3
// storage as a key-value database.
// This package uses [minio-go](https://github.com/minio/minio-go) S3 client.
package teos3
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"path/filepath"
"sync"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
const Version = "0.1.2"
const teoS3bucket = "teos3"
var ErrDestinationObjectAlreadyExists = errors.New(
"destination object already exists",
)
// TeoS3 objects data and methods receiver
type TeoS3 struct {
context context.Context
con *minio.Client
bucket string
}
// MapData is data structure used in ListBody output
type MapData struct {
Key string `json:"key"`
Value []byte `json:"value"`
}
// Connect creates new cinnwction to S3 storage using accessKey, secretKey,
// endpoint, secure flag and bucket (if omitted then default 'teos3' buckets
// name used). The enpoind argument must be specified without http/https
// prefix(just domain and path), and the secure argument defines HTTPS if true
// or HTTP if false.
func Connect(accessKey, secretKey, endpoint string, secure bool,
buckets ...string) (teos3 *TeoS3, err error) {
teos3 = new(TeoS3)
teos3.context = context.Background()
if teos3.con, err = minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
Secure: secure,
}); err != nil {
return
}
if len(buckets) > 0 && len(buckets[0]) > 0 {
teos3.bucket = buckets[0]
return
}
teos3.bucket = teoS3bucket
return
}
// SetContext sets context which will be used in all TeS3 operations if another
// context does not send in function call options argument.
func (m *TeoS3) SetContext(ctx context.Context) *TeoS3 {
return m
}
// Set sets data to map by key. The options parameter may be omitted and
// than default SetObjectOptions with context.Background and empty
// minio.PutObjectOptions used.
func (m *TeoS3) Set(key string, data []byte, options ...*SetOptions) error {
return m.SetObject(key, bytes.NewReader(data), int64(len(data)), options...)
}
// SetObject sets object to map by key. The options parameter may be omitted
// and than default SetObjectOptions with context.Background and empty
// minio.PutObjectOptions used.
func (m *TeoS3) SetObject(key string, reader io.Reader, objectSize int64,
options ...*SetOptions) (err error) {
// Set options
opt := m.getSetOptions(options...)
_, err = m.con.PutObject(opt.Context, m.bucket, key, reader, objectSize,
minio.PutObjectOptions(opt.SetObjectOptions),
)
return
}
// Get map data by key. The options parameter may be omitted and than default
// GetObjectOptions with context.Background and empty minio.SetObjectOptions
// used.
func (m *TeoS3) Get(key string, options ...*GetOptions) (
data []byte, err error) {
// Get object
obj, err := m.GetObject(key, options...)
if err != nil {
return
}
defer obj.Close()
// Read from raw object
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(obj)
if err != nil {
return
}
data = buf.Bytes()
return
}
// GetObject gets map object by key. The options parameter may be omitted and
// than default GetObjectOptions with context.Background and empty minio.
// SetObjectOptions used. Returned object must be cloused with obj.Close()
// after use.
func (m *TeoS3) GetObject(key string, options ...*GetOptions) (
*minio.Object, error) {
// Set options
opt := m.getGetOptions(options...)
return m.con.GetObject(opt.Context, m.bucket, key,
minio.GetObjectOptions(opt.GetObjectOptions))
}
// GetInfo fetchs metadata of an object by key.
func (m *TeoS3) GetInfo(key string, options ...*GetInfoOptions) (
minio.ObjectInfo, error) {
// Set options
opt := m.getGetInfoOptions(options...)
return m.con.StatObject(opt.Context, m.bucket, key,
minio.StatObjectOptions(opt.StatObjectOptions))
}
// Del remove key from map by key. The options parameter may be omitted and than
// default DelObjectOptions with context.Background and empty
// minio.RemoveObjectOptions used.
func (m *TeoS3) Del(key string, options ...*DelOptions) (err error) {
// Set options
opt := m.getDelOptions(options...)
// Perform a recursive delete of a folder
_, err = m.foreach(opt.Context, key, func(key string) (err error) {
return m.Del(key, options...)
})
if err != nil {
return
}
return m.con.RemoveObject(opt.Context, m.bucket, key,
minio.RemoveObjectOptions(opt.DelObjectOptions))
}
// ListLen returns the number of records in the list by prefix and options.
func (m *TeoS3) ListLen(prefix string, options ...*ListOptions) int {
// Get options from prefix and input options arguments
opt := m.getListOptions(prefix, options...)
objInfo := m.con.ListObjects(opt.Context, m.bucket,
minio.ListObjectsOptions(opt.ListObjectsOptions))
var i int
for range objInfo {
if opt.MaxKeys > 0 && i >= opt.MaxKeys {
break
}
i++
}
return i
}
// List gets list of map keys by prefix. The options parameter may be omitted
// and than default ListObjectsOptions with context.Background and empty
// minio.ListObjectsOptions used. The Prefix parameter of the ListObjectsOptions
// will be always overwritten with the prefix functions argument (so it may be
// empty).
func (m *TeoS3) List(prefix string, options ...*ListOptions) (keys chan string) {
// Get options from prefix and input options arguments
opt := m.getListOptions(prefix, options...)
// Get keys
keys = make(chan string, 1)
go func() {
var i int
objInfo := m.con.ListObjects(opt.Context, m.bucket,
minio.ListObjectsOptions(opt.ListObjectsOptions))
for obj := range objInfo {
if opt.MaxKeys > 0 && i >= opt.MaxKeys {
break
}
keys <- obj.Key
i++
}
close(keys)
}()
return
}
// ListAr gets string array of map keys by prefix.
func (m *TeoS3) ListAr(prefix string, options ...*ListOptions) (
list []string) {
// Get options from prefix and input options arguments
opt := m.getListOptions(prefix, options...)
objInfo := m.con.ListObjects(opt.Context, m.bucket,
minio.ListObjectsOptions(opt.ListObjectsOptions))
for obj := range objInfo {
list = append(list, obj.Key)
}
return
}
// ListBody gets all keys and values in MapData struct by prefix asynchronously.
func (m *TeoS3) ListBody(prefix string, options ...*ListOptions) (
mapDataChan chan MapData) {
// Get options from prefix and input options arguments
opt := m.getListOptions(prefix, options...)
objInfo := m.con.ListObjects(opt.Context, m.bucket,
minio.ListObjectsOptions(opt.ListObjectsOptions))
mapDataChan = make(chan MapData, 1)
go func() {
var wg sync.WaitGroup
for obj := range objInfo {
wg.Add(1)
go func(obj minio.ObjectInfo) {
defer wg.Done()
data, err := m.Get(obj.Key)
if err != nil {
return
}
mapDataChan <- MapData{obj.Key, data}
}(obj)
}
wg.Wait()
close(mapDataChan)
}()
return
}
// ListBodyAr gets MapData array with all keys and values by prefix.
func (m *TeoS3) ListBodyAr(prefix string, options ...*ListOptions) (
listBody []MapData) {
mapDataChan := m.ListBody(prefix, options...)
for mapData := range mapDataChan {
listBody = append(listBody, mapData)
}
return
}
// Copy copys source object to destination object
func (m *TeoS3) Copy(source, destination string, options ...*CopyOptions) (
err error) {
// Get context from options argument
context := m.context
if len(options) > 0 {
context = options[0].Context
}
// Check if destination does not exist
if _, e := m.GetInfo(destination, &GetInfoOptions{Context: context}); e == nil {
err = ErrDestinationObjectAlreadyExists
return
}
// Recursive copy
done, err := m.foreach(context, source, func(key string) (err error) {
name := m.fileBase(key)
return m.Copy(source+name, destination+name, options...)
})
if err != nil || done {
return
}
// If destination is a folder than create new destination folder instead of
// copy source folder to destination because there is empty folder (all
// folder was processed in Recursive copy above) and CopyObject wiil return
// an error for copy folder
if m.isFolder(destination) {
return m.Set(destination, nil)
}
// Create copy source option
src := minio.CopySrcOptions{
Bucket: m.bucket,
Object: source,
}
// Create copy destination option
dst := minio.CopyDestOptions{
Bucket: m.bucket,
Object: destination,
}
// Copy source object to destination object
_, err = m.con.CopyObject(context, dst, src)
fmt.Println("copy", dst, src, err)
return
}
// Move movess source object to destination object
func (m *TeoS3) Move(source, destination string, options ...*CopyOptions) (
err error) {
// Copy source object to destination object
if err = m.Copy(source, destination, options...); err != nil {
return
}
// Get context from options arguments
context := m.context
if len(options) > 0 {
context = options[0].Context
}
// Delete source object
err = m.Del(source, &DelOptions{Context: context})
return
}
// foreach calls callback function for all keys in folder key. The foreach
// returns done = true if at list one callback function was processed.
func (m *TeoS3) foreach(context context.Context, key string,
calback func(key string) error) (done bool, err error) {
if m.isFolder(key) {
for k := range m.List(key, &ListOptions{Context: context}) {
if k == key {
continue
}
done = true
if err = calback(k); err != nil {
return
}
}
}
return
}
// Base returns the last element of path including trailing slash.
func (m *TeoS3) fileBase(key string) (base string) {
base = filepath.Base(key)
if m.isFolder(key) {
base += "/"
}
return base
}
// isFolder returns true if key is a folder
func (m *TeoS3) isFolder(key string) bool {
l := len(key)
return l > 0 && key[l-1] == '/'
}