-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
329 lines (260 loc) · 6.95 KB
/
storage.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
package storage
import (
"io"
"sync"
"time"
lbcf "github.com/lidstromberg/config"
lblog "github.com/lidstromberg/log"
"cloud.google.com/go/storage"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// StorMgr handles interactions with GCS
type StorMgr struct {
st *storage.Client
bc lbcf.ConfigSetting
}
// NewMgr returns a new storage manager
func NewMgr(ctx context.Context, bc lbcf.ConfigSetting) (*StorMgr, error) {
preflight(ctx, bc)
if EnvDebugOn {
lblog.LogEvent("StorMgr", "NewMgr", "info", "start")
}
storageClient, err := storage.NewClient(ctx, option.WithGRPCConnectionPool(EnvClientPool))
if err != nil {
return nil, err
}
st1 := &StorMgr{
st: storageClient,
bc: bc,
}
if EnvDebugOn {
lblog.LogEvent("StorMgr", "NewMgr", "info", "end")
}
return st1, nil
}
// NewJSONMgr returns a new storage manager based on a GCP credential supplied as a byte array
func NewJSONMgr(ctx context.Context, bc lbcf.ConfigSetting, cred []byte) (*StorMgr, error) {
preflight(ctx, bc)
if EnvDebugOn {
lblog.LogEvent("StorMgr", "NewJSONMgr", "info", "start")
}
storageClient, err := storage.NewClient(ctx, option.WithGRPCConnectionPool(EnvClientPool), option.WithCredentialsJSON(cred))
if err != nil {
return nil, err
}
st1 := &StorMgr{
st: storageClient,
bc: bc,
}
if EnvDebugOn {
lblog.LogEvent("StorMgr", "NewJSONMgr", "info", "end")
}
return st1, nil
}
// GetBucketFileData returns a byte array for a bucket file
func (sto *StorMgr) GetBucketFileData(ctx context.Context, bucketName string, fileName string) ([]byte, error) {
if EnvDebugOn {
lblog.LogEvent("StorMgr", "GetBucketFileData", "info", "start")
}
rc, err := sto.st.Bucket(bucketName).Object(fileName).NewReader(ctx)
if err != nil {
return nil, err
}
defer func(rc *storage.Reader) {
err := rc.Close()
if err != nil {
lblog.LogEvent("StorMgr", "GetBucketFileData", "error", err.Error())
}
}(rc)
data, err := io.ReadAll(rc)
if err != nil {
return nil, err
}
if EnvDebugOn {
lblog.LogEvent("StorMgr", "GetBucketFileData", "info", "end")
}
return data, nil
}
// WriteBucketFile writes a file byte array to a bucket file
func (sto *StorMgr) WriteBucketFile(ctx context.Context, bucketName string, fileName string, data []byte) error {
if EnvDebugOn {
lblog.LogEvent("StorMgr", "WriteBucketFile", "info", "start")
}
wc := sto.st.Bucket(bucketName).Object(fileName).NewWriter(ctx)
defer func(wc *storage.Writer) {
err := wc.Close()
if err != nil {
lblog.LogEvent("StorMgr", "GetBucketFileData", "error", err.Error())
}
}(wc)
if _, err := wc.Write(data); err != nil {
return err
}
if EnvDebugOn {
lblog.LogEvent("StorMgr", "WriteBucketFile", "info", "end")
}
return nil
}
// ListBucket returns a configurable buffered channel which contains a subset of object metadata
func (sto *StorMgr) ListBucket(ctx context.Context, bucketName, prefix string, bufferSize int) (<-chan interface{}, error) {
if EnvDebugOn {
lblog.LogEvent("StorMgr", "ListBucket", "info", "start")
}
//waitgroup to control goroutines
var wg sync.WaitGroup
//create the channel
result := make(chan interface{}, bufferSize)
//bucket listing function
rfn := func(bucketname, prefix string) {
lbucketName := bucketname
lprefix := prefix
defer wg.Done()
//get an iterator to the target bucket
it := &storage.ObjectIterator{}
//if a query prefix was supplied, then use it
if lprefix != "" {
qr := &storage.Query{
Prefix: lprefix,
}
itx := sto.st.Bucket(lbucketName).Objects(ctx, qr)
it = itx
} else {
itx := sto.st.Bucket(lbucketName).Objects(ctx, nil)
it = itx
}
//collect the subset of attributes
for {
attrs, err := it.Next()
if err != nil {
if err == iterator.Done {
return
}
//send back the error if any occur
result <- err
return
}
//collect the attributes
at := make(map[string]interface{})
at[ObjAttrName] = attrs.Name
at[ObjAttrContentType] = attrs.ContentType
at[ObjAttrOwner] = attrs.Owner
at[ObjAttrSize] = attrs.Size
at[ObjAttrContentEncoding] = attrs.ContentEncoding
at[ObjAttrCreated] = attrs.Created.Unix()
select {
case <-ctx.Done():
return
default:
result <- at
}
}
}
wg.Add(1)
go rfn(bucketName, prefix)
go func() {
wg.Wait()
close(result)
}()
if EnvDebugOn {
lblog.LogEvent("StorMgr", "ListBucket", "info", "end")
}
return result, nil
}
// ListBucketByTime returns a configurable buffered channel which contains a subset of object metadata
func (sto *StorMgr) ListBucketByTime(ctx context.Context, bucketName, prefix string, start, end *time.Time, bufferSize int) (<-chan interface{}, error) {
if EnvDebugOn {
lblog.LogEvent("StorMgr", "ListBucketByTime", "info", "start")
}
//if the dates are not available, then exit with error
if start == nil || end == nil {
return nil, ErrMissingDateRange
}
//waitgroup to control goroutines
var wg sync.WaitGroup
//create the channel
result := make(chan interface{}, bufferSize)
//bucket listing function
rfn := func(bucketname, prefix string) {
lbucketName := bucketname
lprefix := prefix
defer wg.Done()
//get an iterator to the target bucket
it := &storage.ObjectIterator{}
//if a query prefix was supplied, then use it
if lprefix != "" {
qr := &storage.Query{
Prefix: lprefix,
}
itx := sto.st.Bucket(lbucketName).Objects(ctx, qr)
it = itx
} else {
itx := sto.st.Bucket(lbucketName).Objects(ctx, nil)
it = itx
}
//collect the subset of attributes
for {
attrs, err := it.Next()
if err != nil {
if err == iterator.Done {
return
}
//send back the error if any occur
result <- err
return
}
//collect the object attributes if the object is created within the required date range
if attrs.Created.After(*start) && attrs.Created.Before(*end) {
at := make(map[string]interface{})
at[ObjAttrName] = attrs.Name
at[ObjAttrContentType] = attrs.ContentType
at[ObjAttrOwner] = attrs.Owner
at[ObjAttrSize] = attrs.Size
at[ObjAttrContentEncoding] = attrs.ContentEncoding
at[ObjAttrCreated] = attrs.Created.Unix()
select {
case <-ctx.Done():
return
default:
result <- at
}
}
}
}
wg.Add(1)
go rfn(bucketName, prefix)
go func() {
wg.Wait()
close(result)
}()
if EnvDebugOn {
lblog.LogEvent("StorMgr", "ListBucketByTime", "info", "end")
}
return result, nil
}
// RemoveFile deletes a bucket file
func (sto *StorMgr) RemoveFile(ctx context.Context, bucketName string, fileName string) error {
if EnvDebugOn {
lblog.LogEvent("StorMgr", "RemoveFile", "info", "start")
}
err := sto.st.Bucket(bucketName).Object(fileName).Delete(ctx)
if err != nil {
return err
}
if EnvDebugOn {
lblog.LogEvent("StorMgr", "RemoveFile", "info", "end")
}
return nil
}
// DrainFn drains a channel until it is closed
func DrainFn(c <-chan interface{}) {
for {
select {
case _, ok := <-c:
if ok == false {
return
}
}
}
}