/
item.go
110 lines (92 loc) · 2.21 KB
/
item.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package azure
import (
"io"
"net/url"
"sync"
"time"
az "github.com/Azure/azure-sdk-for-go/storage"
"github.com/graymeta/stow"
"github.com/pkg/errors"
)
type item struct {
id string
container *container
client *az.BlobStorageClient
properties az.BlobProperties
url url.URL
metadata map[string]interface{}
infoOnce sync.Once
infoErr error
}
var (
_ stow.Item = (*item)(nil)
_ stow.ItemRanger = (*item)(nil)
)
func (i *item) ID() string {
return i.id
}
func (i *item) Name() string {
return i.id
}
func (i *item) URL() *url.URL {
u := i.client.GetContainerReference(i.container.id).GetBlobReference(i.id).GetURL()
url, _ := url.Parse(u)
url.Scheme = "azure"
return url
}
func (i *item) Size() (int64, error) {
return i.properties.ContentLength, nil
}
func (i *item) Open() (io.ReadCloser, error) {
return i.client.GetContainerReference(i.container.id).GetBlobReference(i.id).Get(nil)
}
func (i *item) ETag() (string, error) {
return i.properties.Etag, nil
}
func (i *item) LastMod() (time.Time, error) {
return time.Time(i.properties.LastModified), nil
}
func (i *item) Metadata() (map[string]interface{}, error) {
err := i.ensureInfo()
if err != nil {
return nil, errors.Wrap(err, "retrieving metadata")
}
return i.metadata, nil
}
func (i *item) ensureInfo() error {
if i.metadata == nil {
i.infoOnce.Do(func() {
blob := i.client.GetContainerReference(i.container.Name()).GetBlobReference(i.Name())
infoErr := blob.GetMetadata(nil)
if infoErr != nil {
i.infoErr = infoErr
return
}
mdParsed, infoErr := parseMetadata(blob.Metadata)
if infoErr != nil {
i.infoErr = infoErr
return
}
i.metadata = mdParsed
})
}
return i.infoErr
}
func (i *item) getInfo() (stow.Item, error) {
itemInfo, err := i.container.Item(i.ID())
if err != nil {
return nil, err
}
return itemInfo, nil
}
// OpenRange opens the item for reading starting at byte start and ending
// at byte end.
func (i *item) OpenRange(start, end uint64) (io.ReadCloser, error) {
opts := &az.GetBlobRangeOptions{
Range: &az.BlobRange{
Start: start,
End: end,
},
}
return i.client.GetContainerReference(i.container.id).GetBlobReference(i.id).GetRange(opts)
}