/
aws_s3_blobstore.go
184 lines (152 loc) · 4.63 KB
/
aws_s3_blobstore.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
package client
import (
"errors"
"fmt"
"io"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/cloudfoundry/bosh-s3cli/config"
)
var errorInvalidCredentialsSourceValue = errors.New("the client operates in read only mode. Change 'credentials_source' parameter value ")
var oneTB = int64(1000 * 1024 * 1024 * 1024)
// awsS3Client encapsulates AWS S3 blobstore interactions
type awsS3Client struct {
s3Client *s3.S3
s3cliConfig *config.S3Cli
}
// Get fetches a blob, destination will be overwritten if exists
func (b *awsS3Client) Get(src string, dest io.WriterAt) error {
downloader := s3manager.NewDownloaderWithClient(b.s3Client)
_, err := downloader.Download(dest, &s3.GetObjectInput{
Bucket: aws.String(b.s3cliConfig.BucketName),
Key: b.key(src),
})
if err != nil {
return err
}
return nil
}
// Put uploads a blob
func (b *awsS3Client) Put(src io.ReadSeeker, dest string) error {
cfg := b.s3cliConfig
if cfg.CredentialsSource == config.NoneCredentialsSource {
return errorInvalidCredentialsSourceValue
}
uploader := s3manager.NewUploaderWithClient(b.s3Client, func(u *s3manager.Uploader) {
u.LeavePartsOnError = false
if !cfg.MultipartUpload {
// disable multipart uploads by way of large PartSize configuration
u.PartSize = oneTB
}
})
uploadInput := &s3manager.UploadInput{
Body: src,
Bucket: aws.String(cfg.BucketName),
Key: b.key(dest),
}
if cfg.ServerSideEncryption != "" {
uploadInput.ServerSideEncryption = aws.String(cfg.ServerSideEncryption)
}
if cfg.SSEKMSKeyID != "" {
uploadInput.SSEKMSKeyId = aws.String(cfg.SSEKMSKeyID)
}
retry := 0
maxRetries := 3
for {
putResult, err := uploader.Upload(uploadInput)
if err != nil {
if _, ok := err.(s3manager.MultiUploadFailure); ok {
if retry == maxRetries {
log.Println("Upload retry limit exceeded:", err.Error())
return fmt.Errorf("upload retry limit exceeded: %s", err.Error())
}
retry++
time.Sleep(time.Second * time.Duration(retry))
continue
}
log.Println("Upload failed:", err.Error())
return fmt.Errorf("upload failure: %s", err.Error())
}
log.Println("Successfully uploaded file to", putResult.Location)
return nil
}
}
// Delete removes a blob - no error is returned if the object does not exist
func (b *awsS3Client) Delete(dest string) error {
if b.s3cliConfig.CredentialsSource == config.NoneCredentialsSource {
return errorInvalidCredentialsSourceValue
}
deleteParams := &s3.DeleteObjectInput{
Bucket: aws.String(b.s3cliConfig.BucketName),
Key: b.key(dest),
}
_, err := b.s3Client.DeleteObject(deleteParams)
if err == nil {
return nil
}
if reqErr, ok := err.(awserr.RequestFailure); ok {
if reqErr.StatusCode() == 404 {
return nil
}
}
return err
}
// Exists checks if blob exists
func (b *awsS3Client) Exists(dest string) (bool, error) {
existsParams := &s3.HeadObjectInput{
Bucket: aws.String(b.s3cliConfig.BucketName),
Key: b.key(dest),
}
_, err := b.s3Client.HeadObject(existsParams)
if err == nil {
log.Printf("File '%s' exists in bucket '%s'\n", dest, b.s3cliConfig.BucketName)
return true, nil
}
if reqErr, ok := err.(awserr.RequestFailure); ok {
if reqErr.StatusCode() == 404 {
log.Printf("File '%s' does not exist in bucket '%s'\n", dest, b.s3cliConfig.BucketName)
return false, nil
}
}
return false, err
}
// Sign creates a presigned URL
func (b *awsS3Client) Sign(objectID string, action string, expiration time.Duration) (string, error) {
action = strings.ToUpper(action)
switch action {
case "GET":
return b.getSigned(objectID, expiration)
case "PUT":
return b.putSigned(objectID, expiration)
default:
return "", fmt.Errorf("action not implemented: %s", action)
}
}
func (b *awsS3Client) key(srcOrDest string) *string {
formattedKey := aws.String(srcOrDest)
if len(b.s3cliConfig.FolderName) != 0 {
formattedKey = aws.String(fmt.Sprintf("%s/%s", b.s3cliConfig.FolderName, srcOrDest))
}
return formattedKey
}
func (b *awsS3Client) getSigned(objectID string, expiration time.Duration) (string, error) {
signParams := &s3.GetObjectInput{
Bucket: aws.String(b.s3cliConfig.BucketName),
Key: b.key(objectID),
}
req, _ := b.s3Client.GetObjectRequest(signParams)
return req.Presign(expiration)
}
func (b *awsS3Client) putSigned(objectID string, expiration time.Duration) (string, error) {
signParams := &s3.PutObjectInput{
Bucket: aws.String(b.s3cliConfig.BucketName),
Key: b.key(objectID),
}
req, _ := b.s3Client.PutObjectRequest(signParams)
return req.Presign(expiration)
}