-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
74 lines (61 loc) · 1.5 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
package storage
import (
"context"
"errors"
)
type Backends string
func (s Backends) String() string {
return string(s)
}
const (
LocalBackends Backends = "file"
S3Backends Backends = "s3"
//GCSBackends Backends = "gcs" // To be implemented
)
var supportedBackendss = map[Backends]bool{
LocalBackends: true,
S3Backends: true,
//GCSBackends: true,
}
var ErrNotExists = errors.New("file not exists")
// Storage represents main interface for file storage as key/value database
type Storage interface {
Put(ctx context.Context, key string, data []byte) error
Get(ctx context.Context, key string) ([]byte, error)
GetMultiple(ctx context.Context, keys []string) ([]Object, error)
Exists(ctx context.Context, key string) (bool, error)
Delete(ctx context.Context, key string) error
List(ctx context.Context, prefix string) ([]Object, error)
Purge(ctx context.Context) error
}
type Object struct {
Key string `json:"key"`
Size int64 `json:"size"`
Data []byte `json:"data"`
}
func NewStorage(options ...Option) (Storage, error) {
opts := new(Options)
for _, opt := range options {
opt(opts)
}
err := opts.validateAndFix()
if err != nil {
return nil, err
}
var s Storage
switch opts.backends {
case S3Backends:
s, err = NewS3Storage(opts.s3opts)
if err != nil {
return nil, err
}
case LocalBackends:
s = NewFileStorage(opts.pathPrefix)
default:
return nil, ErrUnsupportedBackends
}
if opts.tracer != nil {
return NewTracingStorage(s, opts.tracer), nil
}
return s, nil
}