-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreator.go
59 lines (51 loc) · 1.79 KB
/
creator.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 gcstorage
import (
"context"
"cloud.google.com/go/storage"
"github.com/Nivl/go-filestorage"
"google.golang.org/api/option"
)
// Makes sure Creator is a logger.Creator
var _ filestorage.Creator = (*Creator)(nil)
// NewCreator returns a filestorage creator that will use the provided key
// to create a new google storage client that will be reused for every
// new gcstorage instance
func NewCreator(apiKey string, defaultBucket string) (*Creator, error) {
return NewCreatorWithContext(context.Background(), apiKey, defaultBucket)
}
// NewCreatorWithContext returns a filestorage creator that will
// use the provided context and key to create a new google storage
// client that will be reused for every new gcstorage instance
// The provided context will be used as default context for all new FileStorage
// instance
func NewCreatorWithContext(ctx context.Context, apiKey string, defaultBucket string) (*Creator, error) {
client, err := storage.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
return nil, err
}
return &Creator{
client: client,
defaultCtx: ctx,
defaultBucket: defaultBucket,
}, nil
}
// Creator creates new filestorage
type Creator struct {
client *storage.Client
defaultCtx context.Context
defaultBucket string
}
// New returns a new le client
func (c *Creator) New() (filestorage.FileStorage, error) {
fs := NewWithClient(c.defaultCtx, c.client)
err := fs.SetBucket(c.defaultBucket)
return fs, err
}
// NewWithContext returns a new gc storage client using the provided context as
// default context instead of the one provided during the creation of the
// Creator
func (c *Creator) NewWithContext(ctx context.Context) (filestorage.FileStorage, error) {
fs := NewWithClient(ctx, c.client)
err := fs.SetBucket(c.defaultBucket)
return fs, err
}