forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
208 lines (183 loc) · 4.48 KB
/
s3.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
package core
import (
"context"
"errors"
"net/http"
"path"
"regexp"
"strings"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/peak/s5cmd/url"
)
var (
// ErrInterrupted is the error used when the main context is canceled
ErrInterrupted = errors.New("Operation interrupted")
// ErrNilResult is returned if a nil result in encountered
ErrNilResult = errors.New("nil result")
)
func s3delete(svc *s3.S3, obj *url.S3Url) (*s3.DeleteObjectOutput, error) {
return svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(obj.Bucket),
Key: aws.String(obj.Key),
})
}
func s3head(svc *s3.S3, obj *url.S3Url) (*s3.HeadObjectOutput, error) {
return svc.HeadObject(&s3.HeadObjectInput{
Bucket: aws.String(obj.Bucket),
Key: aws.String(obj.Key),
})
}
type s3listItem struct {
*s3.Object
parsedKey string
isCommonPrefix bool
}
func s3list(ctx context.Context, svc *s3.S3, s3url *url.S3Url, emitChan chan<- interface{}) error {
inp := s3.ListObjectsV2Input{
Bucket: aws.String(s3url.Bucket),
}
wildkey := s3url.Key
var prefix, filter string
loc := strings.IndexAny(wildkey, url.S3WildCharacters)
wildOperation := loc > -1
if !wildOperation {
// no wildcard operation
inp.SetDelimiter("/")
prefix = s3url.Key
} else {
// wildcard operation
prefix = wildkey[:loc]
filter = wildkey[loc:]
}
inp.SetPrefix(prefix)
var (
r *regexp.Regexp
err error
)
trimPrefix := path.Dir(prefix) + "/"
if trimPrefix == "./" {
trimPrefix = ""
}
if !wildOperation {
// prevent "ls s3://bucket/path/key" from matching s3://bucket/path/keyword
// it will still match s3://bucket/path/keydir/ because we don't match the regex on commonPrefixes
filter = prefix[len(trimPrefix):]
}
if filter != "" {
filterRegex := regexp.QuoteMeta(filter)
filterRegex = strings.Replace(filterRegex, "\\?", ".", -1)
filterRegex = strings.Replace(filterRegex, "\\*", ".*?", -1)
r, err = regexp.Compile("^" + filterRegex + "$")
if err != nil {
return err
}
}
var mu sync.Mutex
canceled := false
isCanceled := func() bool {
select {
case <-ctx.Done():
mu.Lock()
defer mu.Unlock()
canceled = true
return true
default:
return false
}
}
emit := func(item *s3listItem) bool {
var data interface{}
if item != nil {
// avoid nil inside interface
data = item
}
for {
select {
case <-ctx.Done():
mu.Lock()
defer mu.Unlock()
canceled = true
return false
case emitChan <- data:
return true
}
}
}
err = svc.ListObjectsV2PagesWithContext(ctx, &inp, func(p *s3.ListObjectsV2Output, lastPage bool) bool {
if isCanceled() {
return false
}
for _, c := range p.CommonPrefixes {
key := *c.Prefix
if strings.Index(key, trimPrefix) == 0 {
key = key[len(trimPrefix):]
}
if !emit(&s3listItem{
Object: &s3.Object{Key: c.Prefix},
parsedKey: key,
isCommonPrefix: true,
}) {
return false
}
}
for _, c := range p.Contents {
key := *c.Key
isCommonPrefix := wildOperation && key[len(key)-1] == '/' // Keys ending with prefix in wild output are "directories"
if strings.Index(key, trimPrefix) == 0 {
key = key[len(trimPrefix):]
}
if r != nil && !r.MatchString(key) {
continue
}
if !emit(&s3listItem{
Object: c,
parsedKey: key,
isCommonPrefix: isCommonPrefix,
}) {
return false
}
}
if !*p.IsTruncated {
emit(nil) // EOF
}
return !isCanceled()
})
mu.Lock()
defer mu.Unlock()
if err == nil && canceled {
return ErrInterrupted
}
return err
}
type s3wildCallback func(*s3listItem) *Job
func s3wildOperation(url *url.S3Url, wp *WorkerParams, callback s3wildCallback) error {
return wildOperation(wp, func(ch chan<- interface{}) error {
return s3list(wp.ctx, wp.s3svc, url, ch)
}, func(data interface{}) *Job {
if data == nil {
return callback(nil)
}
return callback(data.(*s3listItem))
})
}
func GetSessionForBucket(svc *s3.S3, bucket string) (*session.Session, error) {
o, err := svc.GetBucketLocation(&s3.GetBucketLocationInput{
Bucket: &bucket,
})
if err == nil && o.LocationConstraint == nil {
err = ErrNilResult
}
if err != nil {
return nil, err
}
endpointURL := svc.Endpoint
noVerifySSL := false
transport, ok := svc.Config.HTTPClient.Transport.(*http.Transport)
if ok {
noVerifySSL = transport.TLSClientConfig.InsecureSkipVerify
}
return NewAwsSession(-1, endpointURL, *o.LocationConstraint, noVerifySSL)
}