-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.go
285 lines (231 loc) · 7.3 KB
/
list.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
package main
import (
"errors"
"fmt"
"net/url"
"sync"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"go.uber.org/zap"
)
// objectCounter stores the total object count ans sum of object sizes returned in a listObjects page. This is useful
// for things that track prgress such as a progress bar
type objectCounter struct {
count int
size int64
}
// BucketLister stores everything we need to list a bucket, be it for ls output or processing for a copy or remove
// operation
type BucketLister struct {
source url.URL
objects chan []*s3.Object
versions chan []*s3.ObjectIdentifier
sizeChan chan objectCounter
wg sync.WaitGroup
svc *s3.S3
threads int
}
// Process the output of a list object versionsoperation. Stores the srcObjects found in a channel of Object Identifiers.
// delete markers which are really srcObjects themselves are also processed and stored on the channel. For exact match
// operations we filter the result to llook for exact matches.
func (bl *BucketLister) processListObjectsVersionsOutput(exactMatchKey string) func(versions []*s3.ObjectVersion, deleters []*s3.DeleteMarkerEntry) {
if exactMatchKey == "" {
return func(versions []*s3.ObjectVersion, deleters []*s3.DeleteMarkerEntry) {
defer bl.wg.Done()
objectList := make([]*s3.ObjectIdentifier, len(versions)+len(deleters))
objectPos := 0
for _, item := range versions {
objectList[objectPos] = &s3.ObjectIdentifier{Key: item.Key, VersionId: item.VersionId}
objectPos++
}
for _, item := range deleters {
objectList[objectPos] = &s3.ObjectIdentifier{Key: item.Key, VersionId: item.VersionId}
objectPos++
}
bl.versions <- objectList
}
}
return func(versions []*s3.ObjectVersion, deleters []*s3.DeleteMarkerEntry) {
defer bl.wg.Done()
objectList := make([]*s3.ObjectIdentifier, 0, len(versions)+len(deleters))
for _, item := range versions {
if *item.Key == exactMatchKey {
objectList = append(objectList, &s3.ObjectIdentifier{Key: item.Key, VersionId: item.VersionId})
}
}
for _, item := range deleters {
if *item.Key == exactMatchKey {
objectList = append(objectList, &s3.ObjectIdentifier{Key: item.Key, VersionId: item.VersionId})
}
}
bl.versions <- objectList
}
}
// Process the output of a list object operation. Stores the srcObjects found in a channel of Object Identifiers. Also
// optionally stores size and count of cobjects in a seperate channel
func (bl *BucketLister) processListObjectsOutput(withSize bool) func(contents []*s3.Object) {
return func(contents []*s3.Object) {
defer bl.wg.Done()
bl.objects <- contents
if withSize {
var fileSizeTotal int64
objectPos := 0
for _, item := range contents {
objectPos++
if withSize {
fileSizeTotal += *item.Size
}
}
bl.sizeChan <- objectCounter{
count: objectPos,
size: fileSizeTotal,
}
}
}
}
// Lists srcObjects and their versions in a bucket
func (bl *BucketLister) listObjectVersions(exactMatch bool) error {
defer close(bl.versions)
var logger = zap.S()
logger.Infof("Listing all object versions and delete markers in bucket: %s", bl.source.RawPath)
listVersionsInput := s3.ListObjectVersionsInput{
Bucket: aws.String(bl.source.Host),
}
if len(bl.source.Path) > 0 {
listVersionsInput.Prefix = aws.String(bl.source.Path[1:])
}
var exactMatchKey string
if exactMatch {
exactMatchKey = bl.source.Path[1:]
}
processListObjectsVersionsOutputFunc := bl.processListObjectsVersionsOutput(exactMatchKey)
err := bl.svc.ListObjectVersionsPages(&listVersionsInput, func(result *s3.ListObjectVersionsOutput, lastPage bool) bool {
if len(result.Versions)+len(result.DeleteMarkers) > 0 {
bl.wg.Add(1)
go processListObjectsVersionsOutputFunc(result.Versions, result.DeleteMarkers)
}
return true
})
bl.wg.Wait()
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
logger.Fatal(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
logger.Fatal(err.Error())
}
}
return err
}
// Prints objets found in the list operation
func (bl *BucketLister) printAllObjects(versions bool) {
defer bl.wg.Done()
if versions {
for item := range bl.versions {
for _, object := range item {
fmt.Println(*object.VersionId + " " + *object.Key)
}
}
} else {
for item := range bl.objects {
for _, object := range item {
fmt.Println(*object.Key)
}
}
}
}
// listObjects lists srcObjects in a bucket
func (bl *BucketLister) listObjects(withSize bool) error {
defer close(bl.objects)
var logger = zap.S()
listInput := s3.ListObjectsV2Input{
Bucket: aws.String(bl.source.Host),
}
if len(bl.source.Path) > 0 {
listInput.Prefix = aws.String(bl.source.Path[1:])
}
processListObjectsOutputFunc := bl.processListObjectsOutput(withSize)
err := bl.svc.ListObjectsV2Pages(&listInput, func(result *s3.ListObjectsV2Output, lastPage bool) bool {
if len(result.Contents) > 0 {
bl.wg.Add(1)
go processListObjectsOutputFunc(result.Contents)
}
return true
})
bl.wg.Wait()
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
logger.Fatal(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
logger.Fatal(err.Error())
}
fmt.Println(err)
}
return err
}
// List srcObjects for a bucket whose details are stored in the srcLister receiver. Can list versions too
func (bl *BucketLister) List(versions bool) error {
go bl.printAllObjects(versions)
var err error
if versions {
bl.versions = make(chan []*s3.ObjectIdentifier, bl.threads)
err = bl.listObjectVersions(false)
bl.wg.Add(1)
} else {
bl.objects = make(chan []*s3.Object, bl.threads)
err = bl.listObjects(false)
bl.wg.Add(1)
}
bl.wg.Wait()
return err
}
func initBucketLister(source string, versions bool, threads int) (*BucketLister, error) {
sourceURL, err := url.Parse(source)
if err != nil {
return nil, err
}
if sourceURL.Scheme != "s3" {
return nil, errors.New("usage: aws s3 ls <S3Uri> ")
}
//construct a new structure. Initialize resultsChan and sizechan even though they may be overridden
bl := &BucketLister{
source: *sourceURL,
wg: sync.WaitGroup{},
sizeChan: make(chan objectCounter, threads),
threads: threads,
}
if versions {
bl.versions = make(chan []*s3.ObjectIdentifier)
} else {
bl.objects = make(chan []*s3.Object)
}
return bl, nil
}
// NewBucketLister creates a new BucketLister struct initialized with all variables needed to list a bucket
func NewBucketLister(detectRegion bool, source string, versions bool, threads int, sess *session.Session) (*BucketLister, error) {
bl, err := initBucketLister(source, versions, threads)
if err == nil {
bl.svc, err = checkBucket(sess, detectRegion, bl.source.Host, nil)
}
return bl, err
}
// NewBucketListerWithSvc creates a new BucketLister struct initialized with all variables needed to list a bucket
func NewBucketListerWithSvc(source string, versions bool, threads int, svc *s3.S3) (*BucketLister, error) {
bl, err := initBucketLister(source, versions, threads)
if err == nil {
bl.svc = svc
}
return bl, err
}