-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
158 lines (125 loc) · 3.13 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
package omloader
import (
"context"
"fmt"
"io"
"os"
"regexp"
"cloud.google.com/go/storage"
)
type Storage interface {
Read(path string) (io.ReadCloser, error)
Move(src, dst string) error
Delete(path string) error
io.Closer
}
type GoogleStorage interface {
Storage
RegisterFinalizeNotification(bucketName, projectID, topicID string) error
}
type googleStorage struct {
client *storage.Client
ctx context.Context
}
func NewGoogleStorage() (GoogleStorage, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return nil, err
}
return &googleStorage{
client: client,
ctx: ctx,
}, nil
}
func (s *googleStorage) Close() error {
return s.client.Close()
}
func (s *googleStorage) Read(path string) (io.ReadCloser, error) {
bucketName, objectName, err := parseURI(path)
if err != nil {
return nil, err
}
fmt.Println(bucketName, objectName)
bucket := s.client.Bucket(bucketName)
obj := bucket.Object(objectName)
return obj.NewReader(s.ctx)
}
func (s *googleStorage) Move(src, dst string) error {
srcBucketName, srcObjectName, err := parseURI(src)
if err != nil {
return err
}
dstBucketName, dstObjectName, err := parseURI(dst)
if err != nil {
return err
}
srcBucket := s.client.Bucket(srcBucketName)
srcObject := srcBucket.Object(srcObjectName)
dstBucket := s.client.Bucket(dstBucketName)
dstObject := dstBucket.Object(dstObjectName)
if _, err := dstObject.CopierFrom(srcObject).Run(s.ctx); err != nil {
return err
}
if err := srcObject.Delete(s.ctx); err != nil {
return err
}
return nil
}
func (s *googleStorage) Delete(path string) error {
bucketName, objectName, err := parseURI(path)
if err != nil {
return err
}
bucket := s.client.Bucket(bucketName)
object := bucket.Object(objectName)
if err := object.Delete(s.ctx); err != nil {
return err
}
return nil
}
func (s *googleStorage) RegisterFinalizeNotification(bucketName, projectID, topicID string) error {
bucket := s.client.Bucket(bucketName)
notifications, err := bucket.Notifications(s.ctx)
if err != nil {
return err
}
// Check if notification already exists.
for _, v := range notifications {
if v.TopicID == topicID && v.TopicProjectID == projectID {
return nil
}
}
_, err = bucket.AddNotification(s.ctx, &storage.Notification{
TopicProjectID: projectID,
TopicID: topicID,
EventTypes: []string{"OBJECT_FINALIZE"},
PayloadFormat: storage.JSONPayload,
})
return err
}
// parseURI parses a GCS URI into a bucket name and object name.
func parseURI(uri string) (string, string, error) {
re := regexp.MustCompile(`gs://(.*?)/(.*)`)
matches := re.FindStringSubmatch(uri)
if len(matches) != 3 {
return "", "", fmt.Errorf("invalid GCS URI: %s", uri)
}
return matches[1], matches[2], nil
}
type fileStorage struct{}
func NewFileStorage() Storage {
return &fileStorage{}
}
func (s *fileStorage) Close() error {
return nil
}
func (s *fileStorage) Read(path string) (io.ReadCloser, error) {
return os.Open(path)
}
func (s *fileStorage) Move(src, dst string) error {
return os.Rename(src, dst)
}
func (s *fileStorage) Delete(path string) error {
return os.Remove(path)
}