-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.go
44 lines (37 loc) · 877 Bytes
/
helpers.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
package bfs
import (
"context"
"io"
)
// WriteObject is a quick write helper.
func WriteObject(ctx context.Context, bucket Bucket, name string, data []byte, opts *WriteOptions) error {
w, err := bucket.Create(ctx, name, opts)
if err != nil {
return err
}
defer w.Discard()
if _, err := w.Write(data); err != nil {
return err
}
return w.Commit()
}
// CopyObject is a quick helper to copy objects within the same bucket.
func CopyObject(ctx context.Context, bucket Bucket, src, dst string, dstOpts *WriteOptions) error {
if cp, ok := bucket.(supportsCopying); ok {
return cp.Copy(ctx, src, dst)
}
r, err := bucket.Open(ctx, src)
if err != nil {
return err
}
defer r.Close()
w, err := bucket.Create(ctx, dst, dstOpts)
if err != nil {
return err
}
defer w.Discard()
if _, err := io.Copy(w, r); err != nil {
return err
}
return w.Commit()
}