-
Notifications
You must be signed in to change notification settings - Fork 1k
/
blob.go
163 lines (136 loc) · 3.23 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package client
import (
"bytes"
"context"
"io"
"net/http"
pb "github.com/micro/micro/v3/proto/store"
"github.com/micro/micro/v3/service/client"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/store"
)
const bufferSize = 1024
// NewBlobStore returns a new store service implementation
func NewBlobStore() store.BlobStore {
return &blob{}
}
type blob struct {
client pb.BlobStoreService
}
func (b *blob) Read(key string, opts ...store.BlobOption) (io.Reader, error) {
// validate the key
if len(key) == 0 {
return nil, store.ErrMissingKey
}
// parse the options
var options store.BlobOptions
for _, o := range opts {
o(&options)
}
// execute the rpc
stream, err := b.cli().Read(context.TODO(), &pb.BlobReadRequest{
Key: key,
Options: &pb.BlobOptions{
Namespace: options.Namespace,
},
}, client.WithAuthToken())
// handle the error
if verr := errors.FromError(err); verr != nil && verr.Code == http.StatusNotFound {
return nil, store.ErrNotFound
} else if verr != nil {
return nil, verr
} else if err != nil {
return nil, err
}
// create a buffer to store the bytes in
buf := bytes.NewBuffer(nil)
// keep recieving bytes from the stream until it's closed by the server
for {
res, err := stream.Recv()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
buf.Write(res.Blob)
}
// return the bytes
return buf, nil
}
func (b *blob) Write(key string, blob io.Reader, opts ...store.BlobOption) error {
// validate the key
if len(key) == 0 {
return store.ErrMissingKey
}
// parse the options
var options store.BlobOptions
for _, o := range opts {
o(&options)
}
// setup a context
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
// open the stream
stream, err := b.cli().Write(ctx, client.WithAuthToken())
if verr := errors.FromError(err); verr != nil {
return verr
} else if err != nil {
return err
}
// read from the blob and stream it to the server
buffer := make([]byte, bufferSize)
for {
num, err := blob.Read(buffer)
if err == io.EOF {
break
} else if err != nil {
return err
}
req := &pb.BlobWriteRequest{
Key: key,
Options: &pb.BlobOptions{
Namespace: options.Namespace,
Public: options.Public,
ContentType: options.ContentType,
},
Blob: buffer[:num],
}
if err := stream.Send(req); err != nil {
return err
}
}
// wait for the server to process the blob
_, err = stream.CloseAndRecv()
return err
}
func (b *blob) Delete(key string, opts ...store.BlobOption) error {
// validate the key
if len(key) == 0 {
return store.ErrMissingKey
}
// parse the options
var options store.BlobOptions
for _, o := range opts {
o(&options)
}
// execute the rpc
_, err := b.cli().Delete(context.TODO(), &pb.BlobDeleteRequest{
Key: key,
Options: &pb.BlobOptions{
Namespace: options.Namespace,
},
}, client.WithAuthToken())
// handle the error
if verr := errors.FromError(err); verr != nil && verr.Code == http.StatusNotFound {
return store.ErrNotFound
} else if verr != nil {
return verr
}
return err
}
func (b *blob) cli() pb.BlobStoreService {
if b.client == nil {
b.client = pb.NewBlobStoreService("store", client.DefaultClient)
}
return b.client
}