-
Notifications
You must be signed in to change notification settings - Fork 7
/
s3.go
159 lines (144 loc) · 4.5 KB
/
s3.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
package s3storage
import (
"archive/zip"
"context"
"io"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/lunarway/release-manager/internal/flow"
"github.com/lunarway/release-manager/internal/log"
"github.com/pkg/errors"
)
// downloadArtifact downloads an artifact from its AWS S3 key. The returned
// string is a file system path to a raw artifact, ie. unzipped.
func (f *Service) downloadArtifact(ctx context.Context, key string) (string, func(context.Context), error) {
span, ctx := f.tracer.FromCtx(ctx, "s3storage.downloadArtifact")
defer span.Finish()
logger := log.WithContext(ctx)
logger.WithFields("key", key).Infof("Downloading artifact from S3 key '%s'", key)
zipDest, err := ioutil.TempFile("", "s3-artifact-zip")
if err != nil {
return "", nil, errors.WithMessage(err, "create temp file for zip")
}
logger.Debugf("Zip destination: %s", zipDest.Name())
defer func() {
err := zipDest.Close()
if err != nil {
logger.Errorf("Failed to close zip destination file: %v", err)
}
err = os.Remove(zipDest.Name())
if err != nil {
logger.Errorf("Failed to remove zip destination file '%s': %v", zipDest.Name(), err)
}
}()
downloader := s3manager.NewDownloaderWithClient(f.s3client)
n, err := downloader.DownloadWithContext(ctx, zipDest, &s3.GetObjectInput{
Bucket: aws.String(f.bucketName),
Key: aws.String(key),
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == "NoSuchKey" {
return "", nil, flow.ErrArtifactNotFound
}
return "", nil, errors.WithMessage(err, "download object")
}
logger.Infof("Downloaded %d bytes", n)
destPath, closeSource, err := tempDir("s3-artifact-paths")
if err != nil {
return "", nil, errors.WithMessage(err, "get temp dir")
}
logger.Infof("Artifact destination: %s", destPath)
span, _ = f.tracer.FromCtx(ctx, "unzip artifact")
files, err := unzipFile(zipDest.Name(), destPath)
defer span.Finish()
if err != nil {
// manually close destination directory here as we must allow callers to
// access the directory and this cannot use defer
closeSource(ctx)
return "", nil, errors.WithMessage(err, "unzip file")
}
logger.WithFields("files", files).Infof("Artifact contains %d files", len(files))
return destPath, closeSource, nil
}
func tempDir(prefix string) (string, func(context.Context), error) {
path, err := ioutil.TempDir("", prefix)
if err != nil {
return "", func(context.Context) {}, err
}
return path, func(ctx context.Context) {
err := os.RemoveAll(path)
if err != nil {
log.WithContext(ctx).Errorf("Removing temporary directory failed: path '%s': %v", path, err)
}
}, nil
}
func unzipFile(src, destination string) (filenames []string, err error) {
var r *zip.ReadCloser
r, err = zip.OpenReader(src)
if err != nil {
err = errors.WithMessagef(err, "open zip '%s'", src)
return
}
defer checkClose(r, &err, "zip reader")
for _, f := range r.File {
var rc io.ReadCloser
rc, err = f.Open()
if err != nil {
err = errors.WithMessagef(err, "open file '%s'", f.Name)
return
}
defer checkClose(rc, &err, "source file")
var fpath string
fpath, err = securejoin.SecureJoin(destination, f.Name)
if err != nil {
err = errors.WithMessagef(err, "join destination path for file '%s'", f.Name)
return
}
if f.FileInfo().IsDir() {
err = os.MkdirAll(fpath, os.ModePerm)
if err != nil {
err = errors.WithMessagef(err, "create directory '%s'", fpath)
return
}
continue
}
if lastIndex := strings.LastIndex(fpath, string(os.PathSeparator)); lastIndex > -1 {
fdir := fpath[:lastIndex]
err = os.MkdirAll(fdir, os.ModePerm)
if err != nil {
err = errors.WithMessagef(err, "create directory '%s'", fdir)
return
}
}
var file *os.File
file, err = os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
err = errors.WithMessagef(err, "open file '%s'", fpath)
return
}
defer checkClose(file, &err, "destination file")
_, err = io.Copy(file, rc)
if err != nil {
err = errors.WithMessagef(err, "copy zip file '%s' to file '%s'", f.Name, fpath)
return
}
filenames = append(filenames, fpath)
}
return filenames, nil
}
func checkClose(c io.Closer, err *error, action string) {
cerr := c.Close()
if cerr != nil {
if err == nil {
err = &cerr
return
}
log.Errorf("unzipper: %s: close failed: %v", action, cerr)
}
}