Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/cloud/objstorage_cloud.py
Show All 16 Lines | class CloudObjStorage(ObjStorage, metaclass=abc.ABCMeta): | ||||
"""Abstract ObjStorage that connect to a cloud using Libcloud | """Abstract ObjStorage that connect to a cloud using Libcloud | ||||
Implementations of this class must redefine the _get_provider | Implementations of this class must redefine the _get_provider | ||||
method to make it return a driver provider (i.e. object that | method to make it return a driver provider (i.e. object that | ||||
supports `get_driver` method) which return a LibCloud driver (see | supports `get_driver` method) which return a LibCloud driver (see | ||||
https://libcloud.readthedocs.io/en/latest/storage/api.html). | https://libcloud.readthedocs.io/en/latest/storage/api.html). | ||||
""" | """ | ||||
def __init__(self, api_key, api_secret_key, container_name): | def __init__(self, api_key, api_secret_key, container_name, **kwargs): | ||||
super().__init__(**kwargs) | |||||
self.driver = self._get_driver(api_key, api_secret_key) | self.driver = self._get_driver(api_key, api_secret_key) | ||||
self.container_name = container_name | self.container_name = container_name | ||||
self.container = self.driver.get_container( | self.container = self.driver.get_container( | ||||
container_name=container_name) | container_name=container_name) | ||||
def _get_driver(self, api_key, api_secret_key): | def _get_driver(self, api_key, api_secret_key): | ||||
"""Initialize a driver to communicate with the cloud | """Initialize a driver to communicate with the cloud | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | def check(self, obj_id): | ||||
# Check that the file exists, as _get_object raises ObjNotFoundError | # Check that the file exists, as _get_object raises ObjNotFoundError | ||||
self._get_object(obj_id) | self._get_object(obj_id) | ||||
# Check the content integrity | # Check the content integrity | ||||
obj_content = self.get(obj_id) | obj_content = self.get(obj_id) | ||||
content_obj_id = compute_hash(obj_content) | content_obj_id = compute_hash(obj_content) | ||||
if content_obj_id != obj_id: | if content_obj_id != obj_id: | ||||
raise Error(obj_id) | raise Error(obj_id) | ||||
def delete(self, obj_id): | |||||
super().delete(obj_id) # Check delete permission | |||||
obj = self._get_object(obj_id) | |||||
return self.driver.delete_object(obj) | |||||
def _get_object(self, obj_id): | def _get_object(self, obj_id): | ||||
"""Get a Libcloud wrapper for an object pointer. | """Get a Libcloud wrapper for an object pointer. | ||||
This wrapper does not retrieve the content of the object | This wrapper does not retrieve the content of the object | ||||
directly. | directly. | ||||
""" | """ | ||||
hex_obj_id = hashutil.hash_to_hex(obj_id) | hex_obj_id = hashutil.hash_to_hex(obj_id) | ||||
Show All 31 Lines |