-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathremote_files.go
72 lines (58 loc) · 1.62 KB
/
remote_files.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
package filestore
import (
"bufio"
"context"
"io"
"net/url"
"strings"
"github.com/dgraph-io/dgraph/chunker"
"github.com/dgraph-io/dgraph/x"
"github.com/minio/minio-go/v6"
)
type remoteFiles struct {
mc *x.MinioClient
}
func (rf *remoteFiles) Open(path string) (io.ReadCloser, error) {
url, err := url.Parse(path)
x.Check(err)
bucket, prefix := rf.mc.ParseBucketAndPrefix(url.Path)
obj, err := rf.mc.GetObject(bucket, prefix, minio.GetObjectOptions{})
if err != nil {
return nil, err
}
return obj, nil
}
// Checking if a file exists is a no-op in minio, since s3 cannot confirm if a directory exists
func (rf *remoteFiles) Exists(path string) bool {
return true
}
func hasAnySuffix(str string, suffixes []string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(str, suffix) {
return true
}
}
return false
}
func (rf *remoteFiles) FindDataFiles(str string, ext []string) (paths []string) {
for _, dirPath := range strings.Split(str, ",") {
url, err := url.Parse(dirPath)
x.Check(err)
bucket, prefix := rf.mc.ParseBucketAndPrefix(url.Path)
for obj := range rf.mc.ListObjectsV2(bucket, prefix, true, context.TODO().Done()) {
if hasAnySuffix(obj.Key, ext) {
paths = append(paths, bucket+"/"+obj.Key)
}
}
}
return
}
func (rf *remoteFiles) ChunkReader(file string, key x.SensitiveByteSlice) (*bufio.Reader, func()) {
url, err := url.Parse(file)
x.Check(err)
bucket, prefix := rf.mc.ParseBucketAndPrefix(url.Path)
obj, err := rf.mc.GetObject(bucket, prefix, minio.GetObjectOptions{})
x.Check(err)
return chunker.StreamReader(url.Path, key, obj)
}
var _ FileStore = (*remoteFiles)(nil)