@@ -145,6 +145,7 @@ class SwiftStorage(Storage):
145
145
_token = ''
146
146
name_prefix = setting ('SWIFT_NAME_PREFIX' , '' )
147
147
full_listing = setting ('SWIFT_FULL_LISTING' , True )
148
+ max_retries = setting ('SWIFT_MAX_RETRIES' , 5 )
148
149
149
150
def __init__ (self , ** settings ):
150
151
# check if some of the settings provided as class attributes
@@ -169,21 +170,19 @@ def __init__(self, **settings):
169
170
}
170
171
self .os_options .update (self .os_extra_options )
171
172
172
- # Get authentication token
173
- self .storage_url , self .token = swiftclient .get_auth (
174
- self .api_auth_url ,
175
- self .api_username ,
176
- self .api_key ,
177
- auth_version = self .auth_version ,
178
- os_options = self .os_options )
179
- self .http_conn = swiftclient .http_connection (self .storage_url )
173
+ # Get Connection wrapper
174
+ self .swift_conn = swiftclient .Connection (
175
+ authurl = self .api_auth_url ,
176
+ user = self .api_username ,
177
+ key = self .api_key ,
178
+ retries = self .max_retries ,
179
+ tenant_name = self .tenant_name ,
180
+ os_options = self .os_options ,
181
+ auth_version = self .auth_version )
180
182
181
183
# Check container
182
184
try :
183
- swiftclient .head_container (self .storage_url ,
184
- self .token ,
185
- self .container_name ,
186
- http_conn = self .http_conn )
185
+ self .swift_conn .head_container (self .container_name )
187
186
except swiftclient .ClientException :
188
187
headers = {}
189
188
if self .auto_create_container :
@@ -192,11 +191,8 @@ def __init__(self, **settings):
192
191
if self .auto_create_container_allow_orgin :
193
192
headers ['X-Container-Meta-Access-Control-Allow-Origin' ] = \
194
193
self .auto_create_container_allow_orgin
195
- swiftclient .put_container (self .storage_url ,
196
- self .token ,
197
- self .container_name ,
198
- http_conn = self .http_conn ,
199
- headers = headers )
194
+ self .swift_conn .put_container (self .container_name ,
195
+ headers = headers )
200
196
else :
201
197
raise ImproperlyConfigured (
202
198
"Container %s does not exist." % self .container_name )
@@ -205,7 +201,7 @@ def __init__(self, **settings):
205
201
# Derive a base URL based on the authentication information from
206
202
# the server, optionally overriding the protocol, host/port and
207
203
# potentially adding a path fragment before the auth information.
208
- self .base_url = self .storage_url + '/'
204
+ self .base_url = self .swift_conn . url + '/'
209
205
if self .override_base_url is not None :
210
206
# override the protocol and host, append any path fragments
211
207
split_derived = urlparse .urlsplit (self .base_url )
@@ -222,32 +218,11 @@ def __init__(self, **settings):
222
218
else :
223
219
self .base_url = self .override_base_url
224
220
225
- def get_token (self ):
226
- if time () - self ._token_creation_time >= self .auth_token_duration :
227
- new_token = swiftclient .get_auth (
228
- self .api_auth_url ,
229
- self .api_username ,
230
- self .api_key ,
231
- auth_version = self .auth_version ,
232
- os_options = self .os_options )[1 ]
233
- self .token = new_token
234
- return self ._token
235
-
236
- def set_token (self , new_token ):
237
- self ._token_creation_time = time ()
238
- self ._token = new_token
239
-
240
- token = property (get_token , set_token )
241
-
242
221
def _open (self , name , mode = 'rb' ):
243
222
original_name = name
244
223
name = self .name_prefix + name
245
224
246
- headers , content = swiftclient .get_object (self .storage_url ,
247
- self .token ,
248
- self .container_name ,
249
- name ,
250
- http_conn = self .http_conn )
225
+ headers , content = self .swift_conn .get_object (self .container_name , name )
251
226
buf = BytesIO (content )
252
227
buf .name = os .path .basename (original_name )
253
228
buf .mode = mode
@@ -273,15 +248,12 @@ def _save(self, name, content, headers=None):
273
248
else :
274
249
content_type = mimetypes .guess_type (name )[0 ]
275
250
content_length = content .size
276
- swiftclient .put_object (self .storage_url ,
277
- self .token ,
278
- self .container_name ,
279
- name ,
280
- content ,
281
- http_conn = self .http_conn ,
282
- content_type = content_type ,
283
- content_length = content_length ,
284
- headers = headers )
251
+ self .swift_conn .put_object (self .container_name ,
252
+ name ,
253
+ content ,
254
+ content_length = content_length ,
255
+ content_type = content_type ,
256
+ headers = headers )
285
257
return original_name
286
258
287
259
def get_headers (self , name ):
@@ -294,12 +266,8 @@ def get_headers(self, name):
294
266
"""
295
267
if name != self .last_headers_name :
296
268
# miss -> update
297
- self .last_headers_value = swiftclient .head_object (
298
- self .storage_url ,
299
- self .token ,
300
- self .container_name ,
301
- name ,
302
- http_conn = self .http_conn )
269
+ self .last_headers_value = self .swift_conn .head_object (
270
+ self .container_name , name )
303
271
self .last_headers_name = name
304
272
return self .last_headers_value
305
273
@@ -314,11 +282,7 @@ def exists(self, name):
314
282
@prepend_name_prefix
315
283
def delete (self , name ):
316
284
try :
317
- swiftclient .delete_object (self .storage_url ,
318
- self .token ,
319
- self .container_name ,
320
- name ,
321
- http_conn = self .http_conn )
285
+ self .swift_conn .delete_object (self .container_name , name )
322
286
except swiftclient .ClientException :
323
287
pass
324
288
@@ -386,9 +350,8 @@ def isdir(self, name):
386
350
387
351
@prepend_name_prefix
388
352
def listdir (self , path ):
389
- container = swiftclient .get_container (self .storage_url , self .token ,
390
- self .container_name , prefix = path ,
391
- full_listing = self .full_listing )
353
+ container = self .swift_conn .get_container (
354
+ self .container_name , prefix = path , full_listing = self .full_listing )
392
355
files = []
393
356
dirs = []
394
357
for obj in container [1 ]:
@@ -404,23 +367,18 @@ def listdir(self, path):
404
367
405
368
@prepend_name_prefix
406
369
def makedirs (self , dirs ):
407
- swiftclient .put_object (self .storage_url ,
408
- token = self .token ,
409
- container = self .container_name ,
410
- name = '%s/.' % (self .name_prefix + dirs ),
411
- contents = '' )
370
+ self .swift_conn .put_object (self .container_name ,
371
+ '%s/.' % (self .name_prefix + dirs ),
372
+ contents = '' )
412
373
413
374
@prepend_name_prefix
414
375
def rmtree (self , abs_path ):
415
- container = swiftclient .get_container (self .storage_url , self .token ,
416
- self .container_name )
376
+ container = self .swift_conn .get_container (self .container_name )
417
377
418
378
for obj in container [1 ]:
419
379
if obj ['name' ].startswith (abs_path ):
420
- swiftclient .delete_object (self .storage_url ,
421
- token = self .token ,
422
- container = self .container_name ,
423
- name = obj ['name' ])
380
+ self .swift_conn .delete_object (self .container_name ,
381
+ obj ['name' ])
424
382
425
383
426
384
class StaticSwiftStorage (SwiftStorage ):
0 commit comments