forked from aws-samples/aws-lambda-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-aws-sdk.go
104 lines (92 loc) · 2.62 KB
/
use-aws-sdk.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
package main
import (
"bytes"
"errors"
"os"
"path"
"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"
)
func createS3Client(creds credentials.Value) (*s3.S3, error) {
// Create session and S3 client
s3region, s3regionFound := os.LookupEnv("AWS_REGION")
if !s3regionFound {
return nil, errors.New("AWS_REGION is not set")
}
sess, err := session.NewSession(
&aws.Config{
Region: aws.String(s3region),
Credentials: credentials.NewStaticCredentials(
creds.AccessKeyID,
creds.SecretAccessKey,
creds.SessionToken,
),
})
if err != nil {
return nil, err
}
svc := s3.New(sess)
return svc, nil
}
func createMultipartUpload(svc *s3.S3, s3bucket string, filename string) (*s3.CreateMultipartUploadOutput, error) {
input := &s3.CreateMultipartUploadInput{
Bucket: aws.String(s3bucket),
Key: aws.String(path.Base(filename)),
}
resp, err := svc.CreateMultipartUpload(input)
return resp, err
}
func uploadPart(svc *s3.S3, resp *s3.CreateMultipartUploadOutput, fileChunk []byte, partNumber int) (*s3.CompletedPart, error) {
partInput := &s3.UploadPartInput{
Body: bytes.NewReader(fileChunk),
Bucket: resp.Bucket,
Key: resp.Key,
PartNumber: aws.Int64(int64(partNumber)),
UploadId: resp.UploadId,
ContentLength: aws.Int64(int64(len(fileChunk))),
}
tryNum := 1
for {
uploadResult, err := svc.UploadPart(partInput)
if err != nil {
if tryNum >= 3 {
if aerr, ok := err.(awserr.Error); ok {
return nil, aerr
}
return nil, err
}
tryNum++
} else {
return &s3.CompletedPart{
ETag: uploadResult.ETag,
PartNumber: aws.Int64(int64(partNumber)),
}, nil
}
}
}
func abortMultipartUpload(svc *s3.S3, resp *s3.CreateMultipartUploadOutput) error {
abortInput := &s3.AbortMultipartUploadInput{
Bucket: resp.Bucket,
Key: resp.Key,
UploadId: resp.UploadId,
}
_, err := svc.AbortMultipartUpload(abortInput)
return err
}
func completeMultipartUpload(svc *s3.S3, resp *s3.CreateMultipartUploadOutput, completedParts []*s3.CompletedPart) (*s3.CompleteMultipartUploadOutput, error) {
completeInput := &s3.CompleteMultipartUploadInput{
Bucket: resp.Bucket,
Key: resp.Key,
UploadId: resp.UploadId,
MultipartUpload: &s3.CompletedMultipartUpload{
Parts: completedParts,
},
}
completedOutput, err := svc.CompleteMultipartUpload(completeInput)
return completedOutput, err
}