-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.go
352 lines (284 loc) · 7.21 KB
/
reader.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package s3io
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"strconv"
"strings"
"sync/atomic"
"time"
"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/types"
"github.com/aws/smithy-go"
)
// ReadAll reads all the bytes for a given object
func ReadAll(ctx context.Context, s3 DownloadAPIClient, input *s3.GetObjectInput, opts ...ObjectReaderOption) ([]byte, error) {
rd := NewObjectReader(ctx, s3, input, opts...)
return io.ReadAll(rd)
}
// ObjectReader is an io.Reader implementation for an S3 Object
type ObjectReader struct {
ctx context.Context
s3 DownloadAPIClient
rd *io.PipeReader
logger *slog.Logger
chunkSize int64
concurrency int
retries int
clientOptions []func(*s3.Options)
input *s3.GetObjectInput
closed *atomic.Bool
}
// NewObjectReader returns a new ObjectReader to do io.Reader opperations on your s3 object
func NewObjectReader(ctx context.Context, s3 DownloadAPIClient, input *s3.GetObjectInput, opts ...ObjectReaderOption) io.Reader {
rd := &ObjectReader{
ctx: ctx,
s3: s3,
input: input,
retries: defaultRetries,
chunkSize: DefaultChunkSize,
concurrency: defaultConcurrency,
logger: noopLogger,
closed: &atomic.Bool{},
}
ObjectReaderOptions(opts...)(rd)
return rd
}
// ObjectReaderOption is an option for the given read operation.
type ObjectReaderOption func(*ObjectReader)
// ObjectReaderOptions is a collection of ObjectReaderOption's.
func ObjectReaderOptions(opts ...ObjectReaderOption) ObjectReaderOption {
return func(r *ObjectReader) {
for _, op := range opts {
op(r)
}
}
}
// Stat is the fs.File implementaion for the ObjectReader.
func (r *ObjectReader) Stat() (fs.FileInfo, error) {
if r.closed.Load() {
return nil, fs.ErrClosed
}
ctx := r.ctx
input := *r.input
input.Range = aws.String("bytes=0-0")
res, err := r.s3.GetObject(ctx, &input, r.clientOptions...)
if err != nil {
var apiError smithy.APIError
if errors.As(err, &apiError) {
switch apiError.(type) {
case *types.NotFound:
return nil, fs.ErrNotExist
default:
return nil, apiError
}
}
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
var contentLen int64
if res.ContentRange == nil {
if l := aws.ToInt64(res.ContentLength); l > 0 {
contentLen = l
}
} else {
parts := strings.Split(*res.ContentRange, "/")
total := int64(-1)
var err error
// Checking for whether or not a numbered total exists
// If one does not exist, we will assume the total to be -1, undefined,
// and sequentially download each chunk until hitting a 416 error
totalStr := parts[len(parts)-1]
if totalStr != "*" {
total, err = strconv.ParseInt(totalStr, 10, 64)
if err != nil {
return nil, err
}
}
contentLen = total
}
fi := fileInfo{
name: aws.ToString(r.input.Key),
size: contentLen,
lastUpdated: aws.ToTime(res.LastModified),
rd: r,
}
return fi, nil
}
// close is the io.Close implementation for the ObjectReader
func (r *ObjectReader) Close() error {
if r.closed.CompareAndSwap(false, true) && r.rd != nil {
r.rd.CloseWithError(io.EOF)
}
return nil
}
// Read is the io.Reader implementation for the ObjectReader.
//
// It returns an fs.ErrNotExists if the object doesn't exist in the given bucket.
// And returns an io.EOF when all bytes are read.
func (r *ObjectReader) Read(p []byte) (int, error) {
if r.closed.Load() {
return 0, fs.ErrClosed
}
if r.rd == nil {
if err := r.preRead(); err != nil {
return 0, err
}
}
c, err := r.rd.Read(p)
if err != nil && err == io.ErrClosedPipe {
err = fs.ErrClosed
}
return c, err
}
func (r *ObjectReader) preRead() error {
ctx := r.ctx
rd, wr := io.Pipe()
r.rd = rd
stats, err := r.Stat()
if err != nil {
closeErr := err
if errors.Is(err, fs.ErrNotExist) {
closeErr = io.EOF
}
defer rd.CloseWithError(closeErr)
return err
}
contentLen := stats.Size()
r.logger.Debug("pre read", slog.Int64("content-length", contentLen))
cl := newConcurrencyLock(r.concurrency)
nextLock := make(chan struct{}, 1)
go r.getChunk(ctx, wr, cl, nextLock, 0, contentLen)
defer close(nextLock)
return nil
}
func (r *ObjectReader) getChunk(
ctx context.Context,
wr *io.PipeWriter,
cl *concurrencyLock,
sequenceLock chan struct{},
start, contentLen int64,
) {
if start == contentLen+1 { // EOF
defer cl.Close()
select {
case <-ctx.Done():
case <-sequenceLock:
wr.CloseWithError(io.EOF)
}
return
}
cl.Lock()
defer cl.Unlock()
end := start + int64(r.chunkSize)
if end > contentLen {
end = contentLen
}
nextLock := make(chan struct{}, 1)
defer close(nextLock)
go r.getChunk(ctx, wr, cl, nextLock, end+1, contentLen)
res, err := r.getObject(ctx, start, end)
if err != nil {
wr.CloseWithError(err)
return
}
defer res.Body.Close()
select {
case <-ctx.Done():
return
case <-sequenceLock:
if _, err := io.Copy(wr, res.Body); err != nil && err != io.EOF {
wr.CloseWithError(err)
}
r.logger.DebugContext(ctx, "chunk read",
slog.Group("chunk", slog.Int64("start", start), slog.Int64("end", end), slog.Int64("content_lenght", contentLen)),
)
}
}
func (r *ObjectReader) getObject(ctx context.Context, start, end int64) (*s3.GetObjectOutput, error) {
r.logger.DebugContext(ctx, "getting chunk", slog.Group("chunk", slog.Int64("start", start), slog.Int64("end", end)))
byteRange := fmt.Sprintf("bytes=%d-%d", start, end)
input := *r.input
input.Range = &byteRange
var res *s3.GetObjectOutput
var err error
for range r.retries {
res, err = r.s3.GetObject(ctx, &input, r.clientOptions...)
if err == nil {
return res, err
}
}
return res, err
}
/*
* Options
*/
// WithReaderLogger sets the logger for this reader
func WithReaderLogger(logger *slog.Logger) ObjectReaderOption {
return func(r *ObjectReader) {
if logger != nil {
r.logger = logger
}
}
}
// WithReaderChunkSize sets the chunksize for this reader
func WithReaderChunkSize(size int64) ObjectReaderOption {
return func(r *ObjectReader) {
r.chunkSize = size
}
}
// WithReaderConcurrency set the concurency amount for this reader
func WithReaderConcurrency(i int) ObjectReaderOption {
return func(r *ObjectReader) {
if i < 1 {
i = 1
}
r.concurrency = i
}
}
// WithReaderRetries sets the retry count for this reader
func WithReaderRetries(i int) ObjectReaderOption {
return func(r *ObjectReader) {
if i < 1 {
i = 1
}
r.retries = i
}
}
// WithReaderS3Options adds s3 options to the reader opperations
func WithReaderS3Options(opts ...func(*s3.Options)) ObjectReaderOption {
return func(r *ObjectReader) {
r.clientOptions = append(r.clientOptions, opts...)
}
}
// fileInfo is the fs.FileInfo implenentation for the ObjectReader
type fileInfo struct {
name string
size int64
lastUpdated time.Time
rd *ObjectReader
}
func (f fileInfo) Name() string {
return f.name
}
func (f fileInfo) Size() int64 {
return f.size
}
func (f fileInfo) Mode() fs.FileMode {
return fs.ModePerm
}
func (f fileInfo) ModTime() time.Time {
return f.lastUpdated
}
func (f fileInfo) IsDir() bool {
return false
}
func (f fileInfo) Sys() any {
return f.rd
}