/
google.go
100 lines (76 loc) · 2.04 KB
/
google.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
package storage
import (
"context"
"errors"
"os"
"strings"
"google.golang.org/api/option"
log "github.com/sirupsen/logrus"
gstorage "cloud.google.com/go/storage"
)
type GoogleCloudStorage struct {
Storage
Options []option.ClientOption
client *gstorage.Client
bucket *gstorage.BucketHandle
bucketName string
}
func (gcs *GoogleCloudStorage) Setup() error {
bktName := os.Getenv("GOOGLE_STORAGE_BUCKET")
if bktName == "" {
return errors.New("GOOGLE_STORAGE_BUCKET env must be set")
}
gcs.bucketName = bktName
projectID := os.Getenv("GOOGLE_STORAGE_PROJECT_ID")
if projectID == "" {
return errors.New("GOOGLE_STORAGE_PROJECT_ID env must be set")
}
location := os.Getenv("GOOGLE_STORAGE_LOCATION")
if location == "" {
return errors.New("GOOGLE_STORAGE_LOCATION env must be set")
}
ctx := context.Background()
client, err := gstorage.NewClient(ctx, gcs.Options...)
if err != nil {
return err
}
gcs.client = client
bkt := client.Bucket(bktName)
attrs := &gstorage.BucketAttrs{Location: location}
err = bkt.Create(ctx, projectID, attrs)
if err == nil {
log.Printf("Created Google Cloud Storage bucket %s in %s",
bktName, location)
}
if err != nil {
if !strings.Contains(err.Error(), "You already own this bucket") {
return err
}
log.Printf("Using existing Google Cloud Storage bucket %v", bktName)
}
gcs.bucket = bkt
return nil
}
func (gcs *GoogleCloudStorage) PublicURL(filename string) string {
return "https://storage.googleapis.com/" + gcs.bucketName + "/" + filename
}
func (gcs *GoogleCloudStorage) Store(ctx context.Context, filename string, data []byte, metadata map[string]string) error {
o := gcs.bucket.Object(filename)
w := o.NewWriter(ctx)
w.ObjectAttrs = gstorage.ObjectAttrs{
Name: filename,
Metadata: metadata,
}
_, err := w.Write(data)
if err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
return nil
}
func (gcs *GoogleCloudStorage) Delete(ctx context.Context, filename string) error {
o := gcs.bucket.Object(filename)
return o.Delete(ctx)
}