-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstorage.py
More file actions
68 lines (50 loc) · 2.23 KB
/
storage.py
File metadata and controls
68 lines (50 loc) · 2.23 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
from keystoneauth1 import session
from keystoneauth1.identity import v3
import time
import abc
import swiftclient.client
class Storage(object, metaclass=abc.ABCMeta):
@abc.abstractmethod
def save_blob(self, blob_bytes, blob_id):
raise NotImplementedError('users must define method to use base class')
@abc.abstractmethod
def close(self):
raise NotImplementedError('users must define method to use base class')
# The auth token expires after 24 hours by default, but refresh more frequently:
OS_SWIFT_CONN_MAX_LIFETIME_SECONDS = 60 * 60
class LocalStorage(Storage):
def __init__(self, config):
self.config = config
def save_blob(self, blob_bytes, blob_id):
with open("{0}/{1}".format(config["folder"],blob_id),"w") as fh:
fh.write(blob_bytes)
class OsSwiftStorage(Storage):
def __init__(self, config):
self.config = config
self.conn = None
self.conn_timestamp_connected = None
# Try to connect now, to fail fast:
self.__reauthenticate_if_needed()
def save_blob(self, blob_bytes, blob_id):
self.__reauthenticate_if_needed()
if isinstance(blob_bytes, bytearray):
# Workaround a bug in the OpenStack client - 'bytearray' is not properly handled as a content.
# (see https://bugs.launchpad.net/python-swiftclient/+bug/1741991)
blob_bytes = bytes(blob_bytes)
self.conn.put_object('Haste_Stream_Storage', blob_id, blob_bytes)
def close(self):
if self.conn is not None:
self.conn.close()
def __reauthenticate_if_needed(self):
if self.conn is None \
or self.conn_timestamp_connected is None \
or self.conn_timestamp_connected + OS_SWIFT_CONN_MAX_LIFETIME_SECONDS < time.time():
print('HasteStorageClient: (re)connecting os_swift...')
if self.conn is not None:
self.conn.close()
self.conn = None
self.conn_timestamp_connected = None
auth = v3.Password(**self.config)
keystone_session = session.Session(auth=auth)
self.conn = swiftclient.client.Connection(session=keystone_session)
self.conn_timestamp_connected = time.time()