/
gcs.go
180 lines (158 loc) · 4.16 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
package skbn
import (
"context"
"fmt"
"io"
"log"
"path/filepath"
"strings"
"github.com/maorfr/skbn/pkg/utils"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
// GetClientToGcs checks the connection to GCS and returns the tested client
func GetClientToGcs(ctx context.Context, path string) (*storage.Client, error) {
pSplit := strings.Split(path, "/")
bucketName, _ := initGcsVariables(pSplit)
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
client, err := storage.NewClient(ctx)
if err != nil {
if attempt == attempts {
return nil, err
}
utils.Sleep(attempt)
continue
}
bucket := client.Bucket(bucketName)
_, err = bucket.Attrs(ctx)
if err != nil {
if attempt == attempts {
return nil, err
}
}
if err == nil {
return client, nil
}
utils.Sleep(attempt)
}
log.Println("Could not get client to GCS")
return nil, nil
}
// GetListOfFilesFromGcs gets list of files in path from GCS (recursive)
func GetListOfFilesFromGcs(ctx context.Context, iClient interface{}, path string) ([]string, error) {
client := iClient.(*storage.Client)
pSplit := strings.Split(path, "/")
if err := validateGcsPath(pSplit); err != nil {
return nil, err
}
bucketName, gcsPath := initGcsVariables(pSplit)
var outLines []string
bucket := client.Bucket(bucketName)
objectIterator := bucket.Objects(ctx, &storage.Query{Prefix: gcsPath}) // gets all files and directories recursively
for objectAttrs, err := objectIterator.Next(); err != iterator.Done; objectAttrs, err = objectIterator.Next() {
if err != nil {
return nil, err
}
fileName := objectAttrs.Name
if !strings.HasSuffix(fileName, "/") { // don't append directories
outLines = append(outLines, strings.Replace(fileName, gcsPath, "", 1))
}
}
return outLines, nil
}
// DownloadFromGcs downloads a single file from GCS
func DownloadFromGcs(ctx context.Context, iClient interface{}, path string, writer io.Writer) error {
client := iClient.(*storage.Client)
pSplit := strings.Split(path, "/")
if err := validateGcsPath(pSplit); err != nil {
return err
}
bucketName, gcsPath := initGcsVariables(pSplit)
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
err := func() error {
reader, err := client.Bucket(bucketName).Object(gcsPath).NewReader(ctx)
defer func() {
if err := reader.Close(); err != nil {
log.Println("Error in reader.Close()", err)
}
}()
if err != nil { // no object found at [path]
return err
}
_, err = io.Copy(writer, reader)
if err != nil { // error other than EOF occurred
return err
}
return nil
}()
if err != nil {
if attempt == attempts {
return err
}
utils.Sleep(attempt)
continue
}
return nil
}
log.Println("Could not download file from GCS at", path)
return nil
}
// UploadToGCS uploads a single file to GCS
func UploadToGcs(ctx context.Context, iClient interface{}, toPath, fromPath string, reader io.Reader) error {
client := iClient.(*storage.Client)
pSplit := strings.Split(toPath, "/")
if err := validateGcsPath(pSplit); err != nil {
return err
}
if len(pSplit) == 1 {
_, fileName := filepath.Split(fromPath)
pSplit = append(pSplit, fileName)
}
bucketName, gcsPath := initGcsVariables(pSplit)
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
err := func() error {
writer := client.Bucket(bucketName).Object(gcsPath).NewWriter(ctx)
defer func() {
if err := writer.Close(); err != nil {
log.Println("Error in writer.Close()", err)
}
}()
_, err := io.Copy(writer, reader)
if err != nil { // error other than EOF occurred
return err
}
return nil
}()
if err != nil {
if attempt == attempts {
return err
}
utils.Sleep(attempt)
continue
}
return nil
}
log.Println("Could not upload file to GCS at", toPath)
return nil
}
func validateGcsPath(pathSplit []string) error {
if len(pathSplit) >= 1 {
return nil
}
return fmt.Errorf("illegal path: %s", filepath.Join(pathSplit...))
}
func initGcsVariables(split []string) (string, string) {
bucket := split[0]
path := filepath.Join(split[1:]...)
return bucket, path
}