forked from ohsu-comp-bio/funnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric_s3.go
164 lines (142 loc) · 4.17 KB
/
generic_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
package storage
import (
"context"
"fmt"
"strings"
"github.com/minio/minio-go"
"github.com/ohsu-comp-bio/funnel/config"
)
// GenericS3 provides access to an S3 object store.
type GenericS3 struct {
client *minio.Client
endpoint string
}
// NewGenericS3 creates a new GenericS3 instance, given an endpoint URL
// and a set of authentication credentials.
func NewGenericS3(conf config.GenericS3Storage) (*GenericS3, error) {
ssl := strings.HasPrefix(conf.Endpoint, "https")
endpoint := endpointRE.ReplaceAllString(conf.Endpoint, "$2")
client, err := minio.NewV2(endpoint, conf.Key, conf.Secret, ssl)
if err != nil {
return nil, fmt.Errorf("error creating generic s3 backend: %v", err)
}
return &GenericS3{client, endpoint + "/"}, nil
}
// Stat returns information about the object at the given storage URL.
func (s3 *GenericS3) Stat(ctx context.Context, url string) (*Object, error) {
u, err := s3.parse(url)
if err != nil {
return nil, err
}
opts := minio.GetObjectOptions{}
obj, err := s3.client.GetObjectWithContext(ctx, u.bucket, u.path, opts)
if err != nil {
return nil, fmt.Errorf("genericS3: getting object: %s", err)
}
info, err := obj.Stat()
if err != nil {
return nil, fmt.Errorf("genericS3: stat object: %s", err)
}
return &Object{
URL: url,
Name: info.Key,
ETag: info.ETag,
LastModified: info.LastModified,
Size: info.Size,
}, nil
}
// List lists the objects at the given url.
func (s3 *GenericS3) List(ctx context.Context, url string) ([]*Object, error) {
u, err := s3.parse(url)
if err != nil {
return nil, err
}
// Recursively list all objects.
var objects []*Object
recursive := true
for info := range s3.client.ListObjects(u.bucket, u.path, recursive, ctx.Done()) {
// check if key represents a directory
if strings.HasSuffix(info.Key, "/") {
continue
}
objects = append(objects, &Object{
URL: "s3://" + u.bucket + "/" + info.Key,
Name: info.Key,
ETag: info.ETag,
LastModified: info.LastModified,
Size: info.Size,
})
}
return objects, nil
}
// Get copies an object from S3 to the host path.
func (s3 *GenericS3) Get(ctx context.Context, url, path string) (*Object, error) {
obj, err := s3.Stat(ctx, url)
if err != nil {
return nil, err
}
u, err := s3.parse(url)
if err != nil {
return nil, err
}
opts := minio.GetObjectOptions{}
err = s3.client.FGetObjectWithContext(ctx, u.bucket, u.path, path, opts)
if err != nil {
return nil, fmt.Errorf("genericS3: getting object %s: %v", url, err)
}
return obj, nil
}
// Put copies an object (file) from the host path to S3.
func (s3 *GenericS3) Put(ctx context.Context, url, path string) (*Object, error) {
u, err := s3.parse(url)
if err != nil {
return nil, err
}
opts := minio.PutObjectOptions{}
_, err = s3.client.FPutObjectWithContext(ctx, u.bucket, u.path, path, opts)
if err != nil {
return nil, fmt.Errorf("genericS3: putting object %s: %v", url, err)
}
return s3.Stat(ctx, url)
}
// Join joins the given URL with the given subpath.
func (s3 *GenericS3) Join(url, path string) (string, error) {
return strings.TrimSuffix(url, "/") + "/" + path, nil
}
// UnsupportedOperations describes which operations (Get, Put, etc) are not
// supported for the given URL.
func (s3 *GenericS3) UnsupportedOperations(url string) UnsupportedOperations {
u, err := s3.parse(url)
if err != nil {
return AllUnsupported(err)
}
ok, err := s3.client.BucketExists(u.bucket)
if err != nil {
err = fmt.Errorf("genericS3: failed to find bucket %q: %v", u.bucket, err)
return AllUnsupported(err)
}
if !ok {
err := fmt.Errorf("genericS3: bucket does not exist: %q", u.bucket)
return AllUnsupported(err)
}
return AllSupported()
}
func (s3 *GenericS3) parse(rawurl string) (*urlparts, error) {
if !strings.HasPrefix(rawurl, s3Protocol) {
return nil, &ErrUnsupportedProtocol{"genericS3"}
}
path := strings.TrimPrefix(rawurl, s3Protocol)
path = strings.TrimPrefix(path, s3.endpoint)
if path == "" {
return nil, &ErrInvalidURL{"genericS3"}
}
split := strings.SplitN(path, "/", 2)
url := &urlparts{}
if len(split) > 0 {
url.bucket = split[0]
}
if len(split) == 2 {
url.path = split[1]
}
return url, nil
}