This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
generated from beyondstorage/go-service-example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
64 lines (56 loc) · 1.48 KB
/
service.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
60
61
62
63
64
package gcs
import (
"context"
"google.golang.org/api/iterator"
ps "github.com/beyondstorage/go-storage/v4/pairs"
typ "github.com/beyondstorage/go-storage/v4/types"
)
func (s *Service) create(ctx context.Context, name string, opt pairServiceCreate) (store typ.Storager, err error) {
st, err := s.newStorage(ps.WithName(name))
if err != nil {
return nil, err
}
err = st.bucket.Create(ctx, s.projectID, nil)
if err != nil {
return nil, err
}
return st, nil
}
func (s *Service) delete(ctx context.Context, name string, opt pairServiceDelete) (err error) {
store, err := s.newStorage(ps.WithName(name))
if err != nil {
return err
}
err = store.bucket.Delete(ctx)
if err != nil {
return err
}
return nil
}
func (s *Service) get(ctx context.Context, name string, opt pairServiceGet) (store typ.Storager, err error) {
st, err := s.newStorage(ps.WithName(name))
if err != nil {
return nil, err
}
return st, nil
}
func (s *Service) list(ctx context.Context, opt pairServiceList) (it *typ.StoragerIterator, err error) {
return typ.NewStoragerIterator(ctx, s.nextStoragePage, nil), nil
}
func (s *Service) nextStoragePage(ctx context.Context, page *typ.StoragerPage) error {
it := s.service.Buckets(ctx, s.projectID)
for {
bucket, err := it.Next()
if err == iterator.Done {
return typ.IterateDone
}
if err != nil {
return err
}
store, err := s.newStorage(ps.WithName(bucket.Name))
if err != nil {
return err
}
page.Data = append(page.Data, store)
}
}