Changeset View
Changeset View
Standalone View
Standalone View
swh/objstorage/backends/libcloud.py
Show First 20 Lines • Show All 165 Lines • ▼ Show 20 Lines | def check(self, obj_id): | ||||
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): | def delete(self, obj_id): | ||||
super().delete(obj_id) # Check delete permission | super().delete(obj_id) # Check delete permission | ||||
obj = self._get_object(obj_id) | obj = self._get_object(obj_id) | ||||
return self.driver.delete_object(obj) | return self.driver.delete_object(obj) | ||||
def _object_path(self, obj_id): | |||||
"""Get the full path to an object""" | |||||
hex_obj_id = hashutil.hash_to_hex(obj_id) | |||||
return hex_obj_id | |||||
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) | object_path = self._object_path(obj_id) | ||||
try: | try: | ||||
return self.driver.get_object(self.container_name, hex_obj_id) | return self.driver.get_object(self.container_name, object_path) | ||||
except ObjectDoesNotExistError: | except ObjectDoesNotExistError: | ||||
raise ObjNotFoundError(obj_id) | raise ObjNotFoundError(obj_id) | ||||
def _compressor(self, data): | def _compressor(self, data): | ||||
comp = compressors[self.compression]() | comp = compressors[self.compression]() | ||||
for chunk in data: | for chunk in data: | ||||
cchunk = comp.compress(chunk) | cchunk = comp.compress(chunk) | ||||
if cchunk: | if cchunk: | ||||
yield cchunk | yield cchunk | ||||
trail = comp.flush() | trail = comp.flush() | ||||
if trail: | if trail: | ||||
yield trail | yield trail | ||||
def _put_object(self, content, obj_id): | def _put_object(self, content, obj_id): | ||||
"""Create an object in the cloud storage. | """Create an object in the cloud storage. | ||||
Created object will contain the content and be referenced by | Created object will contain the content and be referenced by | ||||
the given id. | the given id. | ||||
""" | """ | ||||
hex_obj_id = hashutil.hash_to_hex(obj_id) | object_path = self._object_path(obj_id) | ||||
if not isinstance(content, collections.Iterator): | if not isinstance(content, collections.Iterator): | ||||
content = (content,) | content = (content,) | ||||
self.driver.upload_object_via_stream( | self.driver.upload_object_via_stream( | ||||
self._compressor(content), | self._compressor(content), | ||||
self.container, hex_obj_id) | self.container, object_path) | ||||
class AwsCloudObjStorage(CloudObjStorage): | class AwsCloudObjStorage(CloudObjStorage): | ||||
""" Amazon's S3 Cloud-based object storage | """ Amazon's S3 Cloud-based object storage | ||||
""" | """ | ||||
def _get_provider(self): | def _get_provider(self): | ||||
return Provider.S3 | return Provider.S3 | ||||
class OpenStackCloudObjStorage(CloudObjStorage): | class OpenStackCloudObjStorage(CloudObjStorage): | ||||
""" OpenStack Swift Cloud based object storage | """ OpenStack Swift Cloud based object storage | ||||
""" | """ | ||||
def _get_provider(self): | def _get_provider(self): | ||||
return Provider.OPENSTACK_SWIFT | return Provider.OPENSTACK_SWIFT |