-
Notifications
You must be signed in to change notification settings - Fork 4
/
gcs.go
80 lines (69 loc) · 2.33 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
/*
Copyright 2017, 2018 Ankyra
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gcs
import (
"fmt"
"io"
"strings"
"cloud.google.com/go/storage"
"github.com/ankyra/escape-core/parsers"
"github.com/ankyra/escape-inventory/config"
"golang.org/x/net/context"
"google.golang.org/api/option"
)
type GoogleCloudStorageBackend struct {
BucketString string
Bucket *storage.BucketHandle
Client *storage.Client
Context context.Context
}
func NewGoogleCloudStorageBackend() *GoogleCloudStorageBackend {
return &GoogleCloudStorageBackend{}
}
func (ls *GoogleCloudStorageBackend) Init(settings config.StorageSettings) error {
if settings.Bucket == "" {
return fmt.Errorf("Missing storage_settings.bucket configuration variable")
}
ls.BucketString = settings.Bucket
ls.Context = context.Background()
var client *storage.Client
var err error
if settings.Credentials == "" {
client, err = storage.NewClient(ls.Context)
} else {
client, err = storage.NewClient(ls.Context, option.WithServiceAccountFile(settings.Credentials))
}
if err != nil {
return err
}
ls.Client = client
ls.Bucket = client.Bucket(ls.BucketString)
return nil
}
func (ls *GoogleCloudStorageBackend) Upload(namespace string, releaseId *parsers.ReleaseId, pkg io.ReadSeeker) (string, error) {
archive := strings.Join([]string{namespace, releaseId.Name, releaseId.ToString() + ".tgz"}, "/")
writer := ls.Bucket.Object(archive).NewWriter(ls.Context)
if _, err := io.Copy(writer, pkg); err != nil {
return "", err
}
if err := writer.Close(); err != nil {
return "", err
}
return "gcs://" + ls.BucketString + "/" + archive, nil
}
func (ls *GoogleCloudStorageBackend) Download(namespace, uri string) (io.Reader, error) {
path := uri[len("gcs://"):]
parts := strings.SplitN(path, "/", 2)
bucket := ls.Client.Bucket(parts[0])
return bucket.Object(parts[1]).NewReader(ls.Context)
}