forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 23
/
list_objects.go
267 lines (226 loc) · 6.05 KB
/
list_objects.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
package s3gof3r
import (
"context"
"encoding/xml"
"math"
"net/http"
"strconv"
"time"
"github.com/github/s3gof3r/internal/s3client"
"golang.org/x/sync/errgroup"
)
func newObjectLister(c *Config, b *Bucket, prefixes []string, maxKeys int) (*ObjectLister, error) {
cCopy := *c
cCopy.NTry = max(c.NTry, 1)
cCopy.Concurrency = max(c.Concurrency, 1)
bCopy := *b
ctx, cancel := context.WithCancel(context.TODO())
l := ObjectLister{
cancel: cancel,
b: &bCopy,
c: &cCopy,
prefixCh: make(chan string, len(prefixes)),
resultCh: make(chan []string, 1),
maxKeys: maxKeys,
}
// Enqueue all of the prefixes that we were given. This won't
// block because we have initialized `prefixCh` to be long enough
// to hold all of them. This has the added benefit that there is
// no data race if the caller happens to modify the contents of
// the slice after this call returns.
for _, p := range prefixes {
l.prefixCh <- p
}
close(l.prefixCh)
eg, ctx := errgroup.WithContext(ctx)
for i := 0; i < min(l.c.Concurrency, len(prefixes)); i++ {
eg.Go(func() error {
return l.worker(ctx)
})
}
go func() {
l.finalErr = eg.Wait()
close(l.resultCh)
l.cancel()
}()
return &l, nil
}
type ObjectLister struct {
cancel context.CancelFunc
b *Bucket
c *Config
maxKeys int
prefixCh chan string
resultCh chan []string
// finalErr is set before closing `resultCh` if any of the workers
// returned errors. Any subsequent calls to `Next()` report this
// error.
finalErr error
// currentValue and currentErr are the "results" of the most
// recent call to `Next()`.
currentValue []string
currentErr error
}
func (l *ObjectLister) worker(ctx context.Context) error {
for p := range l.prefixCh {
var continuation string
retries:
for {
res, err := l.retryListObjects(ctx, p, continuation)
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
default:
return err
}
}
keys := make([]string, 0, len(res.Contents))
for _, c := range res.Contents {
keys = append(keys, c.Key)
}
select {
case <-ctx.Done():
return ctx.Err()
case l.resultCh <- keys:
continuation = res.NextContinuationToken
if continuation != "" {
continue
}
// Break from this prefix and grab the next one
break retries
}
}
}
return nil
}
func (l *ObjectLister) retryListObjects(
ctx context.Context, p, continuation string,
) (*listBucketResult, error) {
var err error
var res *listBucketResult
var timer *time.Timer
for i := 0; i < l.c.NTry; i++ {
opts := listObjectsOptions{MaxKeys: l.maxKeys, Prefix: p, ContinuationToken: continuation}
res, err = listObjects(l.c, l.b, opts)
if err == nil {
return res, nil
}
// Exponential back-off, reusing the timer if possible:
duration := time.Duration(math.Exp2(float64(i))) * 100 * time.Millisecond
if timer == nil {
timer = time.NewTimer(duration)
} else {
// The only way to get here is if the timer was created
// during an earlier iteration of the loop, in which case
// the select below must have gone through the `<-timer.C`
// branch, which drained the timer. So it is safe to call
// `Reset()`:
timer.Reset(duration)
}
select {
case <-timer.C:
// Timer has fired and been drained, so it is ready for reuse.
case <-ctx.Done():
// Stop the timer to prevent a resource leak:
timer.Stop()
return nil, ctx.Err()
}
}
return nil, err
}
// Next moves the iterator to the next set of results. It returns true if there
// are more results, or false if there are no more results or there was an
// error.
func (l *ObjectLister) Next() bool {
var ok bool
l.currentValue, ok = <-l.resultCh
if !ok {
// If there has been an error, we now show it to the caller:
l.currentErr = l.finalErr
return false
}
return true
}
func (l *ObjectLister) Value() []string {
return l.currentValue
}
func (l *ObjectLister) Error() error {
return l.currentErr
}
func (l *ObjectLister) Close() {
l.cancel()
}
// ListObjectsOptions specifies the options for a ListObjects operation on a S3
// bucket
type listObjectsOptions struct {
// Maximum number of keys to return per request
MaxKeys int
// Only list those keys that start with the given prefix
Prefix string
// Continuation token from the previous request
ContinuationToken string
}
type listBucketResult struct {
Name string `xml:"Name"`
Prefix string `xml:"Prefix"`
KeyCount int `xml:"KeyCount"`
MaxKeys int `xml:"MaxKeys"`
IsTruncated bool `xml:"IsTrucated"`
NextContinuationToken string `xml:"NextContinuationToken"`
Contents []listBucketContents `xml:"Contents"`
}
type listBucketContents struct {
Key string `xml:"Key"`
LastModified time.Time `xml:"LastModified"`
ETag string `xml:"ETag"`
Size int64 `xml:"Size"`
StorageClass string `xml:"StorageClass"`
CommonPrefixes []CommonPrefix `xml:"CommonPrefixes"`
}
type CommonPrefix struct {
Prefix string `xml:"Prefix"`
}
type ListObjectsResult struct {
result *listBucketResult
}
func listObjects(c *Config, b *Bucket, opts listObjectsOptions) (*listBucketResult, error) {
result := new(listBucketResult)
u, err := b.url("", c)
if err != nil {
return nil, err
}
q := u.Query()
q.Set("list-type", "2")
if opts.MaxKeys > 0 {
q.Set("max-keys", strconv.Itoa(opts.MaxKeys))
}
if opts.Prefix != "" {
q.Set("prefix", opts.Prefix)
}
if opts.ContinuationToken != "" {
q.Set("continuation-token", opts.ContinuationToken)
}
u.RawQuery = q.Encode()
r := http.Request{
Method: "GET",
URL: u,
}
b.Sign(&r)
resp, err := b.conf().Do(&r)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, s3client.NewRespError(resp)
}
err = xml.NewDecoder(resp.Body).Decode(result)
closeErr := resp.Body.Close()
if err != nil {
return nil, err
}
if closeErr != nil {
return nil, closeErr
}
return result, nil
}