forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_objects.go
231 lines (197 loc) · 4.8 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
package s3gof3r
import (
"encoding/xml"
"math"
"net/http"
"strconv"
"sync"
"time"
)
func newObjectLister(c *Config, b *Bucket, prefixes []string, maxKeys int) (*ObjectLister, error) {
l := new(ObjectLister)
l.c, l.b = new(Config), new(Bucket)
*l.c, *l.b = *c, *b
l.c.NTry = max(c.NTry, 1)
l.c.Concurrency = max(c.Concurrency, 1)
l.getCh, l.putCh = make(chan string), make(chan []string, 1)
l.quit = make(chan struct{})
l.prefixes = prefixes
l.maxKeys = maxKeys
for i := 0; i < l.c.Concurrency; i++ {
l.wg.Add(1)
go l.worker()
}
go l.initPrefixes()
return l, nil
}
type ObjectLister struct {
b *Bucket
c *Config
prefixes []string
maxKeys int
next []string
err error
getCh chan string
putCh chan []string
wg sync.WaitGroup
quit chan struct{}
quitOnce sync.Once
}
func (l *ObjectLister) closeQuit() {
l.quitOnce.Do(func() { close(l.quit) })
}
func (l *ObjectLister) initPrefixes() {
// We first enqueue all of the prefixes we were given
for _, p := range l.prefixes {
l.getCh <- p
}
close(l.getCh)
l.wg.Wait()
close(l.putCh)
}
func (l *ObjectLister) worker() {
for p := range l.getCh {
var continuation string
retries:
for {
res, err := l.retryListObjects(p, continuation)
if err != nil {
select {
case <-l.quit:
return
default:
l.err = err
l.closeQuit()
return
}
}
keys := make([]string, 0, len(res.Contents))
for _, c := range res.Contents {
keys = append(keys, c.Key)
}
select {
case <-l.quit:
return
case l.putCh <- keys:
continuation = res.NextContinuationToken
if continuation != "" {
continue
}
// Break from this prefix and grab the next one
break retries
}
}
}
l.wg.Done()
}
func (l *ObjectLister) retryListObjects(p, continuation string) (*listBucketResult, error) {
var err error
var res *listBucketResult
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
}
time.Sleep(time.Duration(math.Exp2(float64(i))) * 100 * time.Millisecond) // exponential back-off
}
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 {
if l.err != nil {
return false
}
select {
case n, ok := <-l.putCh:
if !ok {
l.err = nil
return false
}
l.next = n
return true
case <-l.quit:
return false
}
}
func (l *ObjectLister) Value() []string {
return l.next
}
func (l *ObjectLister) Error() error {
return l.err
}
func (l *ObjectLister) Close() {
l.closeQuit()
}
// 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) (result *listBucketResult, err 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
}
defer checkClose(resp.Body, err)
if resp.StatusCode != 200 {
return nil, newRespError(resp)
}
decoder := xml.NewDecoder(resp.Body)
if err := decoder.Decode(result); err != nil {
return nil, err
}
return result, nil
}