-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-s3.go
718 lines (647 loc) · 21.7 KB
/
client-s3.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
* Copyright (c) 2019. 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 s3 provides an endpoint for connecting to Amazon S3 or an S3-compatible storage
package s3
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
errors2 "github.com/micro/go-micro/errors"
"github.com/pborman/uuid"
"github.com/pydio/minio-go"
"go.uber.org/zap"
"golang.org/x/text/unicode/norm"
servicescommon "github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/sync/model"
)
var (
UserAgentAppName = "pydio.sync.client.s3"
UserAgentVersion = "1.0"
)
// Client wraps a Minio Client to speak with an S3-compatible backend
type Client struct {
Mc MockableMinio
Bucket string
RootPath string
Host string
ServerRequiresNormalization bool
options model.EndpointOptions
globalContext context.Context
plainSizeComputer func(nodeUUID string) (int64, error)
}
func NewClient(ctx context.Context, host string, key string, secret string, bucket string, rootPath string, secure bool, options model.EndpointOptions) (*Client, error) {
mc, e := minio.New(host, key, secret, secure)
if e != nil {
return nil, e
}
mc.SetAppInfo(UserAgentAppName, UserAgentVersion)
return &Client{
Mc: mc,
Bucket: bucket,
Host: host,
RootPath: strings.TrimRight(rootPath, "/"),
options: options,
globalContext: ctx,
}, e
}
func (c *Client) GetEndpointInfo() model.EndpointInfo {
return model.EndpointInfo{
URI: "s3://" + c.Host + "/" + path.Join(c.Bucket, c.RootPath),
RequiresFoldersRescan: false,
RequiresNormalization: c.ServerRequiresNormalization,
}
}
func (c *Client) normalize(path string) string {
if c.ServerRequiresNormalization {
return string(norm.NFC.Bytes([]byte(path)))
}
return path
}
func (c *Client) denormalize(path string) string {
if c.ServerRequiresNormalization {
return string(norm.NFD.Bytes([]byte(path)))
}
return path
}
func (c *Client) getLocalPath(eventPath string) string {
return strings.TrimPrefix(c.normalize(eventPath), c.normalize(c.RootPath))
}
func (c *Client) getFullPath(path string) string {
path = c.denormalize(path)
if c.RootPath == "" {
return strings.TrimLeft(path, "/")
} else {
return strings.TrimLeft(c.RootPath+"/"+strings.TrimLeft(path, "/"), "/")
}
}
func (c *Client) Stat(path string) (i os.FileInfo, err error) {
if path == "" || path == "/" {
buckets, err := c.Mc.ListBuckets()
if err != nil {
return nil, err
}
for _, b := range buckets {
if b.Name == c.Bucket {
return NewS3FolderInfo(minio.ObjectInfo{
Key: "/",
LastModified: b.CreationDate,
}), nil
}
}
return nil, errors2.NotFound("bucket.not.found", "cannot find bucket %s", c.Bucket)
}
objectInfo, e := c.Mc.StatObject(c.Bucket, c.getFullPath(path), minio.StatObjectOptions{})
if e != nil {
// Try folder
folderInfo, e2 := c.Mc.StatObject(c.Bucket, c.getFullPath(path)+"/"+servicescommon.PYDIO_SYNC_HIDDEN_FILE_META, minio.StatObjectOptions{})
if e2 != nil {
return nil, e
}
return NewS3FolderInfo(folderInfo), nil
}
return NewS3FileInfo(objectInfo), nil
}
func (c *Client) CreateNode(ctx context.Context, node *tree.Node, updateIfExists bool) (err error) {
if node.IsLeaf() {
return errors.New("This is a DataSyncTarget, use PutNode for leafs instead of CreateNode")
}
hiddenPath := fmt.Sprintf("%v/%s", c.getFullPath(node.Path), servicescommon.PYDIO_SYNC_HIDDEN_FILE_META)
_, err = c.Mc.PutObject(c.Bucket, hiddenPath, strings.NewReader(node.Uuid), int64(len(node.Uuid)), minio.PutObjectOptions{ContentType: "text/plain"})
return err
}
func (c *Client) DeleteNode(ctx context.Context, path string) (err error) {
path = c.getFullPath(path)
doneChan := make(chan struct{})
defer close(doneChan)
for object := range c.Mc.ListObjectsV2(c.Bucket, path, true, doneChan) {
err = c.Mc.RemoveObject(c.Bucket, object.Key)
if err != nil {
log.Logger(c.globalContext).Error("Error while deleting object", zap.String("key", object.Key), zap.Error(err))
return err
}
}
return err
}
func (c *Client) MoveNode(ctx context.Context, oldPath string, newPath string) (err error) {
doneChan := make(chan struct{})
defer close(doneChan)
oldPath = c.getFullPath(oldPath)
newPath = c.getFullPath(newPath)
for object := range c.Mc.ListObjectsV2(c.Bucket, oldPath, true, doneChan) {
targetKey := newPath + strings.TrimPrefix(object.Key, oldPath)
destinationInfo, _ := minio.NewDestinationInfo(c.Bucket, targetKey, nil, nil)
sourceInfo := minio.NewSourceInfo(c.Bucket, object.Key, nil)
copyResult := c.Mc.CopyObject(destinationInfo, sourceInfo)
if copyResult == nil {
c.Mc.RemoveObject(c.Bucket, object.Key)
} else {
return copyResult
}
}
return nil
}
func (c *Client) GetWriterOn(cancel context.Context, path string, targetSize int64) (out io.WriteCloser, writeDone chan bool, writeErr chan error, err error) {
writeErr = make(chan error, 1)
writeDone = make(chan bool, 1)
path = c.getFullPath(path)
reader, out := io.Pipe()
go func() {
defer func() {
reader.Close()
close(writeDone)
close(writeErr)
}()
_, e := c.Mc.PutObject(c.Bucket, path, reader, targetSize, minio.PutObjectOptions{ContentType: "application/octet-stream"})
if e != nil {
writeErr <- e
}
}()
return out, writeDone, writeErr, nil
}
func (c *Client) GetReaderOn(path string) (out io.ReadCloser, err error) {
return c.Mc.GetObject(c.Bucket, c.getFullPath(path), minio.GetObjectOptions{})
}
func (c *Client) ComputeChecksum(node *tree.Node) error {
p := c.getFullPath(node.GetPath())
if newInfo, err := c.s3forceComputeEtag(minio.ObjectInfo{Key: p}); err == nil {
node.Etag = strings.Trim(newInfo.ETag, "\"")
} else {
return err
}
return nil
}
func (c *Client) Walk(walknFc model.WalkNodesFunc, root string, recursive bool) (err error) {
ctx := context.Background()
wrappingFunc := func(path string, info *S3FileInfo, err error) error {
path = c.getLocalPath(path)
node, test := c.loadNode(ctx, path, !info.IsDir())
if test != nil || node == nil {
// Ignoring node not found
return nil
}
node.MTime = info.ModTime().Unix()
node.Size = info.Size()
node.Mode = int32(info.Mode())
if !info.IsDir() {
node.Etag = strings.Trim(info.Object.ETag, "\"")
} else {
node.Uuid = strings.Trim(info.Object.ETag, "\"")
}
walknFc(path, node, nil)
return nil
}
return c.actualLsRecursive(recursive, c.getFullPath(root), wrappingFunc)
}
func (c *Client) actualLsRecursive(recursive bool, recursivePath string, walknFc func(path string, info *S3FileInfo, err error) error) (err error) {
doneChan := make(chan struct{})
defer close(doneChan)
createdDirs := make(map[string]bool)
log.Logger(c.globalContext).Info("Listing all S3 objects for path", zap.String("bucket", c.Bucket), zap.String("path", recursivePath))
for objectInfo := range c.Mc.ListObjectsV2(c.Bucket, recursivePath, recursive, doneChan) {
if objectInfo.Err != nil {
log.Logger(c.globalContext).Error("Error while listing", zap.Error(objectInfo.Err))
return objectInfo.Err
}
folderKey := path.Dir(objectInfo.Key)
if strings.HasSuffix(objectInfo.Key, servicescommon.PYDIO_SYNC_HIDDEN_FILE_META) {
// Create Fake Folder
// log.Print("Folder Key is " , folderKey)
if folderKey == "" || folderKey == "." {
continue
}
if _, exists := createdDirs[folderKey]; exists {
if !c.isIgnoredFile(objectInfo.Key) {
// Walk the .pydio before continuing, otherwise this creates an issue if another hidden file like ".aaa"
// is appearing in the folder before arriving to the ".pydio" file
s3FileInfo := NewS3FileInfo(objectInfo)
walknFc(c.normalize(objectInfo.Key), s3FileInfo, nil)
}
continue
}
folderObjectInfo := objectInfo
//This will be called again inside the walknFc
c.createFolderIdsWhileWalking(createdDirs, walknFc, folderKey, objectInfo.LastModified, true)
folderObjectInfo.ETag, _, _ = c.readOrCreateFolderId(folderKey)
s3FileInfo := NewS3FolderInfo(folderObjectInfo)
walknFc(c.normalize(folderKey), s3FileInfo, nil)
createdDirs[folderKey] = true
}
if c.isIgnoredFile(objectInfo.Key) {
continue
}
if folderKey != "" && folderKey != "." {
c.createFolderIdsWhileWalking(createdDirs, walknFc, folderKey, objectInfo.LastModified, false)
}
s3FileInfo := NewS3FileInfo(objectInfo)
walknFc(c.normalize(objectInfo.Key), s3FileInfo, nil)
}
return nil
}
// Will try to create PYDIO_SYNC_HIDDEN_FILE_META to avoid missing empty folders
func (c *Client) createFolderIdsWhileWalking(createdDirs map[string]bool, walknFc func(path string, info *S3FileInfo, err error) error, currentDir string, lastModified time.Time, skipLast bool) {
// Do not create hidden files in BrowseOnly mode
if c.options.BrowseOnly {
return
}
parts := strings.Split(currentDir, "/")
max := len(parts)
if skipLast {
max = len(parts) - 1
if max == 0 {
return
}
}
for i := 0; i < max; i++ {
testDir := strings.Join(parts[0:i+1], "/")
if _, exists := createdDirs[testDir]; exists {
continue
}
uid, created, _ := c.readOrCreateFolderId(testDir)
dirObjectInfo := minio.ObjectInfo{
ETag: uid,
Key: testDir,
LastModified: lastModified,
Size: 0,
}
s3FolderInfo := NewS3FolderInfo(dirObjectInfo)
walknFc(c.normalize(testDir), s3FolderInfo, nil)
if created.Key != "" {
walknFc(c.normalize(created.Key), NewS3FileInfo(created), nil)
}
createdDirs[testDir] = true
}
}
func (c *Client) s3forceComputeEtag(objectInfo minio.ObjectInfo) (minio.ObjectInfo, error) {
var destinationInfo minio.DestinationInfo
var sourceInfo minio.SourceInfo
destinationInfo, _ = minio.NewDestinationInfo(c.Bucket, objectInfo.Key, nil, nil)
sourceInfo = minio.NewSourceInfo(c.Bucket, objectInfo.Key, nil)
sourceInfo.Headers.Set(servicescommon.X_AMZ_META_DIRECTIVE, "REPLACE")
copyErr := c.Mc.CopyObject(destinationInfo, sourceInfo)
if copyErr != nil {
log.Logger(c.globalContext).Error("Compute Etag Copy", zap.Error(copyErr))
return objectInfo, copyErr
}
newInfo, e := c.Mc.StatObject(c.Bucket, objectInfo.Key, minio.StatObjectOptions{})
if e != nil {
return objectInfo, e
}
return newInfo, nil
}
func (c *Client) LoadNode(ctx context.Context, path string, extendedStats ...bool) (node *tree.Node, err error) {
return c.loadNode(ctx, path)
}
func (c *Client) loadNode(ctx context.Context, path string, leaf ...bool) (node *tree.Node, err error) {
var hash, uid string
var isLeaf bool
var metaSize int64
var stat os.FileInfo
var eStat error
if len(leaf) > 0 {
isLeaf = leaf[0]
} else {
stat, eStat = c.Stat(path)
if eStat != nil {
return nil, eStat
} else {
isLeaf = !stat.IsDir()
}
}
uid, hash, metaSize, err = c.getNodeIdentifier(path, isLeaf)
if err != nil {
return nil, err
}
nodeType := tree.NodeType_LEAF
if !isLeaf {
nodeType = tree.NodeType_COLLECTION
}
node = &tree.Node{
Path: path,
Type: nodeType,
Uuid: uid,
Etag: hash,
}
if stat != nil {
node.MTime = stat.ModTime().Unix()
node.Size = stat.Size()
node.Mode = int32(stat.Mode())
} else {
node.MTime = time.Now().Unix()
}
if metaSize > -1 {
node.Size = metaSize
}
return node, nil
}
// UpdateNodeUuid makes this endpoint an UuidReceiver
func (c *Client) UpdateNodeUuid(ctx context.Context, node *tree.Node) (*tree.Node, error) {
var uid string
if node.Uuid != "" {
uid = node.Uuid
} else {
uid = uuid.New()
node.Uuid = uid
}
if node.IsLeaf() {
d, e := minio.NewDestinationInfo(c.Bucket, c.getFullPath(node.Path), nil, map[string]string{
servicescommon.X_AMZ_META_DIRECTIVE: "REPLACE",
servicescommon.X_AMZ_META_NODE_UUID: node.Uuid,
})
if e != nil {
return nil, e
}
s := minio.NewSourceInfo(c.Bucket, c.getFullPath(node.Path), nil)
err := c.Mc.CopyObject(d, s)
return node, err
} else {
hiddenPath := fmt.Sprintf("%v/%s", c.getFullPath(node.Path), servicescommon.PYDIO_SYNC_HIDDEN_FILE_META)
_, err := c.Mc.PutObject(c.Bucket, hiddenPath, strings.NewReader(uid), int64(len(uid)), minio.PutObjectOptions{ContentType: "text/plain"})
return node, err
}
}
func (c *Client) SetPlainSizeComputer(computer func(nodeUUID string) (int64, error)) {
c.plainSizeComputer = computer
}
func (c *Client) getNodeIdentifier(path string, leaf bool) (uid string, eTag string, metaSize int64, e error) {
if leaf {
return c.getFileHash(c.getFullPath(path))
} else {
uid, _, e = c.readOrCreateFolderId(c.getFullPath(path))
return uid, "", -1, e
}
}
func (c *Client) readOrCreateFolderId(folderPath string) (uid string, created minio.ObjectInfo, e error) {
hiddenPath := fmt.Sprintf("%v/%s", folderPath, servicescommon.PYDIO_SYNC_HIDDEN_FILE_META)
hiddenPath = strings.TrimLeft(hiddenPath, "/")
object, err := c.Mc.GetObject(c.Bucket, hiddenPath, minio.GetObjectOptions{})
if err == nil {
defer object.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(object)
uid = buf.String()
if len(strings.TrimSpace(uid)) > 0 {
log.Logger(c.globalContext).Debug("Read Uuid for folderPath", zap.String("path", folderPath), zap.String("uuid", uid))
return uid, minio.ObjectInfo{}, nil
}
}
// Does not exists
// Create dir uuid now
uid = uuid.New()
eTag := model.StringContentToETag(uid)
if c.options.BrowseOnly {
// Return fake file without actually creating it
return uid, minio.ObjectInfo{
ETag: eTag,
Key: hiddenPath,
LastModified: time.Now(),
Size: 36,
ContentType: "text/plain",
}, nil
}
log.Logger(c.globalContext).Info("Create Hidden File for folder", zap.String("path", hiddenPath))
size, _ := c.Mc.PutObject(c.Bucket, hiddenPath, strings.NewReader(uid), int64(len(uid)), minio.PutObjectOptions{ContentType: "text/plain"})
created = minio.ObjectInfo{
ETag: eTag,
Key: hiddenPath,
LastModified: time.Now(),
Size: size,
ContentType: "text/plain",
}
return uid, created, nil
}
func (c *Client) getFileHash(path string) (uid string, hash string, metaSize int64, e error) {
metaSize = -1
objectInfo, e := c.Mc.StatObject(c.Bucket, path, minio.StatObjectOptions{})
if e != nil {
return "", "", metaSize, e
}
uid = objectInfo.Metadata.Get(servicescommon.X_AMZ_META_NODE_UUID)
if size := objectInfo.Metadata.Get(servicescommon.X_AMZ_META_CLEAR_SIZE); size != "" {
if size == servicescommon.X_AMZ_META_CLEAR_SIZE_UNKOWN {
if c.plainSizeComputer != nil {
if plain, er := c.plainSizeComputer(uid); er == nil {
metaSize = plain
// TODO: Refresh metadata inside object now?
} else {
log.Logger(c.globalContext).Info("Cannot compute plain size", zap.Error(er))
}
}
} else {
metaSize, _ = strconv.ParseInt(size, 10, 64)
}
}
etag := strings.Trim(objectInfo.ETag, "\"")
/*
if len(etag) == 0 || etag == common.DefaultEtag {
fmt.Println("getFileHash - Recompute ETAG")
var etagE error
objectInfo, etagE = c.s3forceComputeEtag(objectInfo)
if etagE != nil {
return uid, "", metaSize, etagE
}
etag = strings.Trim(objectInfo.ETag, "\"")
}
*/
return uid, etag, metaSize, nil
}
func (c *Client) Watch(recursivePath string) (*model.WatchObject, error) {
eventChan := make(chan model.EventInfo)
errorChan := make(chan error)
doneChan := make(chan bool)
log.Logger(c.globalContext).Debug("Watching Bucket", zap.String("bucket", c.Bucket))
// Extract bucket and object.
//if err := isValidBucketName(bucket); err != nil {
// return nil, err
//}
// Flag set to set the notification.
var events []string
events = append(events, string(minio.ObjectCreatedAll))
events = append(events, string(minio.ObjectRemovedAll))
doneCh := make(chan struct{})
// wait for doneChan to close the other channels
go func() {
<-doneChan
close(doneCh)
close(eventChan)
close(errorChan)
}()
// Start listening on all bucket events.
eventsCh := c.Mc.ListenBucketNotification(c.Bucket, c.getFullPath(recursivePath), "", events, doneCh)
wo := &model.WatchObject{
EventInfoChan: eventChan,
ErrorChan: errorChan,
DoneChan: doneChan,
}
// wait for events to occur and sent them through the eventChan and errorChan
go func() {
defer wo.Close()
for notificationInfo := range eventsCh {
if notificationInfo.Err != nil {
if nErr, ok := notificationInfo.Err.(minio.ErrorResponse); ok && nErr.Code == "APINotSupported" {
errorChan <- errors.New("API Not Supported")
return
}
errorChan <- notificationInfo.Err
}
for _, record := range notificationInfo.Records {
//bucketName := record.S3.Bucket.Name
key, e := url.QueryUnescape(record.S3.Object.Key)
if e != nil {
errorChan <- e
continue
}
objectPath := key
folder := false
var additionalCreate string
if strings.HasSuffix(key, servicescommon.PYDIO_SYNC_HIDDEN_FILE_META) {
additionalCreate = objectPath
objectPath = path.Dir(key)
folder = true
}
if c.isIgnoredFile(objectPath, record) {
continue
}
objectPath = c.getLocalPath(objectPath)
if strings.HasPrefix(record.EventName, "s3:ObjectCreated:") {
log.Logger(c.globalContext).Debug("S3 Event", zap.String("event", "ObjectCreated"), zap.Any("event", record))
eventChan <- model.EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Etag: record.S3.Object.ETag,
Path: objectPath,
Folder: folder,
Source: c,
Type: model.EventCreate,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Metadata: stripCloseParameters(additionalCreate != "", record.RequestParameters),
}
if additionalCreate != "" {
// Send also the PYDIO_SYNC_HIDDEN_FILE_META event
log.Logger(c.globalContext).Debug("S3 Event", zap.String("event", "ObjectCreated"), zap.String("path", additionalCreate))
eventChan <- model.EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Etag: record.S3.Object.ETag,
Path: additionalCreate,
Folder: false,
Source: c,
Type: model.EventCreate,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Metadata: record.RequestParameters,
}
}
} else if strings.HasPrefix(record.EventName, "s3:ObjectRemoved:") {
log.Logger(c.globalContext).Debug("S3 Event", zap.String("event", "ObjectRemoved"), zap.String("path", objectPath))
eventChan <- model.EventInfo{
Time: record.EventTime,
Path: objectPath,
Folder: folder,
Source: c,
Type: model.EventRemove,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Metadata: stripCloseParameters(additionalCreate != "", record.RequestParameters),
}
if additionalCreate != "" {
log.Logger(c.globalContext).Debug("S3 Event", zap.String("event", "ObjectRemoved"), zap.String("path", additionalCreate))
eventChan <- model.EventInfo{
Time: record.EventTime,
Path: additionalCreate,
Folder: false,
Source: c,
Type: model.EventRemove,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Metadata: record.RequestParameters,
}
}
} else if record.EventName == minio.ObjectAccessedGet {
eventChan <- model.EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Etag: record.S3.Object.ETag,
Path: objectPath,
Source: c,
Type: model.EventAccessedRead,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Metadata: record.RequestParameters,
}
} else if record.EventName == minio.ObjectAccessedHead {
eventChan <- model.EventInfo{
Time: record.EventTime,
Size: record.S3.Object.Size,
Etag: record.S3.Object.ETag,
Path: objectPath,
Source: c,
Type: model.EventAccessedStat,
Host: record.Source.Host,
Port: record.Source.Port,
UserAgent: record.Source.UserAgent,
Metadata: record.RequestParameters,
}
}
}
}
}()
return wo, nil
}
func stripCloseParameters(do bool, params map[string]string) map[string]string {
if !do {
return params
}
newParams := make(map[string]string, len(params))
for k, v := range params {
if k == servicescommon.XPydioSessionUuid && strings.HasPrefix(v, servicescommon.SyncSessionClose_) {
newParams[k] = strings.TrimPrefix(v, servicescommon.SyncSessionClose_)
} else {
newParams[k] = v
}
}
return newParams
}
func (c *Client) isIgnoredFile(path string, record ...minio.NotificationEvent) bool {
if len(record) > 0 && strings.Contains(record[0].Source.UserAgent, UserAgentAppName) {
return true
}
/*
if model.IsIgnoredFile(path) {
return true
}
*/
return false
}