forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_gcs.go
230 lines (198 loc) · 5.11 KB
/
get_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package getter
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
)
// GCSGetter is a Getter implementation that will download a module from
// a GCS bucket.
type GCSGetter struct {
getter
// Timeout sets a deadline which all GCS operations should
// complete within. Zero value means no timeout.
Timeout time.Duration
// FileSizeLimit limits the size of an single
// decompressed file.
//
// The zero value means no limit.
FileSizeLimit int64
}
func (g *GCSGetter) ClientMode(u *url.URL) (ClientMode, error) {
ctx := g.Context()
if g.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, g.Timeout)
defer cancel()
}
// Parse URL
bucket, object, _, err := g.parseURL(u)
if err != nil {
return 0, err
}
client, err := g.getClient(ctx)
if err != nil {
return 0, err
}
iter := client.Bucket(bucket).Objects(ctx, &storage.Query{Prefix: object})
for {
obj, err := iter.Next()
if err != nil && err != iterator.Done {
return 0, err
}
if err == iterator.Done {
break
}
if strings.HasSuffix(obj.Name, "/") {
// A directory matched the prefix search, so this must be a directory
return ClientModeDir, nil
} else if obj.Name != object {
// A file matched the prefix search and doesn't have the same name
// as the query, so this must be a directory
return ClientModeDir, nil
}
}
// There are no directories or subdirectories, and if a match was returned,
// it was exactly equal to the prefix search. So return File mode
return ClientModeFile, nil
}
func (g *GCSGetter) Get(dst string, u *url.URL) error {
ctx := g.Context()
if g.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, g.Timeout)
defer cancel()
}
// Parse URL
bucket, object, _, err := g.parseURL(u)
if err != nil {
return err
}
// Remove destination if it already exists
_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
// Remove the destination
if err := os.RemoveAll(dst); err != nil {
return err
}
}
// Create all the parent directories
if err := os.MkdirAll(filepath.Dir(dst), g.client.mode(0755)); err != nil {
return err
}
client, err := g.getClient(ctx)
if err != nil {
return err
}
// Iterate through all matching objects.
iter := client.Bucket(bucket).Objects(ctx, &storage.Query{Prefix: object})
for {
obj, err := iter.Next()
if err != nil && err != iterator.Done {
return err
}
if err == iterator.Done {
break
}
if !strings.HasSuffix(obj.Name, "/") {
// Get the object destination path
objDst, err := filepath.Rel(object, obj.Name)
if err != nil {
return err
}
objDst = filepath.Join(dst, objDst)
// Download the matching object.
err = g.getObject(ctx, client, objDst, bucket, obj.Name, "")
if err != nil {
return err
}
}
}
return nil
}
func (g *GCSGetter) GetFile(dst string, u *url.URL) error {
ctx := g.Context()
if g.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, g.Timeout)
defer cancel()
}
// Parse URL
bucket, object, fragment, err := g.parseURL(u)
if err != nil {
return err
}
client, err := g.getClient(ctx)
if err != nil {
return err
}
return g.getObject(ctx, client, dst, bucket, object, fragment)
}
func (g *GCSGetter) getObject(ctx context.Context, client *storage.Client, dst, bucket, object, fragment string) error {
var rc *storage.Reader
var err error
if fragment != "" {
generation, err := strconv.ParseInt(fragment, 10, 64)
if err != nil {
return err
}
rc, err = client.Bucket(bucket).Object(object).Generation(generation).NewReader(ctx)
} else {
rc, err = client.Bucket(bucket).Object(object).NewReader(ctx)
}
if err != nil {
return err
}
defer rc.Close()
// Create all the parent directories
if err := os.MkdirAll(filepath.Dir(dst), g.client.mode(0755)); err != nil {
return err
}
// There is no limit set for the size of an object from GCS
return copyReader(dst, rc, 0666, g.client.umask(), 0)
}
func (g *GCSGetter) parseURL(u *url.URL) (bucket, path, fragment string, err error) {
if strings.Contains(u.Host, "googleapis.com") {
hostParts := strings.Split(u.Host, ".")
if len(hostParts) != 3 {
err = fmt.Errorf("URL is not a valid GCS URL")
return
}
pathParts := strings.SplitN(u.Path, "/", 5)
if len(pathParts) != 5 {
err = fmt.Errorf("URL is not a valid GCS URL")
return
}
bucket = pathParts[3]
path = pathParts[4]
fragment = u.Fragment
}
return
}
func (g *GCSGetter) getClient(ctx context.Context) (client *storage.Client, err error) {
var opts []option.ClientOption
if v, ok := os.LookupEnv("GOOGLE_OAUTH_ACCESS_TOKEN"); ok {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: v,
})
opts = append(opts, option.WithTokenSource(tokenSource))
}
newClient, err := storage.NewClient(ctx, opts...)
if err != nil {
return nil, err
}
return newClient, nil
}