-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage.go
210 lines (181 loc) · 4.71 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
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
package main
import (
"errors"
"path"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
type storageEntry struct {
path string
}
func (e *storageEntry) Path() string {
return e.path
}
func (e *storageEntry) Name() string {
_, file := path.Split(e.path)
return file
}
func (e *storageEntry) String() string {
return e.Name()
}
type StorageDirectory struct {
storageEntry
}
func NewStorageDirectory(p string) *StorageDirectory {
return &StorageDirectory{
storageEntry{
path: p,
},
}
}
func ReverseSlice[T any](s []T) {
i := 0
j := len(s) - 1
for i < j {
s[i], s[j] = s[j], s[i]
i += 1
j -= 1
}
}
// Parents return a slice of all parent directories from the root.
// E.g. it returns [/, /a, /a/b] for /a/b.
func (e *StorageDirectory) Parents() []*StorageDirectory {
if e.path == "" {
// The root directory doesn't have any parents.
return nil
}
var dirs []*StorageDirectory
p := e.path
for idx := strings.LastIndexByte(p, '/'); idx != -1; idx = strings.LastIndexByte(p, '/') {
p = p[:idx]
dirs = append(dirs, NewStorageDirectory(p))
}
// Append root directory.
dirs = append(dirs, NewStorageDirectory(""))
ReverseSlice(dirs)
return dirs
}
type StorageFile struct {
storageEntry
Size int64
}
func NewStorageFile(p string, size int64) *StorageFile {
return &StorageFile{
storageEntry: storageEntry{
path: p,
},
Size: size,
}
}
// FriendlyName returns a user-friendly file name. The implementation just returns the name without extension.
func (e *StorageFile) FriendlyName() string {
name, _ := splitNameExt(e.Name())
return name
}
type S3Storage struct {
s3 *s3.S3
cfg S3Config
}
func NewS3Storage(cfg S3Config) (*S3Storage, error) {
awsConfig := aws.Config{
Region: cfg.Region,
Endpoint: cfg.Endpoint,
}
if cfg.Credentials != nil {
awsConfig.Credentials = credentials.NewStaticCredentials(cfg.Credentials.ID, cfg.Credentials.Secret, cfg.Credentials.Token)
}
if cfg.ForcePathStyle {
awsConfig.S3ForcePathStyle = aws.Bool(true)
}
sess, err := session.NewSession(&awsConfig)
if err != nil {
return nil, err
}
store := S3Storage{
s3: s3.New(sess),
cfg: cfg,
}
return &store, nil
}
// prefix returns an S3 prefix from a public user-provided path.
// prefix can be the entire key.
func (store *S3Storage) prefix(p string) string {
prefix := path.Join(store.cfg.BasePrefix, p)
return prefix
}
// path returns a public path exposed to the user from an internal S3 key.
func (store *S3Storage) path(key string) string {
return strings.TrimRight(
strings.TrimPrefix(key, store.cfg.BasePrefix),
Delimiter,
)
}
// List returns slices of directories and files under the given path.
func (store *S3Storage) List(p string) ([]*StorageDirectory, []*StorageFile, error) {
input := &s3.ListObjectsV2Input{
Bucket: aws.String(store.cfg.Bucket),
Delimiter: aws.String(Delimiter),
}
prefix := store.prefix(p)
if prefix != "" {
input.Prefix = aws.String(prefix + Delimiter)
}
var prefixes []*s3.CommonPrefix
var objects []*s3.Object
err := store.s3.ListObjectsV2Pages(input, func(page *s3.ListObjectsV2Output, lastPage bool) bool {
prefixes = append(prefixes, page.CommonPrefixes...)
for _, object := range page.Contents {
// Ignore empty objects used to emulate empty directories.
if *object.Size != 0 {
objects = append(objects, object)
}
}
return true
})
if err != nil {
return nil, nil, err
}
if len(prefixes) == 0 && len(objects) == 0 {
return nil, nil, errors.New("directory doesn't exist")
}
var dirs []*StorageDirectory
var files []*StorageFile
for _, prefix := range prefixes {
dirs = append(dirs, NewStorageDirectory(store.path(*prefix.Prefix)))
}
for _, object := range objects {
files = append(files, NewStorageFile(store.path(*object.Key), *object.Size))
}
return dirs, files, nil
}
// FileSize returns size of the file under the given path.
func (store *S3Storage) FileSize(p string) (int64, error) {
input := &s3.HeadObjectInput{
Bucket: aws.String(store.cfg.Bucket),
Key: aws.String(store.prefix(p)),
}
resp, err := store.s3.HeadObject(input)
if err != nil {
return 0, err
}
return *resp.ContentLength, nil
}
// FileContentURL returns a publicly accessible URL for the file under the given path.
func (store *S3Storage) FileContentURL(p string) (string, error) {
size, err := store.FileSize(p)
if err != nil {
return "", err
}
if size == 0 {
return "", errors.New("no content")
}
req, _ := store.s3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(store.cfg.Bucket),
Key: aws.String(store.prefix(p)),
})
return req.Presign(time.Duration(store.cfg.RequestPresignExpiry))
}