-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
150 lines (125 loc) · 3.73 KB
/
main.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
// SPDX-FileCopyrightText: 2018-2020 City of Espoo
//
// SPDX-License-Identifier: MIT
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/sirupsen/logrus"
)
// DownloadDetails of an S3 object to be downloaded
type DownloadDetails struct {
Bucket string
Key string
TargetFile string
}
// Shared logger to always include static fields
var log *logrus.Entry
func init() {
logrus.SetFormatter(&logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "@timestamp",
logrus.FieldKeyLevel: "logLevel",
logrus.FieldKeyMsg: "message",
},
})
log = logrus.WithFields(logrus.Fields{
"appBuild": getEnv("APP_BUILD", "local"),
"appCommit": getEnv("APP_COMMIT", "HEAD"),
"appName": os.Getenv("APP_NAME"),
"env": getEnv("VOLTTI_ENV", getEnv("ENV", "local")),
"hostIp": os.Getenv("HOST_IP"),
"type": "app-misc", // Voltti log type
"userIdHash": "", // Required field, should be empty here
"version": "1", // Version of the log format
})
}
func main() {
bucket, prefix, targetDir := readArgs(os.Args)
sess, err := session.NewSession()
svc := s3.New(sess)
resp, err := svc.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(bucket),
Prefix: aws.String(prefix),
})
check(err, "Unable to list objects in bucket: %v", bucket)
downloader := s3manager.NewDownloader(sess)
wg := sync.WaitGroup{}
for _, obj := range resp.Contents {
if strings.HasSuffix(*obj.Key, "/") {
// Skip folders
continue
}
wg.Add(1)
go downloadObject(downloader, &wg, &DownloadDetails{
Bucket: bucket,
Key: *obj.Key,
TargetFile: targetFilePath(*obj.Key, prefix, targetDir),
})
}
// Wait for all download to finish before exiting
wg.Wait()
}
func targetFilePath(key string, prefix string, targetDir string) string {
pathInDir := strings.TrimPrefix(key, prefix)
return filepath.Join(targetDir, pathInDir)
}
func readArgs(argv []string) (string, string, string) {
argc := len(argv)
if argc != 4 {
log.Error(fmt.Sprintf("Wrong number of arguments provided. Usage: %s bucket prefix targetDir", argv[0]))
os.Exit(1)
}
bucket := argv[1]
prefix := ensureSuffix(argv[2], "/")
targetDir := ensureSuffix(argv[3], "/")
return bucket, prefix, targetDir
}
func downloadObject(downloader *s3manager.Downloader, wg *sync.WaitGroup, details *DownloadDetails) {
defer wg.Done()
log.Info(fmt.Sprintf("Downloading object key: %v, file: %q", details.Key, details.TargetFile))
file := createFile(details.TargetFile)
defer file.Close()
_, err := downloader.Download(file, &s3.GetObjectInput{
Bucket: aws.String(details.Bucket),
Key: aws.String(details.Key),
})
check(err, "Unable to download item %q from %q to destination %q", details.Key, details.Bucket, details.TargetFile)
}
func createFile(targetFile string) *os.File {
dir := filepath.Dir(targetFile)
err := os.MkdirAll(dir, os.ModePerm)
check(err, "Unable to create directory: %v", dir)
file, err := os.Create(targetFile)
check(err, "Unable to open file: %q", targetFile)
return file
}
func check(err error, msg string, args ...interface{}) {
if err != nil {
log.WithFields(logrus.Fields{
"exception": "Error",
"stackTrace": err.Error(),
}).Error(fmt.Sprintf(msg, args...))
os.Exit(2)
}
}
func ensureSuffix(s string, suffix string) string {
if strings.HasSuffix(s, suffix) {
return s
}
return s + suffix
}
// Get environment variable value or default if not defined
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}