-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgcs.go
124 lines (113 loc) · 2.98 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
// Copyright (c) 2022 EPAM Systems, Inc.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package gcp
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
"github.com/epam/hubctl/cmd/hub/config"
)
var (
defaultGcsClient *storage.Client
gcsBuckets = make(map[string]*storage.BucketHandle)
gcsTimeout = time.Duration(30 * time.Second)
)
func gcsClient() (*storage.Client, error) {
if defaultGcsClient != nil {
return defaultGcsClient, nil
}
ctx := context.Background()
var err error
opts := []option.ClientOption{option.WithScopes(storage.ScopeReadWrite)}
if config.GcpCredentialsFile != "" {
opts = append(opts, option.WithCredentialsFile(config.GcpCredentialsFile))
}
defaultGcsClient, err = storage.NewClient(ctx, opts...)
return defaultGcsClient, err
}
func gcsBucket(name string) (*storage.BucketHandle, error) {
if bucket, exist := gcsBuckets[name]; exist {
return bucket, nil
}
client, err := gcsClient()
if err != nil {
return nil, err
}
bucket := client.Bucket(name)
gcsBuckets[name] = bucket
return bucket, nil
}
func noRoot(path string) string {
return strings.TrimLeft(path, "/")
}
func StatGCS(path string) (int64, time.Time, error) {
location, err := url.Parse(path)
if err != nil {
return 0, time.Time{}, err
}
bucket, err := gcsBucket(location.Host)
if err != nil {
return 0, time.Time{}, err
}
ctx, cancel := context.WithTimeout(context.Background(), gcsTimeout)
defer cancel()
attrs, err := bucket.Object(noRoot(location.Path)).Attrs(ctx)
if err != nil {
if IsNotFound(err) {
return 0, time.Time{}, os.ErrNotExist
}
return 0, time.Time{}, err
}
return attrs.Size, attrs.Updated, nil
}
func ReadGCS(path string) ([]byte, error) {
location, err := url.Parse(path)
if err != nil {
return nil, err
}
bucket, err := gcsBucket(location.Host)
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), gcsTimeout)
defer cancel()
reader, err := bucket.Object(noRoot(location.Path)).NewReader(ctx)
if err != nil {
return nil, err
}
defer reader.Close()
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, fmt.Errorf("Failed to read GCS object `%s`: %v", path, err)
}
return data, nil
}
func WriteGCS(path string, body []byte) error {
location, err := url.Parse(path)
if err != nil {
return err
}
bucket, err := gcsBucket(location.Host)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), gcsTimeout)
defer cancel()
writer := bucket.Object(noRoot(location.Path)).NewWriter(ctx)
defer writer.Close()
written, err := writer.Write(body)
if err != nil || written != len(body) {
return fmt.Errorf("Failed to write GCS object `%s` (wrote %d of %d bytes): %v",
path, written, len(body), err)
}
return nil
}