forked from aws-samples/aws-lambda-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
89 lines (77 loc) · 2.23 KB
/
upload.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
package main
import (
"context"
"io"
"os"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/s3"
)
func searchForFilesAndUploadAndDelete(ctx context.Context, s3bucket string, rootDirectory string, substringToMatch string, creds credentials.Value) {
filesToUpload, err := getFilesWithSubstring(rootDirectory, substringToMatch)
if err != nil {
res, _ := extensionClient.ExitError(ctx, err.Error())
println(printPrefix, "ExitError response:", prettyPrint(res))
}
if len(filesToUpload) > 0 {
println(printPrefix, "Found", len(filesToUpload), "files to upload")
svc, err := createS3Client(creds)
if err != nil {
res, _ := extensionClient.ExitError(ctx, err.Error())
println(printPrefix, "ExitError response:", prettyPrint(res))
}
count := 0
for _, file := range filesToUpload {
err := uploadFileToS3(svc, s3bucket, file)
if err != nil {
res, _ := extensionClient.ExitError(ctx, err.Error())
println(printPrefix, "ExitError response:", prettyPrint(res))
} else {
count++
}
err = os.Remove(file)
if err != nil {
res, _ := extensionClient.ExitError(ctx, err.Error())
println(printPrefix, "ExitError response:", prettyPrint(res))
}
}
}
}
const maxPartSize int64 = 5 * 1024 * 1024 // 5MB
func uploadFileToS3(svc *s3.S3, s3bucket, filename string) error {
println(printPrefix, "Uploading", filename)
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
resp, err := createMultipartUpload(svc, s3bucket, filename)
if err != nil {
return err
}
buffer := make([]byte, maxPartSize)
var completedParts []*s3.CompletedPart
for {
_, err := file.Read(buffer)
if err != nil {
if err != io.EOF {
_ = abortMultipartUpload(svc, resp)
return err
}
break
}
completedPart, err := uploadPart(svc, resp, buffer, len(completedParts)+1)
if err != nil {
_ = abortMultipartUpload(svc, resp)
return err
}
completedParts = append(completedParts, completedPart)
}
completeResponse, err := completeMultipartUpload(svc, resp, completedParts)
if err != nil {
return err
}
println(completeResponse.String())
return nil
}