-
Notifications
You must be signed in to change notification settings - Fork 179
/
gcs.go
106 lines (87 loc) · 2.36 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
package gcs
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// GoogleBucket ...
type googleBucket struct {
Name string
}
// NewGoogleBucket ...
func NewGoogleBucket(bucketName string) *googleBucket {
return &googleBucket{
Name: bucketName,
}
}
// NewClient ...
func (g *googleBucket) NewClient(ctx context.Context) (*storage.Client, error) {
client, err := storage.NewClient(ctx, option.WithoutAuthentication())
if err != nil {
return nil, err
}
return client, nil
}
// GetFiles returns a list of file names within the Google bucket
func (g *googleBucket) GetFiles(ctx context.Context, client *storage.Client, prefix, delimiter string) ([]string, error) {
it := client.Bucket(g.Name).Objects(ctx, &storage.Query{
Prefix: prefix,
Delimiter: delimiter,
})
var files []string
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
files = append(files, attrs.Name)
}
return files, nil
}
// DownloadFile downloads a file from the bucket to a desination folder
func (g *googleBucket) DownloadFile(ctx context.Context, client *storage.Client, destination, source string) error {
// create dir of destination
dir := filepath.Dir(destination)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("error creating destination directory: %w", err)
}
download, err := client.Bucket(g.Name).Object(source).NewReader(ctx)
if err != nil {
return fmt.Errorf("error creating GCS object reader: %w", err)
}
defer download.Close()
file, err := os.Create(destination)
if err != nil {
return fmt.Errorf("error creating download file: %w", err)
}
defer file.Close()
_, err = io.Copy(file, download)
if err != nil {
return fmt.Errorf("error downloading file: %w", err)
}
return nil
}
// UploadFile uploads a file to the google bucket
func (g *googleBucket) UploadFile(ctx context.Context, client *storage.Client, destination, source string) error {
upload := client.Bucket(g.Name).Object(destination).NewWriter(ctx)
defer upload.Close()
file, err := os.Open(source)
if err != nil {
return fmt.Errorf("Error opening upload file: %w", err)
}
defer file.Close()
_, err = io.Copy(upload, file)
if err != nil {
return fmt.Errorf("Error uploading file: %w", err)
}
return nil
}