forked from maorfr/skbn
/
s3.go
206 lines (171 loc) · 4.33 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package skbn
import (
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/amerello/skbn/pkg/utils"
"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"
)
// GetClientToS3 checks the connection to S3 and returns the tested client
func GetClientToS3(path string) (*session.Session, error) {
pSplit := strings.Split(path, "/")
bucket, _ := initS3Variables(pSplit)
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
s, err := getNewSession()
if err != nil {
if attempt == attempts {
return nil, err
}
utils.Sleep(attempt)
continue
}
_, err = s3.New(s).ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(bucket),
MaxKeys: aws.Int64(0),
})
if attempt == attempts {
if err != nil {
return nil, err
}
}
if err == nil {
return s, nil
}
utils.Sleep(attempt)
}
return nil, nil
}
// GetListOfFilesFromS3 gets list of files in path from S3 (recursive)
func GetListOfFilesFromS3(iClient interface{}, path string) ([]string, error) {
s := iClient.(*session.Session)
pSplit := strings.Split(path, "/")
if err := validateS3Path(pSplit); err != nil {
return nil, err
}
bucket, s3Path := initS3Variables(pSplit)
var outLines []string
err := s3.New(s).ListObjectsPages(&s3.ListObjectsInput{
Bucket: aws.String(bucket),
Prefix: aws.String(s3Path),
}, func(p *s3.ListObjectsOutput, last bool) (shouldContinue bool) {
for _, obj := range p.Contents {
line := *obj.Key
outLines = append(outLines, strings.Replace(line, s3Path, "", 1))
}
return true
})
if err != nil {
return nil, err
}
return outLines, nil
}
// DownloadFromS3 downloads a single file from S3
func DownloadFromS3(iClient interface{}, path string, writer io.Writer) error {
s := iClient.(*session.Session)
pSplit := strings.Split(path, "/")
if err := validateS3Path(pSplit); err != nil {
return err
}
bucket, s3Path := initS3Variables(pSplit)
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
downloader := s3manager.NewDownloader(s)
downloader.Concurrency = 1 // support writerWrapper
_, err := downloader.Download(writerWrapper{writer},
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(s3Path),
})
if err != nil {
if attempt == attempts {
return err
}
utils.Sleep(attempt)
continue
}
return nil
}
return nil
}
type writerWrapper struct {
w io.Writer
}
func (ww writerWrapper) WriteAt(p []byte, off int64) (n int, err error) {
return ww.w.Write(p)
}
// UploadToS3 uploads a single file to S3
func UploadToS3(iClient interface{}, toPath, fromPath string, reader io.Reader) error {
s := iClient.(*session.Session)
pSplit := strings.Split(toPath, "/")
if err := validateS3Path(pSplit); err != nil {
return err
}
if len(pSplit) == 1 {
_, fileName := filepath.Split(fromPath)
pSplit = append(pSplit, fileName)
}
bucket, s3Path := initS3Variables(pSplit)
attempts := 3
attempt := 0
for attempt < attempts {
attempt++
uploader := s3manager.NewUploader(s)
_, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(s3Path),
Body: reader,
})
if err != nil {
if attempt == attempts {
return err
}
utils.Sleep(attempt)
continue
}
return nil
}
return nil
}
func getNewSession() (*session.Session, error) {
awsConfig := &aws.Config{}
region := "eu-central-1"
if rg := os.Getenv("AWS_REGION"); rg != "" {
region = rg
}
awsConfig.Region = aws.String(region)
if endpoint := os.Getenv("AWS_S3_ENDPOINT"); endpoint != "" {
awsConfig.Endpoint = aws.String(endpoint)
}
if disSSL := os.Getenv("AWS_S3_NO_SSL"); disSSL != "" {
disableSSL, _ := strconv.ParseBool(disSSL)
awsConfig.DisableSSL = aws.Bool(disableSSL)
}
if fps := os.Getenv("AWS_S3_FORCE_PATH_STYLE"); fps != "" {
forcePathStyle, _ := strconv.ParseBool(fps)
awsConfig.S3ForcePathStyle = aws.Bool(forcePathStyle)
}
s, err := session.NewSession(awsConfig)
return s, err
}
func validateS3Path(pathSplit []string) error {
if len(pathSplit) >= 1 {
return nil
}
return fmt.Errorf("illegal path: %s", filepath.Join(pathSplit...))
}
func initS3Variables(split []string) (string, string) {
bucket := split[0]
path := filepath.Join(split[1:]...)
return bucket, path
}