This repository was archived by the owner on Oct 18, 2021. It is now read-only.
generated from beyondstorage/go-service-example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
75 lines (60 loc) · 1.76 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
package azblob
import (
"context"
"github.com/Azure/azure-storage-blob-go/azblob"
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, azblob.Metadata{}, azblob.PublicAccessNone)
if err != nil {
return nil, err
}
return st, nil
}
func (s *Service) delete(ctx context.Context, name string, opt pairServiceDelete) (err error) {
bucket := s.service.NewContainerURL(name)
_, err = bucket.Delete(ctx, azblob.ContainerAccessConditions{})
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) {
input := &storagePageStatus{
maxResults: 200,
}
return typ.NewStoragerIterator(ctx, s.nextStoragePage, input), nil
}
func (s *Service) nextStoragePage(ctx context.Context, page *typ.StoragerPage) error {
input := page.Status.(*storagePageStatus)
output, err := s.service.ListContainersSegment(ctx, input.marker, azblob.ListContainersSegmentOptions{
MaxResults: input.maxResults,
})
if err != nil {
return err
}
for _, v := range output.ContainerItems {
store, err := s.newStorage(ps.WithName(v.Name))
if err != nil {
return err
}
page.Data = append(page.Data, store)
}
if !output.NextMarker.NotDone() {
return typ.IterateDone
}
input.marker = output.NextMarker
return nil
}