-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathclient.go
43 lines (33 loc) · 899 Bytes
/
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
package client
import (
"context"
"cloud.google.com/go/storage"
"github.com/pkg/errors"
"google.golang.org/api/option"
)
type Writer interface {
WriteData([]byte) error
Close() error
}
type GCSWriter struct {
writer *storage.Writer
}
func NewWriter(ctx context.Context, serviceAccountJSON []byte, bucketname string, filepath string) (*GCSWriter, error) {
client, err := storage.NewClient(ctx, option.WithCredentialsJSON(serviceAccountJSON))
if err != nil {
return nil, errors.Wrap(err, "error in creating client")
}
writer := client.Bucket(bucketname).Object(filepath).NewWriter(ctx)
return &GCSWriter{
writer: writer,
}, nil
}
func (c *GCSWriter) WriteData(data []byte) error {
if _, err := c.writer.Write(data); err != nil {
return errors.Wrap(err, "error in writing data to an object")
}
return nil
}
func (c *GCSWriter) Close() error {
return c.writer.Close()
}