-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbase.go
59 lines (47 loc) · 1.18 KB
/
base.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
package gcs
import (
"context"
"io"
"cloud.google.com/go/storage"
"github.com/yunify/qscamel/model"
"github.com/yunify/qscamel/utils"
)
// Name implement base.Read
func (c *Client) Name(ctx context.Context) (name string) {
return "gcs:" + c.BucketName
}
// Read implement source.Read
func (c *Client) Read(ctx context.Context, p string) (r io.Reader, err error) {
cp := utils.Join(c.Path, p)
o := c.client.Object(cp)
return o.NewReader(ctx)
}
// ReadRange implement source.ReadRange
func (c *Client) ReadRange(
ctx context.Context, p string, offset, size int64,
) (r io.Reader, err error) {
cp := utils.Join(c.Path, p)
r, err = c.client.Object(cp).NewRangeReader(ctx, offset, size)
if err != nil {
return
}
return
}
// Stat implement source.Stat and destination.Stat
func (c *Client) Stat(ctx context.Context, p string) (o *model.SingleObject, err error) {
cp := utils.Join(c.Path, p)
resp, err := c.client.Object(cp).Attrs(ctx)
if err != nil {
if err == storage.ErrObjectNotExist {
return nil, nil
}
return
}
o = &model.SingleObject{
Key: p,
Size: resp.Size,
LastModified: resp.Updated.Unix(),
MD5: string(resp.MD5),
}
return
}