Always create a new connection to Swift

Fixes #335
This commit is contained in:
Joseph Schorr 2015-08-07 13:41:40 -04:00
parent 7d6c6ba8e8
commit 9798f871ec

View file

@ -26,14 +26,6 @@ class SwiftStorage(BaseStorage):
self._os_options = os_options or {}
self._initialized = False
self._swift_connection = None
def _initialize(self):
if self._initialized:
return
self._initialized = True
self._swift_connection = self._get_connection()
def _get_connection(self):
return Connection(
@ -69,14 +61,13 @@ class SwiftStorage(BaseStorage):
return path
def _get_container(self, path):
self._initialize()
path = self._normalize_path(path)
if path and not path.endswith('/'):
path += '/'
try:
_, container = self._swift_connection.get_container(
_, container = self._get_connection().get_container(
container=self._swift_container,
prefix=path, delimiter='/')
return container
@ -85,10 +76,9 @@ class SwiftStorage(BaseStorage):
raise IOError('Unknown path: %s' % path)
def _get_object(self, path, chunk_size=None):
self._initialize()
path = self._normalize_path(path)
try:
_, obj = self._swift_connection.get_object(self._swift_container, path,
_, obj = self._get_connection().get_object(self._swift_container, path,
resp_chunk_size=chunk_size)
return obj
except Exception:
@ -96,7 +86,6 @@ class SwiftStorage(BaseStorage):
raise IOError('Path %s not found' % path)
def _put_object(self, path, content, chunk=None, content_type=None, content_encoding=None):
self._initialize()
path = self._normalize_path(path)
headers = {}
@ -104,7 +93,7 @@ class SwiftStorage(BaseStorage):
headers['Content-Encoding'] = content_encoding
try:
self._swift_connection.put_object(self._swift_container, path, content,
self._get_connection().put_object(self._swift_container, path, content,
chunk_size=chunk, content_type=content_type,
headers=headers)
except ClientException:
@ -116,10 +105,9 @@ class SwiftStorage(BaseStorage):
raise IOError("Could not put content: %s" % path)
def _head_object(self, path):
self._initialize()
path = self._normalize_path(path)
try:
return self._swift_connection.head_object(self._swift_container, path)
return self._get_connection().head_object(self._swift_container, path)
except Exception:
logger.exception('Could not head object: %s', path)
return None
@ -171,10 +159,9 @@ class SwiftStorage(BaseStorage):
return bool(self._head_object(path))
def remove(self, path):
self._initialize()
path = self._normalize_path(path)
try:
self._swift_connection.delete_object(self._swift_container, path)
self._get_connection().delete_object(self._swift_container, path)
except Exception:
raise IOError('Cannot delete path: %s' % path)