@@ -140,9 +140,12 @@ class SwiftStorage(Storage):
140
140
auth_token_duration = setting ('SWIFT_AUTH_TOKEN_DURATION' , 60 * 60 * 23 )
141
141
os_extra_options = setting ('SWIFT_EXTRA_OPTIONS' , {})
142
142
auto_overwrite = setting ('SWIFT_AUTO_OVERWRITE' , False )
143
+ lazy_connect = setting ('SWIFT_LAZY_CONNECT' , False )
143
144
content_type_from_fd = setting ('SWIFT_CONTENT_TYPE_FROM_FD' , False )
144
145
_token_creation_time = 0
145
146
_token = ''
147
+ _swift_conn = None
148
+ _base_url = None
146
149
name_prefix = setting ('SWIFT_NAME_PREFIX' , '' )
147
150
full_listing = setting ('SWIFT_FULL_LISTING' , True )
148
151
max_retries = setting ('SWIFT_MAX_RETRIES' , 5 )
@@ -170,17 +173,28 @@ def __init__(self, **settings):
170
173
}
171
174
self .os_options .update (self .os_extra_options )
172
175
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 )
182
-
183
- # Check container
176
+ if not self .lazy_connect :
177
+ self .swift_conn
178
+
179
+ @property
180
+ def swift_conn (self ):
181
+ """Get swift connection wrapper"""
182
+ if not self ._swift_conn :
183
+ self ._swift_conn = swiftclient .Connection (
184
+ authurl = self .api_auth_url ,
185
+ user = self .api_username ,
186
+ key = self .api_key ,
187
+ retries = self .max_retries ,
188
+ tenant_name = self .tenant_name ,
189
+ os_options = self .os_options ,
190
+ auth_version = self .auth_version )
191
+ self ._check_container ()
192
+ return self ._swift_conn
193
+
194
+ def _check_container (self ):
195
+ """
196
+ Check that container exists; raises exception if not.
197
+ """
184
198
try :
185
199
self .swift_conn .head_container (self .container_name )
186
200
except swiftclient .ClientException :
@@ -197,26 +211,30 @@ def __init__(self, **settings):
197
211
raise ImproperlyConfigured (
198
212
"Container %s does not exist." % self .container_name )
199
213
200
- if self .auto_base_url :
201
- # Derive a base URL based on the authentication information from
202
- # the server, optionally overriding the protocol, host/port and
203
- # potentially adding a path fragment before the auth information.
204
- self .base_url = self .swift_conn .url + '/'
205
- if self .override_base_url is not None :
206
- # override the protocol and host, append any path fragments
207
- split_derived = urlparse .urlsplit (self .base_url )
208
- split_override = urlparse .urlsplit (self .override_base_url )
209
- split_result = ['' ] * 5
210
- split_result [0 :2 ] = split_override [0 :2 ]
211
- split_result [2 ] = (split_override [2 ] + split_derived [2 ]
212
- ).replace ('//' , '/' )
213
- self .base_url = urlparse .urlunsplit (split_result )
214
-
215
- self .base_url = urlparse .urljoin (self .base_url ,
216
- self .container_name )
217
- self .base_url += '/'
218
- else :
219
- self .base_url = self .override_base_url
214
+ @property
215
+ def base_url (self ):
216
+ if self ._base_url is None :
217
+ if self .auto_base_url :
218
+ # Derive a base URL based on the authentication information from
219
+ # the server, optionally overriding the protocol, host/port and
220
+ # potentially adding a path fragment before the auth information.
221
+ self ._base_url = self .swift_conn .url + '/'
222
+ if self .override_base_url is not None :
223
+ # override the protocol and host, append any path fragments
224
+ split_derived = urlparse .urlsplit (self ._base_url )
225
+ split_override = urlparse .urlsplit (self .override_base_url )
226
+ split_result = ['' ] * 5
227
+ split_result [0 :2 ] = split_override [0 :2 ]
228
+ split_result [2 ] = (split_override [2 ] + split_derived [2 ]
229
+ ).replace ('//' , '/' )
230
+ self ._base_url = urlparse .urlunsplit (split_result )
231
+
232
+ self ._base_url = urlparse .urljoin (self ._base_url ,
233
+ self .container_name )
234
+ self ._base_url += '/'
235
+ else :
236
+ self ._base_url = self .override_base_url
237
+ return self ._base_url
220
238
221
239
def _open (self , name , mode = 'rb' ):
222
240
original_name = name
0 commit comments