-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathminio.go
More file actions
224 lines (185 loc) Β· 5.75 KB
/
Copy pathminio.go
File metadata and controls
224 lines (185 loc) Β· 5.75 KB
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
package minio
import (
"bytes"
"context"
"errors"
"log"
"net/http"
"sync"
"time"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/valyala/bytebufferpool"
)
// Storage interface that is implemented by storage providers
type Storage struct {
minio *minio.Client
cfg Config
mu sync.Mutex
}
// errBucketNotFound is returned by CheckBucketWithContext when the configured
// bucket does not exist, as opposed to an operational error (auth, network,
// context cancellation) where the bucket should not be (re)created.
var errBucketNotFound = errors.New("the specified bucket does not exist")
// New creates a new minio storage using context.Background() for initialization.
func New(config ...Config) *Storage {
return NewWithContext(context.Background(), config...)
}
// NewWithContext creates a new minio storage, using ctx for the initialization
// operations (optional reset and bucket check/creation).
func NewWithContext(ctx context.Context, config ...Config) *Storage {
// Set default config
cfg := configDefault(config...)
// Set MaxRetry
minio.MaxRetry = cfg.MaxRetry
// Minio instance
minioClient, err := minio.New(cfg.Endpoint, &minio.Options{
Creds: credentials.NewStaticV4(cfg.Credentials.AccessKeyID, cfg.Credentials.SecretAccessKey, cfg.Token),
Secure: cfg.Secure,
Region: cfg.Region,
})
if err != nil {
panic(err)
}
storage := &Storage{minio: minioClient, cfg: cfg}
// Reset all entries if set to true
if cfg.Reset {
if err = storage.ResetWithContext(ctx); err != nil {
panic(err)
}
}
// check bucket
err = storage.CheckBucketWithContext(ctx)
if err != nil {
// Only create the bucket when it is genuinely missing; surface any
// other (auth/network/context) error instead of masking it.
if !errors.Is(err, errBucketNotFound) {
panic(err)
}
if err = storage.CreateBucketWithContext(ctx); err != nil {
panic(err)
}
}
return storage
}
// GetWithContext retrieves the value associated with the given key using the provided context.
func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error) {
if len(key) <= 0 {
return nil, errors.New("the key value is required")
}
// get object
object, err := s.minio.GetObject(ctx, s.cfg.Bucket, key, s.cfg.GetObjectOptions)
if err != nil {
return nil, err
}
defer func() {
if err := object.Close(); err != nil {
log.Printf("Error closing object: %v\n", err)
}
}()
// convert to byte
bb := bytebufferpool.Get()
defer bytebufferpool.Put(bb)
_, err = bb.ReadFrom(object)
if err != nil {
return nil, err
}
return bb.Bytes(), nil
}
// Get value by key
func (s *Storage) Get(key string) ([]byte, error) {
return s.GetWithContext(context.Background(), key)
}
// SetWithContext key with value with context
func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error {
if len(key) <= 0 {
return errors.New("the key value is required")
}
// create Reader
file := bytes.NewReader(val)
// set content type
s.mu.Lock()
s.cfg.PutObjectOptions.ContentType = http.DetectContentType(val)
// put object
_, err := s.minio.PutObject(ctx, s.cfg.Bucket, key, file, file.Size(), s.cfg.PutObjectOptions)
s.mu.Unlock()
return err
}
// Set key with value
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
return s.SetWithContext(context.Background(), key, val, exp)
}
// DeleteWithContext key with value with context
func (s *Storage) DeleteWithContext(ctx context.Context, key string) error {
if len(key) <= 0 {
return errors.New("the key value is required")
}
// remove
err := s.minio.RemoveObject(ctx, s.cfg.Bucket, key, s.cfg.RemoveObjectOptions)
return err
}
// Delete entry by key
func (s *Storage) Delete(key string) error {
return s.DeleteWithContext(context.Background(), key)
}
// ResetWithContext all keys with context
func (s *Storage) ResetWithContext(ctx context.Context) error {
objectsCh := make(chan minio.ObjectInfo)
// Send object names that are needed to be removed to objectsCh
go func() {
defer close(objectsCh)
// List all objects from a bucket-name with a matching prefix.
for object := range s.minio.ListObjects(ctx, s.cfg.Bucket, s.cfg.ListObjectsOptions) {
if object.Err != nil {
log.Println(object.Err)
}
objectsCh <- object
}
}()
opts := minio.RemoveObjectsOptions{
GovernanceBypass: true,
}
var errs []error
for err := range s.minio.RemoveObjects(ctx, s.cfg.Bucket, objectsCh, opts) {
errs = append(errs, err.Err)
}
return errors.Join(errs...)
}
func (s *Storage) Reset() error {
return s.ResetWithContext(context.Background())
}
// Close the storage
func (s *Storage) Close() error {
return nil
}
// CheckBucket Check to see if bucket already exists
func (s *Storage) CheckBucket() error {
return s.CheckBucketWithContext(context.Background())
}
// CheckBucketWithContext Check to see if bucket already exists, using the provided context
func (s *Storage) CheckBucketWithContext(ctx context.Context) error {
exists, err := s.minio.BucketExists(ctx, s.cfg.Bucket)
if err != nil {
return err
}
if !exists {
return errBucketNotFound
}
return nil
}
// CreateBucket Bucket not found so Make a new bucket
func (s *Storage) CreateBucket() error {
return s.CreateBucketWithContext(context.Background())
}
// CreateBucketWithContext Bucket not found so Make a new bucket, using the provided context
func (s *Storage) CreateBucketWithContext(ctx context.Context) error {
return s.minio.MakeBucket(ctx, s.cfg.Bucket, minio.MakeBucketOptions{Region: s.cfg.Region})
}
// RemoveBucket Bucket remove if bucket is empty
func (s *Storage) RemoveBucket() error {
return s.minio.RemoveBucket(context.Background(), s.cfg.Bucket)
}
// Conn Return minio client
func (s *Storage) Conn() *minio.Client {
return s.minio
}