forked from raystack/meteor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
44 lines (34 loc) · 897 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
44
package client
import (
"context"
"fmt"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
)
type Writer interface {
WriteData([]byte) error
Close() error
}
type GCSWriter struct {
writer *storage.Writer
}
// NewWriter creates a new GCS writer.
func NewWriter(ctx context.Context, serviceAccountJSON []byte, bucketname, filepath string) (*GCSWriter, error) {
client, err := storage.NewClient(ctx, option.WithCredentialsJSON(serviceAccountJSON))
if err != nil {
return nil, fmt.Errorf("create client: %w", err)
}
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 fmt.Errorf("write data to an object: %w", err)
}
return nil
}
func (c *GCSWriter) Close() error {
return c.writer.Close()
}