-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
129 lines (118 loc) · 3.32 KB
/
aws.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
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/sirupsen/logrus"
)
var s3TemplateMap = map[string]s3Template{}
type s3Template struct {
S3FilePath string
TemplateName string
LastModified string
}
type S3ListObjectsAPI interface {
ListObjectsV2(ctx context.Context,
params *s3.ListObjectsV2Input,
optFns ...func(*s3.Options)) (*s3.ListObjectsV2Output, error)
}
func GetObjects(c context.Context, api S3ListObjectsAPI, input *s3.ListObjectsV2Input) (*s3.ListObjectsV2Output, error) {
return api.ListObjectsV2(c, input)
}
func listS3Objects(s3Path string) {
if !strings.HasPrefix(s3Path, "s3://") {
logrus.Error(fmt.Sprintf("%v is an invalid s3 path", s3Path))
return
}
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
logrus.Error(err.Error())
return
}
client := s3.NewFromConfig(cfg, func(o *s3.Options) {})
bucket := strings.Split(s3Path, "/")[2]
if bucket == "" {
logrus.Error(fmt.Sprintf("%v is an invalid s3 path", s3Path))
return
}
prefix := strings.Split(s3Path, fmt.Sprintf("s3://%v/", bucket))[1]
if prefix == "" {
logrus.Error(fmt.Sprintf("%v is an invalid s3 path", s3Path))
return
}
if !strings.HasSuffix(prefix, "/") {
prefix = fmt.Sprintf("%v/", prefix)
}
input := &s3.ListObjectsV2Input{
Bucket: String(bucket),
Prefix: String(prefix),
}
resp, err := GetObjects(context.TODO(), client, input)
if err != nil {
logrus.Error("AWS S3 Error. Error getting Objects")
logrus.Error(err.Error())
return
}
for _, item := range resp.Contents {
if item.Size != 0 {
checkS3File(bucket, *item.Key, fmt.Sprintf("%v", *item.LastModified))
}
}
}
func checkS3File(bucket string, key string, LastModified string) {
filePathSlice := strings.Split(key, "/")
templateName := filePathSlice[(len(filePathSlice) - 2)]
destFileName := filePathSlice[(len(filePathSlice) - 1)]
s3FilePath := fmt.Sprintf("%v/%v", bucket, key)
desiredS3Template := s3Template{
S3FilePath: s3FilePath,
TemplateName: templateName,
LastModified: LastModified,
}
if s3TemplateMap[s3FilePath] != desiredS3Template {
s3TemplateMap[s3FilePath] = desiredS3Template
downloadS3File(bucket, key, templateName, destFileName)
}
}
func downloadS3File(bucket string, key string, templateName string, destFileName string) {
dirName := fmt.Sprintf("%v/%v/", templatePath, templateName)
if !dirExists(dirName) {
os.MkdirAll(dirName, os.ModePerm)
}
destination := fmt.Sprintf("%v%v", dirName, destFileName)
f, err := os.Create(destination)
if err != nil {
logrus.Error(err.Error())
return
}
defer f.Close()
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
logrus.Error(err.Error())
return
}
client := s3.NewFromConfig(cfg, func(o *s3.Options) {})
object, err := client.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: String(bucket),
Key: String(key),
})
if err != nil {
logrus.Error(err.Error())
return
}
r := ioutil.NopCloser(object.Body)
buf := new(bytes.Buffer)
buf.ReadFrom(r)
r.Close()
_, err = f.Write(buf.Bytes())
if err != nil {
logrus.Error(fmt.Sprintf("Error writing local file : %v", destination))
logrus.Error(err.Error())
}
logrus.Info(fmt.Sprintf("%v file succesfully downloaded from s3 bucket %v", destination, bucket))
}