generated from beyondstorage/go-service-example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
service.go
92 lines (73 loc) · 2.06 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package qingstor
import (
"context"
"github.com/qingstor/qingstor-sdk-go/v4/service"
ps "github.com/beyondstorage/go-storage/v4/pairs"
. "github.com/beyondstorage/go-storage/v4/types"
)
func (s *Service) create(ctx context.Context, name string, opt pairServiceCreate) (store Storager, err error) {
// ServicePairCreate requires location, so we don't need to add location into pairs
pairs := append(opt.pairs, ps.WithName(name))
st, err := s.newStorage(pairs...)
if err != nil {
return
}
_, err = st.bucket.PutWithContext(ctx)
if err != nil {
return
}
return st, nil
}
func (s *Service) delete(ctx context.Context, name string, opt pairServiceDelete) (err error) {
pairs := append(opt.pairs, ps.WithName(name))
store, err := s.newStorage(pairs...)
if err != nil {
return
}
_, err = store.bucket.DeleteWithContext(ctx)
if err != nil {
return
}
return nil
}
func (s *Service) get(ctx context.Context, name string, opt pairServiceGet) (store Storager, err error) {
pairs := append(opt.pairs, ps.WithName(name))
store, err = s.newStorage(pairs...)
if err != nil {
return
}
return
}
func (s *Service) list(ctx context.Context, opt pairServiceList) (it *StoragerIterator, err error) {
input := &storagePageStatus{}
if opt.HasLocation {
input.location = opt.Location
}
return NewStoragerIterator(ctx, s.nextStoragePage, input), nil
}
func (s *Service) nextStoragePage(ctx context.Context, page *StoragerPage) error {
input := page.Status.(*storagePageStatus)
serviceInput := &service.ListBucketsInput{
Limit: &input.offset,
Offset: &input.limit,
}
if input.location != "" {
serviceInput.Location = &input.location
}
output, err := s.service.ListBucketsWithContext(ctx, serviceInput)
if err != nil {
return err
}
for _, v := range output.Buckets {
store, err := s.newStorage(ps.WithName(*v.Name), ps.WithLocation(*v.Location))
if err != nil {
return err
}
page.Data = append(page.Data, store)
}
input.offset += len(output.Buckets)
if input.offset >= service.IntValue(output.Count) {
return IterateDone
}
return nil
}