forked from graymeta/stow
/
container.go
178 lines (156 loc) · 3.99 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
package swift
import (
"bytes"
"fmt"
"io"
"strings"
"unicode/utf8"
"github.com/google/uuid"
"github.com/ncw/swift"
"github.com/pkg/errors"
"gomodules.xyz/stow"
)
type container struct {
id string
client *swift.Connection
}
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) {
return c.getItem(id)
}
func (c *container) Browse(prefix, delimiter, cursor string, count int) (*stow.ItemPage, error) {
params := &swift.ObjectsOpts{
Limit: count,
Marker: cursor,
Prefix: prefix,
}
r, sz := utf8.DecodeRuneInString(delimiter)
if r == utf8.RuneError {
if sz > 0 {
return nil, fmt.Errorf("Bad delimiter %v", delimiter)
}
} else {
params.Delimiter = r
}
objects, err := c.client.Objects(c.id, params)
if err != nil {
return nil, err
}
var prefixes []string
for _, obj := range objects {
if obj.PseudoDirectory {
prefixes = append(prefixes, obj.Name)
}
}
var items []stow.Item
for _, obj := range objects {
if obj.PseudoDirectory {
continue
}
items = append(items, &item{
id: obj.Name,
container: c,
client: c.client,
hash: obj.Hash,
size: obj.Bytes,
lastModified: obj.LastModified,
})
}
marker := ""
if len(objects) == count {
marker = objects[len(objects)-1].Name
}
return &stow.ItemPage{Prefixes: prefixes, Items: items, Cursor: marker}, 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) {
mdPrepped, err := prepMetadata(metadata)
if err != nil {
return nil, errors.Wrap(err, "unable to create or update Item, preparing metadata")
}
headers, err := c.client.ObjectPut(c.id, name, r, false, "", "", mdPrepped)
if err != nil {
return nil, errors.Wrap(err, "unable to create or update Item")
}
mdParsed, err := parseMetadata(headers)
if err != nil {
return nil, errors.Wrap(err, "unable to create or update Item, parsing metadata")
}
item := &item{
id: name,
container: c,
client: c.client,
size: size,
metadata: mdParsed,
}
return item, nil
}
func (c *container) RemoveItem(id string) error {
return c.client.ObjectDelete(c.id, id)
}
func (c *container) getItem(id string) (*item, error) {
info, headers, err := c.client.Object(c.id, id)
if err != nil {
if strings.Contains(err.Error(), "Object Not Found") {
return nil, stow.ErrNotFound
}
return nil, errors.Wrap(err, "error retrieving item")
}
md, err := parseMetadata(headers)
if err != nil {
return nil, errors.Wrap(err, "unable to retrieve Item information, parsing metadata")
}
item := &item{
id: id,
container: c,
client: c.client,
hash: info.Hash,
size: info.Bytes,
lastModified: info.LastModified,
metadata: md,
}
return item, nil
}
// Keys are returned as all lowercase, dashes are allowed
func parseMetadata(md swift.Headers) (map[string]interface{}, error) {
m := make(map[string]interface{}, len(md))
for key, value := range md.ObjectMetadata() {
m[key] = value
}
return m, nil
}
// TODO determine invalid keys.
func prepMetadata(md map[string]interface{}) (map[string]string, error) {
m := make(map[string]string, len(md))
for key, value := range md {
str, ok := value.(string)
if !ok {
return nil, errors.New("could not convert key value") // add a msg mentioning strings only?
}
m["X-Object-Meta-"+key] = str
}
return m, nil
}
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
}