forked from kyma-project/kyma
/
minioclient.go
75 lines (61 loc) · 1.59 KB
/
minioclient.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
package storage
import (
"io"
"net/url"
"path/filepath"
"github.com/golang/glog"
"github.com/minio/minio-go"
"github.com/pkg/errors"
)
type minioClient struct {
client Minio
}
func newMinioClient(client Minio) *minioClient {
return &minioClient{
client: client,
}
}
func (mc *minioClient) Object(bucketName, objectName string) (io.Reader, error) {
return mc.client.GetObject(bucketName, objectName, minio.GetObjectOptions{})
}
func (mc *minioClient) IsNotExistsError(err error) bool {
switch err := err.(type) {
case minio.ErrorResponse:
return err.Code == "NoSuchKey"
default:
return false
}
}
func (mc *minioClient) NotificationChannel(bucketName string, stop <-chan struct{}) <-chan notification {
notificationChannel := make(chan notification)
channel := mc.client.ListenBucketNotification(bucketName, "", "", []string{
"s3:ObjectCreated:*",
"s3:ObjectRemoved:*",
}, stop)
go func() {
defer close(notificationChannel)
for info := range channel {
if info.Err != nil {
glog.Error(errors.Wrapf(info.Err, "while listening notifications on `%s` bucket", bucketName))
}
for _, record := range info.Records {
key, err := url.QueryUnescape(record.S3.Object.Key)
if err != nil {
glog.Warningf("Cannot parse object key: `%s`", record.S3.Object.Key)
continue
}
parent := filepath.Dir(key)
if parent == "." {
parent = ""
}
notification := notification{
parent: parent,
filename: filepath.Base(key),
eventType: record.EventName,
}
notificationChannel <- notification
}
}
}()
return notificationChannel
}