-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcs.go
190 lines (173 loc) · 5.58 KB
/
gcs.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
package filestore
import (
"context"
"fmt"
"io"
"strings"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
type GCSStorage struct {
client *storage.Client
bucket *storage.BucketHandle
}
func NewGCSStorage(ctx context.Context, bucketName, serviceAccountKeyFile string) (*GCSStorage, error) {
client, err := storage.NewClient(ctx, option.WithCredentialsFile(serviceAccountKeyFile))
if err != nil {
return nil, fmt.Errorf("failed to create storage client: %w", err)
}
return &GCSStorage{
client: client,
bucket: client.Bucket(bucketName),
}, nil
}
func (s *GCSStorage) List(ctx context.Context, prefix string) ([]FileStoreItem, error) {
it := s.bucket.Objects(ctx, &storage.Query{Prefix: prefix})
items := []FileStoreItem{}
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf("error iterating over GCS objects: %w", err)
}
item := FileStoreItem{
Directory: strings.HasSuffix(attrs.Name, "/"),
Name: attrs.Name,
Path: attrs.Name,
URL: attrs.MediaLink,
Created: attrs.Created.Unix(),
Size: attrs.Size,
}
items = append(items, item)
}
return items, nil
}
func (s *GCSStorage) Get(ctx context.Context, path string) (FileStoreItem, error) {
attrs, err := s.bucket.Object(path).Attrs(ctx)
if err != nil {
return FileStoreItem{}, fmt.Errorf("error fetching GCS object attributes: %w", err)
}
return FileStoreItem{
Directory: strings.HasSuffix(attrs.Name, "/"),
Name: attrs.Name,
Path: attrs.Name,
URL: attrs.MediaLink,
Created: attrs.Created.Unix(),
Size: attrs.Size,
}, nil
}
func (s *GCSStorage) Upload(ctx context.Context, path string, r io.Reader) (FileStoreItem, error) {
obj := s.bucket.Object(path)
writer := obj.NewWriter(ctx)
if _, err := io.Copy(writer, r); err != nil {
return FileStoreItem{}, fmt.Errorf("failed to copy content to GCS: %w", err)
}
if err := writer.Close(); err != nil {
return FileStoreItem{}, fmt.Errorf("failed to finalize GCS object upload: %w", err)
}
attrs, err := obj.Attrs(ctx)
if err != nil {
return FileStoreItem{}, fmt.Errorf("error fetching GCS object attributes after upload: %w", err)
}
return FileStoreItem{
Directory: strings.HasSuffix(attrs.Name, "/"),
Name: attrs.Name,
Path: attrs.Name,
URL: attrs.MediaLink,
Created: attrs.Created.Unix(),
Size: attrs.Size,
}, nil
}
func (s *GCSStorage) Rename(ctx context.Context, path string, newPath string) (FileStoreItem, error) {
src := s.bucket.Object(path)
dst := s.bucket.Object(newPath)
// For directories, iterate over each item and copy then delete
if strings.HasSuffix(path, "/") {
it := s.bucket.Objects(ctx, &storage.Query{Prefix: path})
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return FileStoreItem{}, fmt.Errorf("error iterating over GCS objects during rename: %w", err)
}
newObjPath := strings.Replace(attrs.Name, path, newPath, 1)
_, err = s.bucket.Object(newObjPath).CopierFrom(src).Run(ctx)
if err != nil {
return FileStoreItem{}, fmt.Errorf("error copying GCS object during rename: %w", err)
}
if err := src.Delete(ctx); err != nil {
return FileStoreItem{}, fmt.Errorf("error deleting original GCS object post rename: %w", err)
}
}
} else { // For single objects
if _, err := dst.CopierFrom(src).Run(ctx); err != nil {
return FileStoreItem{}, fmt.Errorf("failed to rename GCS object: %w", err)
}
if err := src.Delete(ctx); err != nil {
return FileStoreItem{}, fmt.Errorf("failed to delete original GCS object after renaming: %w", err)
}
}
return s.Get(ctx, newPath)
}
func (s *GCSStorage) Delete(ctx context.Context, path string) error {
if strings.HasSuffix(path, "/") { // If it's a directory
it := s.bucket.Objects(ctx, &storage.Query{Prefix: path})
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return fmt.Errorf("error iterating over GCS objects during delete: %w", err)
}
if err := s.bucket.Object(attrs.Name).Delete(ctx); err != nil {
return fmt.Errorf("error deleting GCS object: %w", err)
}
}
} else { // For single objects
if err := s.bucket.Object(path).Delete(ctx); err != nil {
return fmt.Errorf("failed to delete GCS object: %w", err)
}
}
return nil
}
func (s *GCSStorage) CreateFolder(ctx context.Context, path string) (FileStoreItem, error) {
obj := s.bucket.Object(path + "/")
if _, err := obj.NewWriter(ctx).Write([]byte("")); err != nil {
// Check if the error is due to the folder already existing
if strings.Contains(err.Error(), "googleapi: Error 409: Conflict") {
attrs, err := obj.Attrs(ctx)
if err != nil {
return FileStoreItem{}, fmt.Errorf("error fetching GCS object attributes after folder creation: %w", err)
}
return FileStoreItem{
Directory: strings.HasSuffix(attrs.Name, "/"),
Name: attrs.Name,
Path: attrs.Name,
URL: attrs.MediaLink,
Created: attrs.Created.Unix(),
Size: attrs.Size,
}, nil
}
return FileStoreItem{}, fmt.Errorf("failed to create GCS folder: %w", err)
}
attrs, err := obj.Attrs(ctx)
if err != nil {
return FileStoreItem{}, fmt.Errorf("error fetching GCS object attributes after folder creation: %w", err)
}
return FileStoreItem{
Directory: strings.HasSuffix(attrs.Name, "/"),
Name: attrs.Name,
Path: attrs.Name,
URL: attrs.MediaLink,
Created: attrs.Created.Unix(),
Size: attrs.Size,
}, nil
}
// Compile-time interface check:
var _ FileStore = (*GCSStorage)(nil)