-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
141 lines (124 loc) · 3.32 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
package s3
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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/zeebo/errs"
"github.com/jtolio/jam/backends"
)
var (
Error = errs.Class("s3 error")
)
func init() {
backends.Register("s3", New)
}
type Backend struct {
bucket string
prefix string
svc *s3.S3
}
func New(ctx context.Context, u *url.URL) (backends.Backend, error) {
parts := strings.SplitN(strings.TrimPrefix(u.Path, "/"), "/", 2)
var prefix string
bucket := parts[0]
if len(parts) > 1 {
prefix = parts[1]
}
accessKey := u.User.Username()
secretKey, ok := u.User.Password()
if !ok {
return nil, Error.New("s3 url missing secret key. " +
"format s3://<ak>:<sk>@<region/endpoint>/bucket/prefix?disable-ssl=false")
}
cfg := aws.NewConfig().
WithCredentials(credentials.NewStaticCredentials(
accessKey, secretKey, ""))
if strings.Contains(u.Host, ".") {
cfg = cfg.WithEndpoint(u.Host).
WithRegion("us-east-1").
WithS3ForcePathStyle(true)
} else {
cfg = cfg.WithRegion(u.Host)
}
switch strings.ToLower(u.Query().Get("disable-ssl")) {
case "t", "y", "yes", "true":
cfg = cfg.WithDisableSSL(true)
}
sess, err := session.NewSession(cfg)
if err != nil {
return nil, Error.Wrap(err)
}
return &Backend{
bucket: bucket,
prefix: prefix,
svc: s3.New(sess),
}, nil
}
func (b *Backend) Get(ctx context.Context, path string, offset, length int64) (io.ReadCloser, error) {
rangeOffset := fmt.Sprintf("bytes=%d-", offset)
if length > 0 {
rangeOffset = fmt.Sprintf("bytes=%d-%d", offset, offset+length-1)
}
path = b.prefix + path
out, err := b.svc.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: &b.bucket,
Key: &path,
Range: &rangeOffset,
})
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == s3.ErrCodeNoSuchKey {
return nil, Error.Wrap(backends.ErrNotExist)
}
return nil, Error.Wrap(err)
}
return out.Body, nil
}
func (b *Backend) Put(ctx context.Context, path string, data io.Reader) error {
// ugh, probably should spool to disk? this sucks, s3
seekableData, err := ioutil.ReadAll(data)
if err != nil {
return Error.Wrap(err)
}
path = b.prefix + path
_, err = b.svc.PutObjectWithContext(ctx, &s3.PutObjectInput{
Body: bytes.NewReader(seekableData),
Bucket: &b.bucket,
Key: &path,
})
return Error.Wrap(err)
}
func (b *Backend) Delete(ctx context.Context, path string) error {
path = b.prefix + path
_, err := b.svc.DeleteObjectWithContext(ctx, &s3.DeleteObjectInput{
Bucket: &b.bucket,
Key: &path,
})
return Error.Wrap(err)
}
func (b *Backend) List(ctx context.Context, prefix string, cb func(ctx context.Context, path string) error) error {
var internalErr error
prefix = b.prefix + prefix
err := b.svc.ListObjectsPagesWithContext(ctx, &s3.ListObjectsInput{
Bucket: &b.bucket,
Prefix: &prefix,
}, func(page *s3.ListObjectsOutput, lastPage bool) bool {
for _, obj := range page.Contents {
if internalErr != nil {
return false
}
internalErr = cb(ctx, strings.TrimPrefix(*obj.Key, b.prefix))
}
return internalErr == nil
})
return Error.Wrap(errs.Combine(err, internalErr))
}
func (b *Backend) Close() error { return nil }