-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
65 lines (61 loc) · 1.42 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
// Copyright 2020 cloudeng LLC. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package cloudpath
import (
"net/url"
"strings"
)
// pathFromHostAndPath will construct a / separated string from a h
// and path that starts with a /.
func pathFromHostAndPath(u *url.URL) string {
path := ""
if len(u.Host) > 0 {
path += "/" + u.Host
}
if len(u.Path) > 0 {
path += u.Path
}
return path
}
// AWSS3Matcher implements Matcher for AWS S3 object names. It returns AWSS3
// for its scheme result.
func AWSS3Matcher(p string) *Match {
u, err := url.Parse(p)
if err != nil {
return nil
}
m := &Match{
Scheme: AWSS3,
Separator: '/',
Parameters: parametersFromQuery(u),
}
switch u.Scheme {
case "s3":
m.Volume = u.Host
m.Path = pathFromHostAndPath(u)
return m
case "http", "https":
m.Host = u.Host
m.Path = u.Path
default:
return nil
}
leading := strings.TrimSuffix(u.Host, ".amazonaws.com")
if len(leading) == len(u.Host) {
// not trimmed.
return nil
}
parts := strings.Split(leading, ".")
if len(parts) == 2 && parts[0] == "s3" {
// https://s3.Region.amazonaws.com/bucket-name/key
m.Volume = firstPathComponent(u.Path)
return m
}
if len(parts) > 2 && parts[len(parts)-2] == "s3" {
// https://bucket.name.s3.Region.amazonaws.com/key
m.Volume = leading[:strings.Index(leading, "s3")-1]
return m
}
return nil
}