-
Notifications
You must be signed in to change notification settings - Fork 0
/
inflight.go
165 lines (135 loc) · 3.93 KB
/
inflight.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
package inflight
import (
"bytes"
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/s3iface"
"github.com/cenkalti/backoff"
)
type (
// Bucket is a string value which represents the s3 bucket
Bucket string
// KeyPath is a string value which represents the s3 name space objects will be written to
KeyPath string
// ObjectKeyFunc is a function which generates the string to be used as the object key.
ObjectKeyFunc func([]byte) (string, error)
)
func defaultObjectKeyFunc(b []byte) (string, error) {
return fmt.Sprintf("%x", md5.Sum(b)), nil
}
// Ref represents the path to an object in S3 broken down by bucket, Path, and Object
// Bucket = "my-s3-bucket"
// Path = "some/path/within"
// Object = "an-object-in-s3.json"
// s3://my-s3-bucket/some/path/within/an-object-in-s3.json
type Ref struct {
Bucket string `json:"bucket"`
Path string `json:"path"`
Object string `json:"object"`
}
// Inflight is a structure which provides an interface to retrieving and writing data to s3,
// it doesn't care about what data you're writing, just provides an easy way to get to it
type Inflight struct {
s3iface.S3API
Bucket Bucket
KeyPath KeyPath
// ObjectKeyFunc will be called when Inflight#Write(io.ReadSeeker) is invoked.
// The data will be given the name that this function generates.
ObjectKeyFunc ObjectKeyFunc
}
// NewInflight Creates a reference to an Inflight struct
func NewInflight(bucket Bucket, keypath KeyPath, s3 s3iface.S3API) *Inflight {
return &Inflight{
Bucket: bucket,
KeyPath: keypath,
S3API: s3,
ObjectKeyFunc: ObjectKeyFunc(defaultObjectKeyFunc),
}
}
// Write will take the data given and attempt to put it in S3
// It then will return the S3 URI back to the caller so that the data may be passed between callers
func (i *Inflight) Write(data []byte) (ref *Ref, err error) {
objID, err := i.ObjectKeyFunc(data)
if err != nil {
return nil, backoff.Permanent(err)
}
ref = &Ref{
Bucket: string(i.Bucket),
Path: string(i.KeyPath),
Object: objID,
}
err = backoff.Retry(
i.tryWriteToS3(bytes.NewReader(data), ref.Object),
backoff.NewExponentialBackOff(),
)
if err != nil {
return nil, err
}
return ref, nil
}
func (i *Inflight) tryWriteToS3(data io.ReadSeeker, object string) func() error {
bucket := string(i.Bucket)
keyPath := string(i.KeyPath)
return func() error {
req := i.PutObjectRequest(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filepath.Join(keyPath, object)),
Body: data,
ContentType: aws.String("binary/octet-stream"),
})
_, err := req.Send()
if err != nil && aws.IsErrorRetryable(err) {
return err
} else if err != nil {
return backoff.Permanent(err)
}
return nil
}
}
type getter struct {
*Inflight
b []byte
}
// Get will retrieve the Object at the Bucket and KeyPath from S3.Get
// For instance, if you need the object at `cool-bucket/a/cool/key-path/the-object.json`
// you would say inflight::Get("the-object.json")
func (i *Inflight) Get(object string) ([]byte, error) {
g := &getter{Inflight: i}
err := backoff.Retry(
g.tryReadFromS3(object),
backoff.NewExponentialBackOff(),
)
if err != nil {
// returning []byte{} since user may attempt to marshall bytes
return []byte{}, err
}
return g.b, err
}
func (g *getter) tryReadFromS3(object string) func() error {
return func() error {
bucket := string(g.Bucket)
keyPath := string(g.KeyPath)
req := g.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(filepath.Join(keyPath, object)),
})
res, err := req.Send()
if err != nil && aws.IsErrorRetryable(err) {
return err
} else if err != nil {
return backoff.Permanent(err)
}
b, err := ioutil.ReadAll(res.Body)
if err != nil {
return backoff.Permanent(err)
}
defer res.Body.Close()
g.b = b
return nil
}
}