-
Notifications
You must be signed in to change notification settings - Fork 7
/
read.go
173 lines (141 loc) · 3.63 KB
/
read.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
165
166
167
168
169
170
171
172
173
package s3
import (
"bufio"
"compress/gzip"
"crypto/md5"
"fmt"
"path/filepath"
"strings"
minio "github.com/minio/minio-go"
"github.com/pcelvng/task-tools/file/stat"
"github.com/pcelvng/task-tools/file/util"
)
func NewReader(pth string, accessKey, secretKey string) (*Reader, error) {
// get s3 client
s3Client, err := newS3Client(accessKey, secretKey)
if err != nil {
return nil, err
}
return newReaderFromS3Client(pth, s3Client)
}
func newReaderFromS3Client(pth string, s3Client *minio.Client) (*Reader, error) {
sts := stat.New()
sts.SetPath(pth)
// get bucket, objPth and validate
bucket, objPth := parsePth(pth)
// get object
s3Obj, err := s3Client.GetObject(bucket, objPth, minio.GetObjectOptions{})
if err != nil {
return nil, err
}
// stats
objInfo, err := s3Obj.Stat()
if err != nil {
return nil, err
}
sts.SetCreated(objInfo.LastModified)
sts.SetSize(objInfo.Size)
// hash reader
rHshr := util.NewHashReader(md5.New(), s3Obj)
// compression
var rBuf *bufio.Reader
var rGzip *gzip.Reader
if ext := filepath.Ext(pth); ext == ".gz" {
rGzip, err = gzip.NewReader(rHshr)
if err != nil {
return nil, err // problem reading header
}
rBuf = bufio.NewReader(rGzip)
} else {
rBuf = bufio.NewReader(rHshr)
}
return &Reader{
s3Obj: s3Obj,
rBuf: rBuf,
rGzip: rGzip,
rHshr: rHshr,
sts: sts,
}, nil
}
// Reader will read in streamed bytes from the s3 object.NewS3Client
type Reader struct {
s3Obj *minio.Object // s3 file object
rBuf *bufio.Reader
rGzip *gzip.Reader
rHshr *util.HashReader
sts stat.Stats
closed bool
}
func (r *Reader) ReadLine() (ln []byte, err error) {
ln, err = r.rBuf.ReadBytes('\n')
if len(ln) > 0 {
r.sts.AddLine()
// note that even '\n' bytes are
// accounted for.
r.sts.AddBytes(int64(len(ln)))
// drop newline characters
if ln[len(ln)-1] == '\n' {
drop := 1
if len(ln) > 1 && ln[len(ln)-2] == '\r' { // windows newline
drop = 2
}
ln = ln[:len(ln)-drop]
}
}
return ln, err
}
func (r *Reader) Read(p []byte) (n int, err error) {
n, err = r.rBuf.Read(p)
r.sts.AddBytes(int64(n))
return n, err
}
func (r *Reader) Stats() stat.Stats {
return r.sts.Clone()
}
func (r *Reader) Close() (err error) {
if r.closed {
return nil
}
if r.rGzip != nil {
r.rGzip.Close()
}
err = r.s3Obj.Close()
// calculate checksum
r.sts.SetChecksum(r.rHshr.Hshr)
r.closed = true
return err
}
// ListFiles will list all file objects in the provided pth directory.
// pth is assumed to be a directory and so a trailing "/" is appended
// if one does not already exist.
func ListFiles(pth string, accessKey, secretKey string) ([]stat.Stats, error) {
// get s3 client
s3Client, err := newS3Client(accessKey, secretKey)
if err != nil {
return nil, err
}
bucket, objPth := parsePth(pth)
// objPth should always have trailing '/' (assumed to be dir)
if !strings.HasSuffix(objPth, "/") {
objPth = objPth + "/"
}
// create a done channel to control 'ListObjectsV2' go routine.
doneCh := make(chan struct{}) // being used like a context.Context
// indicate to our routine to exit cleanly upon return.
defer close(doneCh)
allSts := make([]stat.Stats, 0)
objInfoCh := s3Client.ListObjectsV2(bucket, objPth, false, doneCh)
for objInfo := range objInfoCh {
// don't include dir and err objects
if strings.HasSuffix(objInfo.Key, "/") || objInfo.Err != nil {
continue
}
sts := stat.New()
sts.SetCreated(objInfo.LastModified)
sts.Checksum = strings.Trim(objInfo.ETag, `"`) // returns checksum with '"'
sts.SetPath(fmt.Sprintf("s3://%s/%s", bucket, objInfo.Key))
sts.SetSize(objInfo.Size)
allSts = append(allSts, sts)
}
return allSts, nil
}