-
Notifications
You must be signed in to change notification settings - Fork 71
/
gcs.go
146 lines (119 loc) · 3.77 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2019 The Kubernetes Authors.
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 filepromoter
import (
"context"
"encoding/hex"
"fmt"
"hash/crc32"
"io"
"os"
"strings"
"cloud.google.com/go/storage"
"github.com/sirupsen/logrus"
"google.golang.org/api/iterator"
api "sigs.k8s.io/k8s-container-image-promoter/api/files"
"sigs.k8s.io/release-sdk/object"
)
type gcsSyncFilestore struct {
filestore *api.Filestore
client *storage.Client
bucket string
prefix string
}
// OpenReader opens an io.ReadCloser for the specified file.
func (s *gcsSyncFilestore) OpenReader(
ctx context.Context,
name string) (io.ReadCloser, error) {
absolutePath := s.prefix + name
return s.client.Bucket(s.bucket).Object(absolutePath).NewReader(ctx)
}
// UploadFile uploads a local file to the specified destination.
func (s *gcsSyncFilestore) UploadFile(ctx context.Context, dest, localFile string) error {
absolutePath := s.prefix + dest
gcsURL := object.GcsPrefix + s.bucket + "/" + absolutePath
in, err := os.Open(localFile)
if err != nil {
return fmt.Errorf("error opening %q: %v", localFile, err)
}
defer func() {
if err := in.Close(); err != nil {
logrus.Warnf("error closing %q: %v", localFile, err)
}
}()
// Compute crc32 checksum for upload integrity
var fileCRC32C uint32
{
hasher := crc32.New(crc32.MakeTable(crc32.Castagnoli))
if _, err := io.Copy(hasher, in); err != nil {
return fmt.Errorf("error computing crc32 checksum: %v", err)
}
fileCRC32C = hasher.Sum32()
if _, err := in.Seek(0, 0); err != nil {
return fmt.Errorf("error rewinding in file: %v", err)
}
}
logrus.Infof("uploading to %s", gcsURL)
w := s.client.Bucket(s.bucket).Object(absolutePath).NewWriter(ctx)
w.CRC32C = fileCRC32C
w.SendCRC32C = true
// Much bigger chunk size for faster uploading
w.ChunkSize = 128 * 1024 * 1024
if _, err := io.Copy(w, in); err != nil {
if err2 := w.Close(); err2 != nil {
logrus.Warnf("error closing upload stream: %v", err)
// TODO: Try to delete the possibly partially written file?
}
return fmt.Errorf("error uploading to %q: %v", gcsURL, err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("error uploading to %q: %v", gcsURL, err)
}
return nil
}
// ListFiles returns all the file artifacts in the filestore, recursively.
func (s *gcsSyncFilestore) ListFiles(
ctx context.Context) (map[string]*SyncFileInfo, error) {
files := make(map[string]*SyncFileInfo)
q := &storage.Query{Prefix: s.prefix}
logrus.Infof("listing files in bucket %s with prefix %q", s.bucket, s.prefix)
it := s.client.Bucket(s.bucket).Objects(ctx, q)
for {
obj, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf(
"error listing objects in %q: %v",
s.filestore.Base, err)
}
name := obj.Name
if !strings.HasPrefix(name, s.prefix) {
return nil, fmt.Errorf(
"found object %q without prefix %q",
name, s.prefix)
}
file := &SyncFileInfo{}
file.AbsolutePath = object.GcsPrefix + s.bucket + "/" + obj.Name
file.RelativePath = strings.TrimPrefix(name, s.prefix)
if obj.MD5 == nil {
return nil, fmt.Errorf("MD5 not set on file %q", file.AbsolutePath)
}
file.MD5 = hex.EncodeToString(obj.MD5)
file.Size = obj.Size
file.filestore = s
files[file.RelativePath] = file
}
return files, nil
}