-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcs_bucket.go
71 lines (60 loc) · 2.3 KB
/
gcs_bucket.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
package mapping
import (
"context"
"encoding/json"
"fmt"
"io"
"cloud.google.com/go/storage"
"github.com/pkg/errors"
"google.golang.org/api/option"
)
type GcloudCreds struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKeyID string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientID string `json:"client_id"`
AuthURI string `json:"auth_uri"`
TokenURI string `json:"token_uri"`
AuthProvider string `json:"auth_provider_x509_cert_url"`
CertURL string `json:"client_x509_cert_url"`
}
type gcsBucketGetter struct {
objectGetter interface {
Object(name string) *storage.ObjectHandle
}
}
func NewGCSBucketGetter(gcreds *GcloudCreds, bucketName string) (*gcsBucketGetter, *storage.Client, error) {
credsB, err := json.Marshal(gcreds)
if err != nil {
return nil, nil, errors.Wrap(err, "json marshal")
}
client, err := storage.NewClient(context.Background(), option.WithCredentialsJSON(credsB))
if err != nil {
return nil, nil, errors.Wrap(err, "gcs storage client")
}
return &gcsBucketGetter{objectGetter: client.Bucket(bucketName)}, client, nil
}
func NewGCSBucketGetterFromEnvironment(bucketName string) (*gcsBucketGetter, *storage.Client, error) {
client, err := storage.NewClient(context.Background())
if err != nil {
return nil, nil, errors.Wrap(err, "gcs storage client")
}
return &gcsBucketGetter{objectGetter: client.Bucket(bucketName)}, client, nil
}
func NewGCSBucketGetterWithClient(client *storage.Client, bucketName string) (*gcsBucketGetter, error) {
return &gcsBucketGetter{objectGetter: client.Bucket(bucketName)}, nil
}
// GetStorageWriter returns the storage writer for google cloud storage.
func (r *gcsBucketGetter) GetStorageWriter(ctx context.Context, fileName string) io.WriteCloser {
return r.objectGetter.Object(fileName).NewWriter(ctx)
}
// GetStorageReader returns the storage reader for google cloud storage.
func (r *gcsBucketGetter) GetStorageReader(ctx context.Context, fileName string) (io.ReadCloser, error) {
return r.objectGetter.Object(fileName).NewReader(ctx)
}
func getMappingFilename(eventFamily string, version string, environment string) string {
// event_family/v1.0.0.json
return fmt.Sprintf("%s/%s/%s.json", eventFamily, environment, version)
}