-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
44 lines (35 loc) · 1.19 KB
/
client.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 cs
import (
"context"
"io"
"cloud.google.com/go/storage"
"github.com/m-mizutani/goerr"
"github.com/m-mizutani/hatchery/pkg/domain/interfaces"
"github.com/m-mizutani/hatchery/pkg/domain/model"
"google.golang.org/api/option"
)
type Client struct {
client *storage.Client
}
var _ interfaces.CloudStorage = (*Client)(nil)
func New(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
c, err := storage.NewClient(ctx, opts...)
if err != nil {
return nil, goerr.Wrap(err, "fail to create Google Cloud Storage client")
}
return &Client{
client: c,
}, nil
}
// NewObjectReader implements interfaces.CloudStorage.
func (c *Client) NewObjectReader(ctx context.Context, bucket model.CSBucket, object model.CSObjectName) (io.ReadCloser, error) {
r, err := c.client.Bucket(string(bucket)).Object(string(object)).NewReader(ctx)
if err != nil {
return nil, goerr.Wrap(err, "fail to create object reader")
}
return r, nil
}
// NewObjectWriter implements interfaces.CloudStorage.
func (c *Client) NewObjectWriter(ctx context.Context, bucket model.CSBucket, object model.CSObjectName) io.WriteCloser {
return c.client.Bucket(string(bucket)).Object(string(object)).NewWriter(ctx)
}