forked from benthosdev/benthos
/
cache_s3.go
228 lines (198 loc) · 5.17 KB
/
cache_s3.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package aws
import (
"bytes"
"context"
"io"
"sync"
"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/s3iface"
"github.com/cenkalti/backoff/v4"
"github.com/benthosdev/benthos/v4/internal/impl/aws/config"
"github.com/benthosdev/benthos/v4/public/service"
)
func s3CacheConfig() *service.ConfigSpec {
retriesDefaults := backoff.NewExponentialBackOff()
retriesDefaults.InitialInterval = time.Second
retriesDefaults.MaxInterval = time.Second * 5
retriesDefaults.MaxElapsedTime = time.Second * 30
spec := service.NewConfigSpec().
Stable().
Version("3.36.0").
Summary(`Stores each item in an S3 bucket as a file, where an item ID is the path of the item within the bucket.`).
Description(`It is not possible to atomically upload S3 objects exclusively when the target does not already exist, therefore this cache is not suitable for deduplication.`).
Field(service.NewStringField("bucket").
Description("The S3 bucket to store items in.")).
Field(service.NewStringField("content_type").
Description("The content type to set for each item.").
Default("application/octet-stream")).
Field(service.NewBoolField("force_path_style_urls").
Description("Forces the client API to use path style URLs, which helps when connecting to custom endpoints.").
Advanced().
Default(false)).
Field(service.NewBackOffField("retries", false, retriesDefaults).
Advanced())
for _, f := range config.SessionFields() {
spec = spec.Field(f)
}
return spec
}
func init() {
err := service.RegisterCache(
"aws_s3", s3CacheConfig(),
func(conf *service.ParsedConfig, mgr *service.Resources) (service.Cache, error) {
s, err := newS3CacheFromConfig(conf)
if err != nil {
return nil, err
}
return s, nil
})
if err != nil {
panic(err)
}
}
func newS3CacheFromConfig(conf *service.ParsedConfig) (*s3Cache, error) {
bucket, err := conf.FieldString("bucket")
if err != nil {
return nil, err
}
contentType, err := conf.FieldString("content_type")
if err != nil {
return nil, err
}
forcePathStyleURLs, err := conf.FieldBool("force_path_style_urls")
if err != nil {
return nil, err
}
sess, err := GetSession(conf, func(c *aws.Config) {
c.S3ForcePathStyle = aws.Bool(forcePathStyleURLs)
})
if err != nil {
return nil, err
}
client := s3.New(sess)
backOff, err := conf.FieldBackOff("retries")
if err != nil {
return nil, err
}
return newS3Cache(bucket, contentType, backOff, client), nil
}
//------------------------------------------------------------------------------
type s3Cache struct {
s3 s3iface.S3API
bucket string
contentType string
boffPool sync.Pool
}
func newS3Cache(bucket, contentType string, backOff *backoff.ExponentialBackOff, s3 s3iface.S3API) *s3Cache {
return &s3Cache{
s3: s3,
bucket: bucket,
contentType: contentType,
boffPool: sync.Pool{
New: func() any {
bo := *backOff
bo.Reset()
return &bo
},
},
}
}
//------------------------------------------------------------------------------
func (s *s3Cache) Get(ctx context.Context, key string) (body []byte, err error) {
boff := s.boffPool.Get().(backoff.BackOff)
defer func() {
boff.Reset()
s.boffPool.Put(boff)
}()
var obj *s3.GetObjectOutput
for {
if obj, err = s.s3.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: &s.bucket,
Key: &key,
}); err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == s3.ErrCodeNoSuchKey {
err = service.ErrKeyNotFound
return
}
} else {
body, err = io.ReadAll(obj.Body)
_ = obj.Body.Close()
return
}
wait := boff.NextBackOff()
if wait == backoff.Stop {
return
}
select {
case <-time.After(wait):
case <-ctx.Done():
return
}
}
}
// Set attempts to set the value of a key.
func (s *s3Cache) Set(ctx context.Context, key string, value []byte, _ *time.Duration) (err error) {
boff := s.boffPool.Get().(backoff.BackOff)
defer func() {
boff.Reset()
s.boffPool.Put(boff)
}()
for {
if _, err = s.s3.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: &s.bucket,
Key: &key,
Body: bytes.NewReader(value),
ContentType: &s.contentType,
}); err == nil {
return
}
wait := boff.NextBackOff()
if wait == backoff.Stop {
return
}
select {
case <-time.After(wait):
case <-ctx.Done():
return
}
}
}
func (s *s3Cache) Add(ctx context.Context, key string, value []byte, _ *time.Duration) error {
if _, err := s.s3.HeadObject(&s3.HeadObjectInput{
Bucket: &s.bucket,
Key: &key,
}); err == nil {
return service.ErrKeyAlreadyExists
}
return s.Set(ctx, key, value, nil)
}
func (s *s3Cache) Delete(ctx context.Context, key string) (err error) {
boff := s.boffPool.Get().(backoff.BackOff)
defer func() {
boff.Reset()
s.boffPool.Put(boff)
}()
for {
if _, err = s.s3.DeleteObjectWithContext(ctx, &s3.DeleteObjectInput{
Bucket: &s.bucket,
Key: &key,
}); err == nil {
return
}
wait := boff.NextBackOff()
if wait == backoff.Stop {
return
}
select {
case <-time.After(wait):
case <-ctx.Done():
return
}
}
}
func (s *s3Cache) Close(context.Context) error {
return nil
}