Skip to content

Commit 66c8356

Browse files
committed
Upload and Download to s3manager
1 parent f8bb66e commit 66c8356

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

pkg/skbn/s3.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212
"github.com/aws/aws-sdk-go/aws"
1313
"github.com/aws/aws-sdk-go/aws/session"
1414
"github.com/aws/aws-sdk-go/service/s3"
15+
"github.com/aws/aws-sdk-go/service/s3/s3manager"
1516
)
1617

1718
// GetClientToS3 checks the connection to S3 and returns the tested client
1819
func GetClientToS3(path string) (*session.Session, error) {
1920
pSplit := strings.Split(path, "/")
20-
bucket := pSplit[0]
21+
bucket, _ := initS3Variables(pSplit)
2122
attempts := 3
2223
attempt := 0
2324
for attempt < attempts {
@@ -57,8 +58,7 @@ func GetListOfFilesFromS3(iClient interface{}, path string) ([]string, error) {
5758
if err := validateS3Path(pSplit); err != nil {
5859
return nil, err
5960
}
60-
bucket := pSplit[0]
61-
pathToCopy := filepath.Join(pSplit[1:]...)
61+
bucket, s3Path := initS3Variables(pSplit)
6262

6363
attempts := 3
6464
attempt := 0
@@ -67,7 +67,7 @@ func GetListOfFilesFromS3(iClient interface{}, path string) ([]string, error) {
6767

6868
objectOutput, err := s3.New(s).ListObjects(&s3.ListObjectsInput{
6969
Bucket: aws.String(bucket),
70-
Prefix: aws.String(pathToCopy),
70+
Prefix: aws.String(s3Path),
7171
})
7272
if err != nil {
7373
if attempt == attempts {
@@ -80,7 +80,7 @@ func GetListOfFilesFromS3(iClient interface{}, path string) ([]string, error) {
8080
var outLines []string
8181
for _, content := range objectOutput.Contents {
8282
line := *content.Key
83-
outLines = append(outLines, strings.Replace(line, pathToCopy, "", 1))
83+
outLines = append(outLines, strings.Replace(line, s3Path, "", 1))
8484
}
8585

8686
return outLines, nil
@@ -96,18 +96,21 @@ func DownloadFromS3(iClient interface{}, path string) ([]byte, error) {
9696
if err := validateS3Path(pSplit); err != nil {
9797
return nil, err
9898
}
99-
bucket := pSplit[0]
100-
s3Path := filepath.Join(pSplit[1:]...)
99+
bucket, s3Path := initS3Variables(pSplit)
101100

102101
attempts := 3
103102
attempt := 0
104103
for attempt < attempts {
105104
attempt++
106105

107-
objectOutput, err := s3.New(s).GetObject(&s3.GetObjectInput{
108-
Bucket: aws.String(bucket),
109-
Key: aws.String(s3Path),
110-
})
106+
buffer := &aws.WriteAtBuffer{}
107+
downloader := s3manager.NewDownloader(s)
108+
109+
_, err := downloader.Download(buffer,
110+
&s3.GetObjectInput{
111+
Bucket: aws.String(bucket),
112+
Key: aws.String(s3Path),
113+
})
111114
if err != nil {
112115
if attempt == attempts {
113116
return nil, err
@@ -116,9 +119,7 @@ func DownloadFromS3(iClient interface{}, path string) ([]byte, error) {
116119
continue
117120
}
118121

119-
buffer := make([]byte, int(*objectOutput.ContentLength))
120-
objectOutput.Body.Read(buffer)
121-
return buffer, nil
122+
return buffer.Bytes(), nil
122123
}
123124

124125
return nil, nil
@@ -135,28 +136,28 @@ func UploadToS3(iClient interface{}, toPath, fromPath string, buffer []byte) err
135136
_, fileName := filepath.Split(fromPath)
136137
pSplit = append(pSplit, fileName)
137138
}
138-
bucket := pSplit[0]
139-
s3Path := filepath.Join(pSplit[1:]...)
139+
bucket, s3Path := initS3Variables(pSplit)
140140

141141
attempts := 3
142142
attempt := 0
143143
for attempt < attempts {
144144
attempt++
145145

146-
_, err := s3.New(s).PutObject(&s3.PutObjectInput{
146+
uploader := s3manager.NewUploader(s)
147+
148+
_, err := uploader.Upload(&s3manager.UploadInput{
147149
Bucket: aws.String(bucket),
148150
Key: aws.String(s3Path),
149151
Body: bytes.NewReader(buffer),
150152
})
151-
if attempt == attempts {
152-
if err != nil {
153+
if err != nil {
154+
if attempt == attempts {
153155
return err
154156
}
157+
utils.Sleep(attempt)
158+
continue
155159
}
156-
if err == nil {
157-
return nil
158-
}
159-
utils.Sleep(attempt)
160+
return nil
160161
}
161162

162163
return nil

0 commit comments

Comments
 (0)