-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
148 lines (102 loc) · 2.26 KB
/
bucket.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
package bucket
import (
"context"
"io"
"net/url"
"strings"
"github.com/whosonfirst/go-ioutil"
"github.com/whosonfirst/go-whosonfirst-iterate/v2/emitter"
"github.com/whosonfirst/go-whosonfirst-iterate/v2/filters"
"gocloud.dev/blob"
)
const PREFIX string = "bucket-"
type BucketEmitter struct {
emitter.Emitter
bucket *blob.Bucket
filters filters.Filters
}
func init() {
ctx := context.Background()
for _, scheme := range blob.DefaultURLMux().BucketSchemes() {
scheme = PREFIX + scheme
emitter.RegisterEmitter(ctx, scheme, NewBucketEmitter)
}
}
func NewBucketEmitter(ctx context.Context, uri string) (emitter.Emitter, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
u.Scheme = strings.Replace(u.Scheme, PREFIX, "", 1)
bucket_uri := u.String()
bucket, err := blob.OpenBucket(ctx, bucket_uri)
if err != nil {
return nil, err
}
q := u.Query()
f, err := filters.NewQueryFiltersFromQuery(ctx, q)
if err != nil {
return nil, err
}
em := &BucketEmitter{
bucket: bucket,
filters: f,
}
return em, nil
}
func (em *BucketEmitter) WalkURI(ctx context.Context, emitter_cb emitter.EmitterCallbackFunc, uri string) error {
// add go routines
// add throttles
// Update to use https://github.com/aaronland/gocloud-blob/tree/main/walk
var list func(context.Context, *blob.Bucket, string) error
list = func(ctx context.Context, b *blob.Bucket, prefix string) error {
iter := b.List(&blob.ListOptions{
Delimiter: "/",
Prefix: prefix,
})
for {
obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
return err
}
if obj.IsDir {
err := list(ctx, b, obj.Key)
if err != nil {
return err
}
continue
}
r, err := em.bucket.NewReader(ctx, obj.Key, nil)
if err != nil {
return err
}
defer r.Close()
fh, err := ioutil.NewReadSeekCloser(r)
if err != nil {
return err
}
if em.filters != nil {
ok, err := em.filters.Apply(ctx, fh)
if err != nil {
return err
}
if !ok {
return nil
}
_, err = fh.Seek(0, 0)
if err != nil {
return err
}
}
err = emitter_cb(ctx, obj.Key, fh)
if err != nil {
return err
}
}
return nil
}
return list(ctx, em.bucket, uri)
}