-
Notifications
You must be signed in to change notification settings - Fork 776
/
stowClient.go
264 lines (224 loc) · 7.09 KB
/
stowClient.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
/*
Copyright 2017 The Fission Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package storagesvc
import (
"fmt"
"io"
"mime/multipart"
"net/url"
"os"
"strings"
"time"
"github.com/graymeta/stow"
"github.com/pkg/errors"
"go.uber.org/zap"
)
type (
// StorageType contains all different types of supported storage
StorageType string
storageConfig struct {
storage Storage
}
//StowClient is the wrapper client for stow (Cloud storage abstraction package)
StowClient struct {
logger *zap.Logger
config *storageConfig
location stow.Location
container stow.Container
}
)
const (
// StorageTypeLocal is a constant to hold local storate type name literal
StorageTypeLocal StorageType = "local"
// StorageTypeS3 is a constant to hold S3 storage type name literal
StorageTypeS3 StorageType = "s3"
// PaginationSize is a constant to hold no of pages
PaginationSize int = 10
)
var (
ErrNotFound = errors.New("not found")
ErrRetrievingItem = errors.New("unable to retrieve item")
ErrOpeningItem = errors.New("unable to open item")
ErrWritingFile = errors.New("unable to write file")
ErrWritingFileIntoResponse = errors.New("unable to copy item into http response")
)
func getContainer(loc stow.Location, containerName string, cursor string) (stow.Container, error) {
// use location.Containers to find containers that match the prefix (container name)
cons, cursorNew, err := loc.Containers(containerName, cursor, 1)
if err != nil {
return nil, err
}
var con stow.Container
for _, v := range cons {
c, err := loc.Container(v.ID())
if err != nil {
return nil, err
}
if c.Name() == containerName {
con = cons[0]
break
}
}
if con == nil && !stow.IsCursorEnd(cursorNew) {
_, err := getContainer(loc, containerName, cursorNew)
if err != nil {
return nil, err
}
}
return con, nil
}
func getOrCreateContainer(loc stow.Location, containerName string, cursor string) (stow.Container, error) {
con, err := loc.CreateContainer(containerName)
if err != nil && (os.IsExist(err) || strings.Contains(err.Error(), "BucketAlreadyOwnedByYou")) {
con, err = getContainer(loc, containerName, stow.CursorStart)
}
if con == nil && err == nil {
err = fmt.Errorf("Storage container %s not found", containerName)
}
return con, err
}
// MakeStowClient create a new StowClient for given storage
func MakeStowClient(logger *zap.Logger, storage Storage) (*StowClient, error) {
storageType := getStorageType(storage)
if strings.Compare(storageType, "local") == 1 && strings.Compare(storageType, "s3") == 1 {
return nil, errors.New("Storage types other than 'local' and 's3' are not implemented")
}
config := &storageConfig{
storage: storage,
}
stowClient := &StowClient{
logger: logger.Named("stow_client"),
config: config,
}
loc, err := getStorageLocation(config)
if err != nil {
return nil, err
}
stowClient.location = loc
con, err := getOrCreateContainer(loc, config.storage.getContainerName(), stow.CursorStart)
if err != nil {
return nil, err
}
stowClient.container = con
return stowClient, nil
}
// putFile writes the file on the storage
func (client *StowClient) putFile(file multipart.File, fileSize int64) (string, error) {
uploadName, err := client.config.storage.getUploadFileName()
if err != nil {
return "", err
}
// save the file to the storage backend
item, err := client.container.Put(uploadName, file, fileSize, nil)
if err != nil {
client.logger.Error("error writing file on storage",
zap.Error(err),
zap.String("file", uploadName))
return "", ErrWritingFile
}
client.logger.Debug("successfully wrote file on storage", zap.String("file", uploadName))
return item.ID(), nil
}
// copyFileToStream gets the file contents into a stream
func (client *StowClient) copyFileToStream(fileId string, w io.Writer) error {
item, err := client.container.Item(fileId)
if err != nil {
if err == stow.ErrNotFound {
return ErrNotFound
} else {
return ErrRetrievingItem
}
}
f, err := item.Open()
if err != nil {
return ErrOpeningItem
}
defer f.Close()
_, err = io.Copy(w, f)
if err != nil {
return ErrWritingFileIntoResponse
}
client.logger.Debug("successfully wrote file into httpresponse", zap.String("file", fileId))
return nil
}
// removeFileByID deletes the file from storage
func (client *StowClient) removeFileByID(itemID string) error {
return client.container.RemoveItem(itemID)
}
func (client *StowClient) getURL(itemID string) (*url.URL, error) {
item, err := client.container.Item(itemID)
if err != nil {
if err == stow.ErrNotFound {
return nil, ErrNotFound
} else {
return nil, ErrRetrievingItem
}
}
return item.URL(), nil
}
func (client *StowClient) getFileSize(itemID string) (int64, error) {
item, err := client.container.Item(itemID)
if err != nil {
if err == stow.ErrNotFound {
return 0, ErrNotFound
} else {
return 0, ErrRetrievingItem
}
}
return item.Size()
}
// filter defines an interface to filter out items from a set of items
type filter func(stow.Item, interface{}) bool
// This method returns all items in a container, filtering out items based on the filter function passed to it
func (client *StowClient) getItemIDsWithFilter(filterFunc filter, filterFuncParam interface{}) ([]string, error) {
cursor := stow.CursorStart
var items []stow.Item
var err error
archiveIDList := make([]string, 0)
for {
items, cursor, err = client.container.Items(client.config.storage.getSubDir(), cursor, PaginationSize)
if err != nil {
return nil, errors.Wrap(err, "error getting items from container")
}
for _, item := range items {
isItemFilterable := filterFunc(item, filterFuncParam)
if isItemFilterable {
continue
}
archiveIDList = append(archiveIDList, item.ID())
}
if stow.IsCursorEnd(cursor) {
break
}
}
return archiveIDList, nil
}
// filterItemCreatedAMinuteAgo is one type of filter function that filters out items created less than a minute ago.
// More filter functions can be written if needed, as long as they are of type filter
func (client StowClient) filterItemCreatedAMinuteAgo(item stow.Item, currentTime interface{}) bool {
itemLastModTime, _ := item.LastMod()
if currentTime.(time.Time).Sub(itemLastModTime) < 1*time.Minute {
client.logger.Debug("item created less than a minute ago",
zap.String("item", item.ID()),
zap.Time("last_modified_time", itemLastModTime))
return true
}
return false
}
func (client StowClient) filterAllItems(item stow.Item, _ interface{}) bool {
itemLastModTime, _ := item.LastMod()
client.logger.Debug("item info",
zap.String("item", item.ID()),
zap.Time("last_modified_time", itemLastModTime))
return false
}