-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmanager.go
152 lines (136 loc) · 4.08 KB
/
manager.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
package main
import (
"bytes"
"errors"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"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"
log "github.com/sirupsen/logrus"
"os"
)
type Manager struct {
config *Config
downloadFile []byte
uploadFile []byte
entry *log.Entry
}
// NewManager -
func NewManager(config *Config) (*Manager, error) {
download, err := os.ReadFile(config.S3.DownloadFilePath)
if err != nil {
return nil, fmt.Errorf("unable read configured download file from path '%s': %s", config.S3.DownloadFilePath, err)
}
upload, err := os.ReadFile(config.S3.UploadFilePath)
if err != nil {
return nil, fmt.Errorf("unable read configured upload file from path '%s': %s", config.S3.UploadFilePath, err)
}
return &Manager{
config: config,
downloadFile: download,
uploadFile: upload,
entry: log.WithFields(log.Fields{
"url": config.S3.URL,
"bucket": config.S3.Bucket,
}),
}, nil
}
func (m *Manager) newSession() (*session.Session, error) {
m.entry.Debugf("creating new session")
config := aws.NewConfig()
config.WithCredentials(credentials.NewStaticCredentials(
m.config.S3.APIKey,
m.config.S3.APISecret,
"",
))
config.WithRegion(m.config.S3.Region)
config.WithEndpoint(m.config.S3.URL)
config.WithMaxRetries(3)
return session.NewSession(config)
}
func (m *Manager) Download() error {
newSession, err := m.newSession()
if err != nil {
m.entry.Errorf("unable to create session: %s", err.Error())
return fmt.Errorf("unable to create session: %s", err)
}
buffer := []byte{}
memWriter := aws.NewWriteAtBuffer(buffer)
downloader := s3manager.NewDownloader(newSession)
_, err = downloader.Download(memWriter,
&s3.GetObjectInput{
Bucket: aws.String(m.config.S3.Bucket),
Key: aws.String(m.config.S3.DownloadKey),
})
if err != nil {
m.entry.Errorf("unable to download file: %s", err.Error())
return fmt.Errorf("unable to download file: %s", err)
}
if !bytes.Equal(m.downloadFile, memWriter.Bytes()) {
m.entry.Errorf("downloaded file content mismatch")
return errors.New("downloaded file content mismatch")
}
return nil
}
func (m *Manager) Upload() error {
newSession, err := m.newSession()
if err != nil {
m.entry.Errorf("unable to create session: %s", err.Error())
return fmt.Errorf("unable to create session: %s", err)
}
reader := bytes.NewReader(m.uploadFile)
uploader := s3manager.NewUploader(newSession)
_, err = uploader.Upload(&s3manager.UploadInput{
Body: reader,
Bucket: aws.String(m.config.S3.Bucket),
Key: aws.String(m.config.S3.UploadKey),
})
if err != nil {
m.entry.Errorf("unable to upload file: %s", err.Error())
return fmt.Errorf("unable to upload file: %s", err)
}
return nil
}
func (m *Manager) FirstRun() error {
newSession, err := m.newSession()
if err != nil {
return fmt.Errorf("unable to create session: %s", err)
}
client := s3.New(newSession)
m.entry.Infof("creating bucket '%s'", m.config.S3.Bucket)
_, err = client.CreateBucket(
&s3.CreateBucketInput{
Bucket: aws.String(m.config.S3.Bucket),
CreateBucketConfiguration: &s3.CreateBucketConfiguration{
LocationConstraint: aws.String(m.config.S3.Region),
},
},
)
if err != nil {
var aerr awserr.Error
if errors.As(err, &aerr) {
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
case s3.ErrCodeBucketAlreadyOwnedByYou:
m.entry.Warnf("bucket already exists: %s", err.Error())
default:
return fmt.Errorf("unable to create bucket '%s': %s", m.config.S3.Bucket, err)
}
}
}
reader := bytes.NewReader(m.downloadFile)
uploader := s3manager.NewUploader(newSession)
m.entry.Infof("uploading initial file '%s' from '%s'", m.config.S3.DownloadKey, m.config.S3.DownloadFilePath)
_, err = uploader.Upload(&s3manager.UploadInput{
Body: reader,
Bucket: aws.String(m.config.S3.Bucket),
Key: aws.String(m.config.S3.DownloadKey),
})
if err != nil {
return fmt.Errorf("unable to upload initial file: %s", err)
}
return nil
}