Revision 3dabe5d2 kamaki/clients/storage.py
b/kamaki/clients/storage.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A. |
33 | 33 |
|
34 | 34 |
from kamaki.clients import Client, ClientError |
35 |
from kamaki.clients.utils import filter_in, filter_out, prefix_keys, path4url
|
|
36 |
#from .connection.kamakicon import KamakiHTTPConnection |
|
35 |
from kamaki.clients.utils import filter_in, filter_out, path4url |
|
36 |
|
|
37 | 37 |
|
38 | 38 |
class StorageClient(Client): |
39 | 39 |
"""OpenStack Object Storage API 1.0 client""" |
40 | 40 |
|
41 | 41 |
def __init__(self, base_url, token, account=None, container=None): |
42 | 42 |
super(StorageClient, self).__init__(base_url, token) |
43 |
#super(StorageClient, self).__init__(base_url, token, http_client=KamakiHTTPConnection()) |
|
44 | 43 |
self.account = account |
45 | 44 |
self.container = container |
46 | 45 |
|
... | ... | |
65 | 64 |
def replace_account_meta(self, metapairs): |
66 | 65 |
self.assert_account() |
67 | 66 |
path = path4url(self.account) |
68 |
for key, val in metapairs:
|
|
69 |
self.set_header('X-Account-Meta-'+key, val)
|
|
67 |
for key, val in metapairs: |
|
68 |
self.set_header('X-Account-Meta-' + key, val)
|
|
70 | 69 |
r = self.post(path, success=202) |
70 |
r.release() |
|
71 | 71 |
|
72 | 72 |
def del_account_meta(self, metakey): |
73 | 73 |
headers = self.get_account_info() |
74 |
self.headers = filter_out(headers, 'X-Account-Meta-'+metakey, exactMatch = True) |
|
74 |
self.headers = filter_out(headers, |
|
75 |
'X-Account-Meta-' + metakey, |
|
76 |
exactMatch=True) |
|
75 | 77 |
if len(self.headers) == len(headers): |
76 | 78 |
raise ClientError('X-Account-Meta-%s not found' % metakey, 404) |
77 | 79 |
path = path4url(self.account) |
78 |
r = self.post(path, success = 202) |
|
80 |
r = self.post(path, success=202) |
|
81 |
r.release() |
|
79 | 82 |
|
80 | 83 |
def create_container(self, container): |
81 | 84 |
self.assert_account() |
... | ... | |
106 | 109 |
self.assert_account() |
107 | 110 |
self.set_param('format', 'json') |
108 | 111 |
path = path4url(self.account) |
109 |
r = self.get(path, success = (200, 204))
|
|
112 |
r = self.get(path, success=(200, 204))
|
|
110 | 113 |
reply = r.json |
111 | 114 |
return reply |
112 | 115 |
|
... | ... | |
117 | 120 |
path = path4url(self.account, self.container, object) |
118 | 121 |
data = f.read(size) if size is not None else f.read() |
119 | 122 |
r = self.put(path, data=data, success=201) |
123 |
r.release() |
|
120 | 124 |
|
121 | 125 |
def create_directory(self, object): |
122 | 126 |
self.assert_container() |
... | ... | |
124 | 128 |
self.set_header('Content-Type', 'application/directory') |
125 | 129 |
self.set_header('Content-length', '0') |
126 | 130 |
r = self.put(path, success=201) |
131 |
r.release() |
|
127 | 132 |
|
128 | 133 |
def get_object_info(self, object): |
129 | 134 |
self.assert_container() |
... | ... | |
142 | 147 |
|
143 | 148 |
def del_object_meta(self, metakey, object): |
144 | 149 |
self.assert_container() |
145 |
self.set_header('X-Object-Meta-'+metakey, '')
|
|
150 |
self.set_header('X-Object-Meta-' + metakey, '')
|
|
146 | 151 |
path = path4url(self.account, self.container, object) |
147 |
r = self.post(path, success = 202) |
|
152 |
r = self.post(path, success=202) |
|
153 |
r.release() |
|
148 | 154 |
|
149 | 155 |
def replace_object_meta(self, metapairs): |
150 | 156 |
self.assert_container() |
151 |
path=path4url(self.account, self.container)
|
|
157 |
path = path4url(self.account, self.container)
|
|
152 | 158 |
for key, val in metapairs: |
153 |
self.set_header('X-Object-Meta-'+key, val)
|
|
159 |
self.set_header('X-Object-Meta-' + key, val)
|
|
154 | 160 |
r = self.post(path, success=202) |
161 |
r.release() |
|
155 | 162 |
|
156 | 163 |
def get_object(self, object): |
157 | 164 |
self.assert_container() |
... | ... | |
161 | 168 |
cnt = r.content |
162 | 169 |
return cnt, size |
163 | 170 |
|
164 |
def copy_object(self, src_container, src_object, dst_container, dst_object=False): |
|
171 |
def copy_object(self, src_container, src_object, dst_container, |
|
172 |
dst_object=False): |
|
165 | 173 |
self.assert_account() |
166 | 174 |
dst_object = dst_object or src_object |
167 | 175 |
dst_path = path4url(self.account, dst_container, dst_object) |
168 | 176 |
self.set_header('X-Copy-From', path4url(src_container, src_object)) |
169 | 177 |
self.set_header('Content-Length', 0) |
170 | 178 |
r = self.put(dst_path, success=201) |
179 |
r.release() |
|
171 | 180 |
|
172 |
def move_object(self, src_container, src_object, dst_container, dst_object=False): |
|
181 |
def move_object(self, src_container, src_object, dst_container, |
|
182 |
dst_object=False): |
|
173 | 183 |
self.assert_account() |
174 | 184 |
dst_object = dst_object or src_object |
175 | 185 |
dst_path = path4url(self.account, dst_container, dst_object) |
176 | 186 |
self.set_header('X-Move-From', path4url(src_container, src_object)) |
177 | 187 |
self.set_header('Content-Length', 0) |
178 | 188 |
r = self.put(dst_path, success=201) |
189 |
r.release() |
|
179 | 190 |
|
180 | 191 |
def delete_object(self, object): |
181 | 192 |
self.assert_container() |
182 | 193 |
path = path4url(self.account, self.container, object) |
183 | 194 |
r = self.delete(path, success=(204, 404)) |
184 | 195 |
if r.status_code == 404: |
185 |
raise ClientError("Object %s not found" %object, r.status_code) |
|
186 |
|
|
196 |
raise ClientError("Object %s not found" % object, r.status_code)
|
|
197 |
|
|
187 | 198 |
def list_objects(self): |
188 | 199 |
self.assert_container() |
189 | 200 |
path = path4url(self.account, self.container) |
190 | 201 |
self.set_param('format', 'json') |
191 | 202 |
r = self.get(path, success=(200, 204, 304, 404), ) |
192 | 203 |
if r.status_code == 404: |
193 |
raise ClientError("Incorrect account (%s) for that container"%self.account, r.status_code) |
|
204 |
raise ClientError(\ |
|
205 |
"Incorrect account (%s) for that container" % self.account, |
|
206 |
r.status_code) |
|
194 | 207 |
elif r.status_code == 304: |
195 | 208 |
return [] |
196 | 209 |
reply = r.json |
... | ... | |
203 | 216 |
self.set_param('path', 'path_prefix') |
204 | 217 |
r = self.get(path, success=(200, 204, 404)) |
205 | 218 |
if r.status_code == 404: |
206 |
raise ClientError("Incorrect account (%s) for that container"%self.account, r.status_code) |
|
219 |
raise ClientError(\ |
|
220 |
"Incorrect account (%s) for that container" % self.account, |
|
221 |
r.status_code) |
|
207 | 222 |
reply = r.json |
208 | 223 |
return reply |
209 |
|
|
210 |
|
Also available in: Unified diff