This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
cloudstorage_fs.go
262 lines (218 loc) · 6.05 KB
/
cloudstorage_fs.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
package storage
import (
"context"
"errors"
"fmt"
"io"
"sync"
"time"
gstorage "cloud.google.com/go/storage"
"golang.org/x/oauth2/google"
"google.golang.org/api/googleapi"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// NewCloudStorageFS creates a Google Cloud Storage FS
// credentials can be nil to use the default GOOGLE_APPLICATION_CREDENTIALS
func NewCloudStorageFS(bucket string, credentials *google.Credentials) FS {
return &cloudStorageFS{
bucketName: bucket,
credentials: credentials,
}
}
// cloudStorageFS implements FS and uses Google Cloud Storage as the underlying
// file storage.
type cloudStorageFS struct {
bucketName string
credentials *google.Credentials
bucketLock sync.RWMutex
bucket *gstorage.BucketHandle
bucketScopes Scope
}
func (c *cloudStorageFS) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error) {
if options == nil {
options = &SignedURLOptions{}
}
options.applyDefaults()
b, err := c.bucketHandle(ctx, ScopeSignURL)
if err != nil {
return "", err
}
return b.SignedURL(path, &gstorage.SignedURLOptions{
Method: options.Method,
Expires: time.Now().Add(options.Expiry),
})
}
// Open implements FS.
func (c *cloudStorageFS) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error) {
b, err := c.bucketHandle(ctx, ScopeRead)
if err != nil {
return nil, err
}
obj := b.Object(path)
if options != nil {
obj = obj.ReadCompressed(options.ReadCompressed)
}
f, err := obj.NewReader(ctx)
if err != nil {
if errors.Is(err, gstorage.ErrObjectNotExist) {
return nil, ¬ExistError{
Path: path,
}
}
return nil, err
}
return &File{
ReadCloser: f,
Attributes: Attributes{
ContentType: f.Attrs.ContentType,
ContentEncoding: f.Attrs.ContentEncoding,
ModTime: f.Attrs.LastModified,
Size: f.Attrs.Size,
},
}, nil
}
// Attributes implements FS.
func (c *cloudStorageFS) Attributes(ctx context.Context, path string, _ *ReaderOptions) (*Attributes, error) {
b, err := c.bucketHandle(ctx, ScopeRead)
if err != nil {
return nil, err
}
a, err := b.Object(path).Attrs(ctx)
if err != nil {
return nil, err
}
return &Attributes{
ContentType: a.ContentType,
ContentEncoding: a.ContentEncoding,
Metadata: a.Metadata,
ModTime: a.Updated,
CreationTime: a.Created,
Size: a.Size,
}, nil
}
// Create implements FS.
func (c *cloudStorageFS) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error) {
b, err := c.bucketHandle(ctx, ScopeWrite)
if err != nil {
return nil, err
}
w := b.Object(path).NewWriter(ctx)
if options != nil {
w.Metadata = options.Attributes.Metadata
w.ContentType = options.Attributes.ContentType
w.ContentEncoding = options.Attributes.ContentEncoding
w.ChunkSize = options.BufferSize
}
w.ChunkSize = c.chunkSize(w.ChunkSize)
return w, nil
}
func (c *cloudStorageFS) chunkSize(size int) int {
if size == 0 {
return googleapi.DefaultUploadChunkSize
} else if size > 0 {
return size
}
return 0 // disable buffering
}
// Delete implements FS.
func (c *cloudStorageFS) Delete(ctx context.Context, path string) error {
b, err := c.bucketHandle(ctx, ScopeDelete)
if err != nil {
return err
}
return b.Object(path).Delete(ctx)
}
// Walk implements FS.
func (c *cloudStorageFS) Walk(ctx context.Context, path string, fn WalkFn) error {
bh, err := c.bucketHandle(ctx, ScopeRead)
if err != nil {
return err
}
it := bh.Objects(ctx, &gstorage.Query{
Prefix: path,
})
for {
r, err := it.Next()
if errors.Is(err, iterator.Done) {
break
}
if err != nil {
// TODO(dhowden): Properly handle this error.
return err
}
if err = fn(r.Name); err != nil {
return err
}
}
return nil
}
func cloudStorageScope(scope Scope) string {
switch {
case scope.Has(ScopeDelete):
return gstorage.ScopeFullControl
case scope.Has(ScopeWrite):
return gstorage.ScopeReadWrite
case scope.Has(ScopeRead), scope.Has(ScopeSignURL):
return gstorage.ScopeReadOnly
default:
panic(fmt.Sprintf("unknown scope: '%s'", scope))
}
}
func ResolveCloudStorageScope(scope Scope) Scope {
switch cloudStorageScope(scope) {
case gstorage.ScopeFullControl:
return ScopeRWD | scope
case gstorage.ScopeReadWrite:
return ScopeRW | scope
case gstorage.ScopeReadOnly:
return ScopeRead | scope
default:
panic(fmt.Sprintf("unknown scope: '%s'", scope))
}
}
func (c *cloudStorageFS) findCredentials(ctx context.Context, scope string) (*google.Credentials, error) {
if c.credentials != nil {
return c.credentials, nil
}
return google.FindDefaultCredentials(ctx, scope)
}
func (c *cloudStorageFS) client(ctx context.Context, scope Scope) (*gstorage.Client, error) {
creds, err := c.findCredentials(ctx, cloudStorageScope(scope))
if err != nil {
return nil, fmt.Errorf("finding credentials: %w", err)
}
var options []option.ClientOption
options = append(options, option.WithCredentials(creds))
options = append(options, option.WithScopes(cloudStorageScope(scope)))
client, err := gstorage.NewClient(ctx, options...)
if err != nil {
return nil, fmt.Errorf("building client: %w", err)
}
return client, nil
}
func (c *cloudStorageFS) bucketHandle(ctx context.Context, scope Scope) (*gstorage.BucketHandle, error) {
c.bucketLock.RLock()
scope |= c.bucketScopes // Expand requested scope to encompass existing scopes
if bucket := c.bucket; bucket != nil && c.bucketScopes.Has(scope) {
c.bucketLock.RUnlock()
return bucket, nil
}
c.bucketLock.RUnlock()
c.bucketLock.Lock()
defer c.bucketLock.Unlock()
if c.bucket != nil && c.bucketScopes.Has(scope) { // Race condition
return c.bucket, nil
}
// Expand the requested scope to include the scopes that GCS would provide
// e.g. Requesting Write actually provides ReadWrite.
// Also include any scope that was previously used.
scope = ResolveCloudStorageScope(c.bucketScopes | scope)
client, err := c.client(ctx, scope)
if err != nil {
return nil, err
}
c.bucket = client.Bucket(c.bucketName)
c.bucketScopes = scope
return c.bucket, nil
}