-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
121 lines (112 loc) · 2.85 KB
/
url.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
package iokit_aws
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"go4ml.xyz/errstr"
"io"
"net/url"
"strings"
)
/*
S3_*_URL => ...
S3_URL = s3://key1:[email protected]/prefix1
s3://$/xxx => Lookup("") => AccessPoint{entrypoint,region1,prefix1,{key1,secret1}} + xxx
S3_DEFAULT_URL = s3://key2:[email protected]/prefix2
s3://$default/xxx => Lookup("default") => AccessPoint{entrypoint,region2,prefix2,{key2,secret2}} + xxx
*/
func DecodeUrl(u *url.URL) (ap *AccessPoint, err error) {
p := u.Path
for len(p) > 0 && p[0] == '/' {
p = p[1:]
}
j := strings.Index(p, "/")
ap = &AccessPoint{}
if j < 0 {
ap.Bucket = p
} else {
ap.Bucket = p[:j]
ap.Prefix = p[j+1:]
}
if ap.Bucket == "" {
return nil, errstr.New("bad bucket name in path `" + u.Path + "`")
}
hs := strings.Split(u.Host, ".")
if len(hs) > 2 {
if hs[0] == "s3" {
hs = hs[1:]
}
ap.Region = hs[0]
}
ap.Endpoint = u.Host
if pwd, ok := u.User.Password(); !ok {
ap.Credentials = credentials.NewCredentials(&credentials.SharedCredentialsProvider{})
} else {
ap.Credentials = credentials.NewStaticCredentials(u.User.Username(), pwd, "")
}
return
}
func ResolveUrl(s3url string) (ssn *session.Session, loc Location, err error) {
u, err := url.Parse(s3url)
if err != nil {
return
}
if len(u.Host) > 0 && u.Host[0] == '$' {
apname := u.Host[1:]
ap, ok := Lookup(apname)
if !ok {
return nil, Location{}, errstr.New("unknown access point " + apname)
}
ssn, err = ap.Session()
path := strings.Trim(ap.Prefix+u.Path, "/")
loc = Location{aws.String(ap.Bucket), aws.String(path)}
return
}
ap, err := DecodeUrl(u)
if err != nil {
return
}
loc = Location{aws.String(ap.Bucket), aws.String(ap.Prefix)}
ssn, err = ap.Session()
return
}
func Download(url string, wr io.WriterAt) (err error) {
ssn, loc, err := ResolveUrl(url)
if err != nil {
return
}
dlr := s3manager.NewDownloader(ssn)
_, err = dlr.Download(wr, &s3.GetObjectInput{Bucket: loc.Bucket, Key: loc.Key})
return
}
func Upload(url string, rd io.Reader, metadata map[string]string) (err error) {
ssn, loc, err := ResolveUrl(url)
if err != nil {
return
}
uploader := s3manager.NewUploader(ssn)
mdp := map[string]*string{}
for k, v := range metadata {
mdp[k] = aws.String(v)
}
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: loc.Bucket,
Key: loc.Key,
Body: rd,
Metadata: mdp,
})
return
}
type Url string
func (url Url) Download(wr io.Writer) error {
return Download(string(url), wr.(io.WriterAt))
}
func (url Url) Upload(rd io.Reader, metadata ...map[string]string) error {
mdp := map[string]string(nil)
if len(metadata) > 0 {
mdp = metadata[0]
}
return Upload(string(url), rd, mdp)
}