-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.go
More file actions
72 lines (60 loc) · 1.42 KB
/
helpers.go
File metadata and controls
72 lines (60 loc) · 1.42 KB
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 bfs
import (
"context"
"io"
)
type supportsCopy interface {
Copy(context.Context, string, string) error
}
type supportsRemoveAll interface {
RemoveAll(context.Context, string) error
}
// 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 b, ok := bucket.(supportsCopy); ok {
return b.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()
}
// RemoveAll removes all files matching the pattern.
func RemoveAll(ctx context.Context, bucket Bucket, pattern string) error {
if b, ok := bucket.(supportsRemoveAll); ok {
return b.RemoveAll(ctx, pattern)
}
it, err := bucket.Glob(ctx, pattern)
if err != nil {
return err
}
defer it.Close()
for it.Next() {
if err := bucket.Remove(ctx, it.Name()); err != nil {
return err
}
}
return it.Error()
}