-
Notifications
You must be signed in to change notification settings - Fork 7
/
write.go
221 lines (188 loc) · 4.53 KB
/
write.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package gs
import (
"mime"
"path/filepath"
"sync"
minio "github.com/minio/minio-go"
"github.com/pcelvng/task-tools/file/buf"
"github.com/pcelvng/task-tools/file/stat"
)
func NewWriter(pth string, accessKey, secretKey string, opt *Options) (*Writer, error) {
// gs client:
// using minio client library;
// final writing doesn't happen until Close is called
// but getting the client now does authentication
// so we know early of authentication issues.
gsClient, err := newGSClient(accessKey, secretKey)
if err != nil {
return nil, err
}
return newWriterFromGSClient(pth, gsClient, opt)
}
func newWriterFromGSClient(pth string, gsClient *minio.Client, opt *Options) (*Writer, error) {
if opt == nil {
opt = NewOptions()
}
// stats
sts := stat.New()
sts.SetPath(pth)
// compression
if ext := filepath.Ext(pth); ext == ".gz" {
opt.Compress = true
}
// buffer
bfr, err := buf.NewBuffer(opt.Options)
if err != nil {
return nil, err
}
tmpPth := bfr.Stats().Path
// gs bucket, objPth
bucket, objPth := parsePth(pth)
return &Writer{
gsClient: gsClient,
bfr: bfr,
bucket: bucket,
objPth: objPth,
tmpPth: tmpPth,
sts: sts,
keepFailed: opt.KeepFailed,
}, nil
}
// Writer will write to local buffer first
// and will copy all the written contents
// to the gs destination after calling Close().
// Close() must be called in order for the written
// contents to be written to gs.
//
// Calling Abort() before Close() will cleanup the
// buffer. Calling Close() after Abort() will not
// result in any writing to gs.
//
// Calling Abort() after Close() will do nothing.
type Writer struct {
gsClient *minio.Client
bfr *buf.Buffer
sts stat.Stats
objSts stat.Stats // stats as reported by gs
tmpPth string
bucket string // destination gs bucket
objPth string // destination gs object path
done bool
mu sync.Mutex
keepFailed bool // keep local copy of failed file if file buffer is used
}
func (w *Writer) Write(p []byte) (n int, err error) {
return w.bfr.Write(p)
}
func (w *Writer) WriteLine(ln []byte) (err error) {
return w.bfr.WriteLine(ln)
}
func (w *Writer) Stats() stat.Stats {
sts := w.bfr.Stats()
sts.Path = w.sts.Path
sts.Created = w.sts.Created
return sts
}
// Abort will:
// - clear and close buffer
//
// Calling Close after Abort will do nothing.
// Writing after calling Abort has undefined behavior.
func (w *Writer) Abort() (err error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.done {
return nil
}
w.done = true
return w.bfr.Abort()
}
// Close will:
// - calculate final checksum
// - copy (mv) buffer to pth file
// - clear and close buffer
// - report any errors
//
// If an error is returned it should be assumed
// that gs object writing failed.
//
// Calling Abort after Close will do nothing.
// Writing after calling Close has undefined behavior.
func (w *Writer) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.done {
return nil
}
w.done = true
// close buffer to finalize writes
// and copy contents to final
// location.
// sets checksum and size.
w.bfr.Close()
// do copy
_, err := w.copy()
if err != nil {
if w.keepFailed {
w.bfr.Reset()
} else {
w.bfr.Cleanup()
}
return err
}
// set object stats
w.setObjSts()
// set created
w.sts.Created = w.objSts.Created
return w.bfr.Cleanup()
}
// copy will copy the contents of buf
// to the gs path indicated at bucket and
// objPth.
//
// Returns num of bytes copied and error.
func (w *Writer) copy() (n int64, err error) {
// Set contentType based on filepath extension if not given or default
// value of "application/octet-stream" if the extension has no associated type.
opts := minio.PutObjectOptions{}
if opts.ContentType = mime.TypeByExtension(filepath.Ext(w.objPth)); opts.ContentType == "" {
opts.ContentType = "application/octet-stream"
}
// copy tmp file buffer
if w.tmpPth != "" {
return w.gsClient.FPutObject(
w.bucket,
w.objPth,
w.tmpPth,
opts,
)
}
// copy memory buffer
return w.gsClient.PutObject(
w.bucket,
w.objPth,
w.bfr,
w.bfr.Stats().Size,
opts,
)
}
// createdAt will retrieve the created date
// of the object. If the object, doesn't
// exist then will return the time.Time
// zero value.
func (w *Writer) setObjSts() error {
// created date
objInfo, err := w.gsClient.StatObject(
w.bucket,
w.objPth,
minio.StatObjectOptions{},
)
if err != nil {
return err
}
w.objSts.SetCreated(objInfo.LastModified)
w.objSts.Checksum = objInfo.ETag
w.objSts.SetPath(objInfo.Key)
w.objSts.SetSize(objInfo.Size)
return nil
}