-
Notifications
You must be signed in to change notification settings - Fork 597
/
reader.go
191 lines (158 loc) · 3.86 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
package manager
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"strconv"
"strings"
"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"
)
// ObjectReader is an io.Reader implementation for an S3 Object
type ObjectReader struct {
ctx context.Context
s3 DownloadAPIClient
rd *io.PipeReader
chunkSize int64
concurrency int
clientOptions []func(*s3.Options)
partRetries int
input *s3.GetObjectInput
}
// NewReader returns a new ObjectReader
func (d *Downloader) NewReader(ctx context.Context, input *s3.GetObjectInput) io.Reader {
rd := &ObjectReader{
ctx: ctx,
s3: d.S3,
input: input,
chunkSize: d.PartSize,
concurrency: d.Concurrency,
partRetries: d.PartBodyMaxRetries,
clientOptions: d.ClientOptions,
}
return rd
}
// 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.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
// Get total Content length
res, err := r.s3.GetObject(ctx, &s3.GetObjectInput{
Bucket: r.input.Bucket,
Key: r.input.Key,
Range: aws.String("bytes=0-0"),
}, r.clientOptions...)
if err != nil {
var apiError smithy.APIError
if errors.As(err, &apiError) {
switch apiError.(type) {
case *types.NotFound:
rd.CloseWithError(io.EOF)
return fs.ErrNotExist
default:
return apiError
}
}
}
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 err
}
}
contentLen = total
}
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)
}
}
}
func (r *ObjectReader) getObject(ctx context.Context, start, end int64) (*s3.GetObjectOutput, error) {
byteRange := fmt.Sprintf("bytes=%d-%d", start, end)
copyInput := *r.input
copyInput.Range = &byteRange
var output *s3.GetObjectOutput
var err error
for i := 0; i < r.partRetries; i++ {
output, err = r.s3.GetObject(ctx, ©Input, r.clientOptions...)
if err == nil {
return output, nil
}
}
return output, err
}