forked from graymeta/stow
/
container.go
219 lines (186 loc) · 5.61 KB
/
container.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package azure
import (
"bytes"
"io"
"strings"
"time"
az "github.com/Azure/azure-sdk-for-go/storage"
"github.com/google/uuid"
"github.com/pkg/errors"
"gomodules.xyz/stow"
)
// The maximum size of an object that can be Put in a single request
const maxPutSize = 256 * 1024 * 1024
// timeFormat is the time format for azure.
var timeFormat = "Mon, 2 Jan 2006 15:04:05 MST"
type container struct {
id string
properties az.ContainerProperties
client *az.BlobStorageClient
}
var _ stow.Container = (*container)(nil)
func (c *container) ID() string {
return c.id
}
func (c *container) Name() string {
return c.id
}
func (c *container) Item(id string) (stow.Item, error) {
blob := c.client.GetContainerReference(c.id).GetBlobReference(id)
err := blob.GetProperties(nil)
if err != nil {
if strings.Contains(err.Error(), "404") {
return nil, stow.ErrNotFound
}
return nil, err
}
item := &item{
id: id,
container: c,
client: c.client,
properties: blob.Properties,
}
etag := cleanEtag(item.properties.Etag) // Etags returned from this method include quotes. Strip them.
item.properties.Etag = etag // Assign the corrected string value to the field.
return item, nil
}
func (c *container) Browse(prefix, delimiter, cursor string, count int) (*stow.ItemPage, error) {
params := az.ListBlobsParameters{
Prefix: prefix,
Delimiter: delimiter,
MaxResults: uint(count),
}
if cursor != "" {
params.Marker = cursor
}
listblobs, err := c.client.GetContainerReference(c.id).ListBlobs(params)
if err != nil {
return nil, err
}
prefixes := make([]string, len(listblobs.BlobPrefixes))
for i, prefix := range listblobs.BlobPrefixes {
prefixes[i] = prefix
}
items := make([]stow.Item, len(listblobs.Blobs))
for i, blob := range listblobs.Blobs {
// Clean Etag just in case.
blob.Properties.Etag = cleanEtag(blob.Properties.Etag)
items[i] = &item{
id: blob.Name,
container: c,
client: c.client,
properties: blob.Properties,
}
}
return &stow.ItemPage{Prefixes: prefixes, Items: items, Cursor: listblobs.NextMarker}, nil
}
func (c *container) Items(prefix, cursor string, count int) ([]stow.Item, string, error) {
page, err := c.Browse(prefix, "", cursor, count)
if err != nil {
return nil, "", err
}
return page.Items, page.Cursor, err
}
func (c *container) Put(name string, r io.Reader, size int64, metadata map[string]interface{}) (stow.Item, error) {
mdParsed, err := prepMetadata(metadata)
if err != nil {
return nil, errors.Wrap(err, "unable to create or update Item, preparing metadata")
}
name = strings.Replace(name, " ", "+", -1)
if size <= 0 || size > maxPutSize {
// Do a multipart upload
err := c.multipartUpload(name, r, size)
if err != nil {
return nil, errors.Wrap(err, "multipart upload")
}
} else {
err = c.client.GetContainerReference(c.id).GetBlobReference(name).CreateBlockBlobFromReader(r, nil)
if err != nil {
return nil, errors.Wrap(err, "unable to create or update Item")
}
}
err = c.SetItemMetadata(name, mdParsed)
if err != nil {
return nil, errors.Wrap(err, "unable to create or update item, setting Item metadata")
}
item := &item{
id: name,
container: c,
client: c.client,
properties: az.BlobProperties{
LastModified: az.TimeRFC1123(time.Now()),
Etag: "",
ContentLength: size,
},
}
return item, nil
}
func (c *container) SetItemMetadata(itemName string, md map[string]string) error {
blob := c.client.GetContainerReference(c.id).GetBlobReference(itemName)
blob.Metadata = md
return blob.SetMetadata(nil)
}
func parseMetadata(md map[string]string) (map[string]interface{}, error) {
rtnMap := make(map[string]interface{}, len(md))
for key, value := range md {
rtnMap[key] = value
}
return rtnMap, nil
}
func prepMetadata(md map[string]interface{}) (map[string]string, error) {
rtnMap := make(map[string]string, len(md))
for key, value := range md {
str, ok := value.(string)
if !ok {
return nil, errors.Errorf(`value of key '%s' in metadata must be of type string`, key)
}
rtnMap[key] = str
}
return rtnMap, nil
}
func (c *container) RemoveItem(id string) error {
return c.client.GetContainerReference(c.id).GetBlobReference(id).Delete(nil)
}
// Remove quotation marks from beginning and end. This includes quotations that
// are escaped. Also removes leading `W/` from prefix for weak Etags.
//
// Based on the Etag spec, the full etag value (<FULL ETAG VALUE>) can include:
// - W/"<ETAG VALUE>"
// - "<ETAG VALUE>"
// - ""
// Source: https://tools.ietf.org/html/rfc7232#section-2.3
//
// Based on HTTP spec, forward slash is a separator and must be enclosed in
// quotes to be used as a valid value. Hence, the returned value may include:
// - "<FULL ETAG VALUE>"
// - \"<FULL ETAG VALUE>\"
// Source: https://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.2
//
// This function contains a loop to check for the presence of the three possible
// filler characters and strips them, resulting in only the Etag value.
func cleanEtag(etag string) string {
for {
// Check if the filler characters are present
if strings.HasPrefix(etag, `\"`) {
etag = strings.Trim(etag, `\"`)
} else if strings.HasPrefix(etag, `"`) {
etag = strings.Trim(etag, `"`)
} else if strings.HasPrefix(etag, `W/`) {
etag = strings.Replace(etag, `W/`, "", 1)
} else {
break
}
}
return etag
}
func (c *container) HasWriteAccess() error {
r := bytes.NewReader([]byte("CheckBucketAccess"))
item, err := c.Put(".trash/"+uuid.New().String(), r, r.Size(), nil)
if err != nil {
return err
}
if err := c.RemoveItem(item.ID()); err != nil {
return err
}
return nil
}