forked from Jonathan-Rosenberg/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblob.go
142 lines (115 loc) · 2.55 KB
/
blob.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
package util
import (
"context"
"fmt"
"math/rand"
"net/url"
"os"
"strings"
"time"
"io"
"gocloud.dev/blob"
_ "gocloud.dev/blob/azureblob"
_ "gocloud.dev/blob/fileblob"
"gocloud.dev/gcerrors"
)
type BlobDir struct {
bucket *blob.Bucket
urlstr string
}
func NewBlobDir(urlstr string) (*BlobDir, error) {
bucket, err := blob.OpenBucket(context.Background(), urlstr)
if err != nil {
return nil, err
}
return &BlobDir{
bucket: bucket,
urlstr: urlstr,
}, nil
}
func (b *BlobDir) listingBlob(ctx context.Context, prefix string) ([]string, error) {
iter := b.bucket.List(&blob.ListOptions{
Prefix: prefix,
Delimiter: "/",
})
var res []string
for {
obj, err := iter.Next(ctx)
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if obj.IsDir {
if ret, err := b.listingBlob(ctx, obj.Key); err != nil {
return nil, err
} else {
res = append(res, ret...)
}
} else {
res = append(res, obj.Key)
}
}
return res, nil
}
func (b *BlobDir) Close() error {
return b.bucket.Close()
}
func (b *BlobDir) Copy(srcPrefix string) (string, []string, error) {
ctx := context.Background()
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
dir := fmt.Sprintf("temp-%s-xxx-%d", srcPrefix, r1.Int())
blobs, err := b.listingBlob(ctx, srcPrefix)
if err != nil {
return "", nil, err
}
var tempBlobs []string
for _, srcKey := range blobs {
dstKey := dir + "-" + srcKey
err = b.bucket.Copy(ctx, dstKey, srcKey, nil)
if err != nil {
return "", nil, err
}
tempBlobs = append(tempBlobs, dstKey)
}
return fmt.Sprintf("%s-%s", dir, srcPrefix), tempBlobs, nil
}
func (b *BlobDir) Delete(dir string, files []string, hardDelete bool) error {
if strings.HasPrefix(b.urlstr, "file://") {
p, err := url.Parse(b.urlstr)
if err != nil {
return err
}
fullDir := p.Path + "/" + dir
return os.RemoveAll(fullDir)
}
if hardDelete {
ctx := context.Background()
for _, f := range files {
if err := b.bucket.Delete(ctx, f); err != nil {
if gcerrors.NotFound == gcerrors.Code(err) {
continue
} else {
return err
}
}
}
}
return nil
}
func (b *BlobDir) DeleteFile(file string) error {
return b.bucket.Delete(context.Background(), file)
}
func (b *BlobDir) CreateTemp() (dir string, placeHolder string, err error) {
s1 := rand.NewSource(time.Now().UnixNano())
r1 := rand.New(s1)
dir = fmt.Sprintf("temp-%d/", r1.Int())
placeHolder = dir + ".xxx"
err = b.bucket.WriteAll(context.Background(), placeHolder, []byte{}, nil)
if err != nil {
return
}
return
}