-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobject_store.py
More file actions
313 lines (271 loc) · 9.26 KB
/
Copy pathobject_store.py
File metadata and controls
313 lines (271 loc) · 9.26 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""Object Store v1 API Library"""
import six
try:
from urllib.parse import urlparse # noqa
except ImportError:
from urlparse import urlparse # noqa
from openstackclient.api import api
class APIv1(api.BaseAPI):
"""Object Store v1 API"""
def __init__(self, **kwargs):
super(APIv1, self).__init__(**kwargs)
def container_create(
self,
container=None,
):
"""Create a container
:param container: name of container to create
:returns: dict of returned headers
"""
response = self.create(container, method='PUT')
url_parts = urlparse(self.endpoint)
data = {
'account': url_parts.path.split('/')[-1],
'container': container,
'x-trans-id': response.headers.get('x-trans-id', None),
}
return data
def container_delete(
self,
container=None,
):
"""Delete a container
:param container: name of container to delete
"""
if container:
self.delete(container)
def container_list(
self,
all_data=False,
marker=None,
limit=None,
end_marker=None,
prefix=None,
**params
):
"""Get containers in an account
:param all_data: if True, return a full listing, else returns a max
of 10000 listings
:param marker: marker query
:param limit: limit query
:param end_marker: end_marker query
:param prefix: prefix query
:returns: list of container names
"""
params['format'] = 'json'
if all_data:
data = listing = self.container_list(
marker=marker,
limit=limit,
end_marker=end_marker,
prefix=prefix,
**params
)
while listing:
# TODO(dtroyer): How can we use name here instead?
marker = listing[-1]['name']
listing = self.container_list(
marker=marker,
limit=limit,
end_marker=end_marker,
prefix=prefix,
**params
)
if listing:
data.extend(listing)
return data
if marker:
params['marker'] = marker
if limit:
params['limit'] = limit
if end_marker:
params['end_marker'] = end_marker
if prefix:
params['prefix'] = prefix
return self.list('', **params)
def container_show(
self,
session=None,
container=None,
):
"""Get container details
:param session: a requests.Session
:param container: name of container to show
:returns: dict of returned headers
"""
if not session:
session = self.session
if not session:
# TODO(dtroyer): sort this
raise Exception
response = self._request('HEAD', container)
data = {
'account': response.headers.get('x-container-meta-owner', None),
'container': container,
'object_count': response.headers.get(
'x-container-object-count',
None,
),
'bytes_used': response.headers.get('x-container-bytes-used', None),
'read_acl': response.headers.get('x-container-read', None),
'write_acl': response.headers.get('x-container-write', None),
'sync_to': response.headers.get('x-container-sync-to', None),
'sync_key': response.headers.get('x-container-sync-key', None),
}
return data
def object_create(
self,
container=None,
object=object,
):
"""Create an object inside a container
:param container: name of container to store object
:param object: local path to object
:returns: dict of returned headers
"""
full_url = "%s/%s" % (container, object)
response = self.create(full_url, method='PUT', data=open(object))
url_parts = urlparse(self.endpoint)
data = {
'account': url_parts.path.split('/')[-1],
'container': container,
'object': object,
'x-trans-id': response.headers.get('X-Trans-Id', None),
'etag': response.headers.get('Etag', None),
}
return data
def object_delete(
self,
container=None,
object=None,
):
"""Delete an object from a container
:param container: name of container that stores object
:param object: name of object to delete
"""
self.delete("%s/%s" % (container, object))
def object_list(
self,
container=None,
all_data=False,
marker=None,
limit=None,
end_marker=None,
delimiter=None,
prefix=None,
**params
):
"""List objects in a container
:param container: container name to get a listing for
:param all_data: if True, return a full listing, else returns a max
of 10000 listings
:param marker: marker query
:param limit: limit query
:param end_marker: marker query
:param delimiter: string to delimit the queries on
:param prefix: prefix query
:returns: a tuple of (response headers, a list of objects) The response
headers will be a dict and all header names will be lowercase.
"""
# params['format'] = 'json'
if all_data:
data = listing = self.object_list(
container=container,
marker=marker,
limit=limit,
end_marker=end_marker,
delimiter=delimiter,
prefix=prefix,
**params
)
while listing:
if delimiter:
marker = listing[-1].get('name', listing[-1].get('subdir'))
else:
marker = listing[-1]['name']
listing = self.object_list(
container=container,
marker=marker,
limit=limit,
end_marker=end_marker,
delimiter=delimiter,
prefix=prefix,
**params
)
if listing:
data.extend(listing)
return data
params = {}
if marker:
params['marker'] = marker
if limit:
params['limit'] = limit
if end_marker:
params['end_marker'] = end_marker
if delimiter:
params['delimiter'] = delimiter
if prefix:
params['prefix'] = prefix
return self.list(container, **params)
def object_show(
self,
session=None,
container=None,
object=None,
):
"""Get object details
:param session: a requests.Session
:param container: container name for object to get
:param object: name of object to get
:returns: dict of object properties
"""
if not session:
session = self.session
if not session:
# TODO(dtroyer): sort this
raise Exception
response = self._request('HEAD', "%s/%s" % (container, object))
data = {
'account': response.headers.get('x-container-meta-owner', None),
'container': container,
'object': object,
'content-type': response.headers.get('content-type', None),
}
if 'content-length' in response.headers:
data['content-length'] = response.headers.get(
'content-length',
None,
)
if 'last-modified' in response.headers:
data['last-modified'] = response.headers.get('last-modified', None)
if 'etag' in response.headers:
data['etag'] = response.headers.get('etag', None)
if 'x-object-manifest' in response.headers:
data['x-object-manifest'] = response.headers.get(
'x-object-manifest', None)
for key, value in six.iteritems(response.headers):
if key.startswith('x-object-meta-'):
data[key[len('x-object-meta-'):].lower()] = value
elif key not in (
'content-type',
'content-length',
'last-modified',
'etag',
'date',
'x-object-manifest',
'x-container-meta-owner',
):
data[key.lower()] = value
return data