def DownloadBlock(self, vault, block): """Gets the data associated with the block id provided :param vault: vault to download the block from :param block: the block to be downloaded :stores: The block Data in the the data property of the block :returns: True on success """ url = api_v1.get_block_path(vault.vault_id, block.block_id) self.ReInit(self.sslenabled, url) self.__update_headers() self.__log_request_data(fn='Download Block') res = requests.get(self.Uri, headers=self.Headers) self.__log_response_data(res, jsondata=False, fn='Download Block') if res.status_code == 200: block.data = res.content return True elif res.status_code == 410: raise errors.MissingBlockError( 'The Storage Block associated with Metadata Block {0:} ' 'is missing from storage. Re-uploading the associated ' 'data will restore access to any files using the block.') else: raise RuntimeError( 'Failed to get Block Content for Block Id . ' 'Error ({0:}): {1:}'.format(res.status_code, res.text))
def UploadBlock(self, vault, block): """Upload a block to the vault specified. :param vault: vault to upload the block into :param block: block to be uploaded must be deuceclient.api.Block type :returns: True on success """ url = api_v1.get_block_path(vault.vault_id, block.block_id) self.ReInit(self.sslenabled, url) self.__update_headers() headers = {} headers.update(self.Headers) headers['content-type'] = 'application/octet-stream' headers['content-length'] = len(block) self.__log_request_data(headers=headers, fn='Upload Block') res = requests.put(self.Uri, headers=headers, data=block.data) self.__log_response_data(res, jsondata=False, fn='Upload Block') if res.status_code == 201: return True else: raise RuntimeError( 'Failed to upload Block. ' 'Error ({0:}): {1:}'.format(res.status_code, res.text))
def HeadBlock(self, vault, block): """Head a block and get its information :param vault: vault to upload the block into :param block: block to be uploaded must be deuceclient.api.Block type :returns: True on success """ url = api_v1.get_block_path(vault.vault_id, block.block_id) self.ReInit(self.sslenabled, url) self.__update_headers() headers = {} headers.update(self.Headers) headers['content-type'] = 'application/octet-stream' self.__log_request_data(headers=headers, fn='Head Block') res = requests.head(self.Uri, headers=headers) self.__log_response_data(res, jsondata=False, fn='Head Block') if res.status_code == 204: block.ref_modified = int(res.headers['X-Ref-Modified'])\ if res.headers['X-Ref-Modified'] else 0 block.ref_count = int(res.headers['X-Block-Reference-Count'])\ if res.headers['X-Block-Reference-Count'] else 0 block.set_block_size(int(res.headers['X-Block-Size'] if res.headers['X-Block-Size'] else 0)) block.storage_id = None if res.headers['X-Storage-ID'] == \ 'None' else res.headers['X-Storage-ID'] # Any block we get back here cannot be orphaned block.block_orphaned = False return block elif res.status_code == 410: raise errors.MissingBlockError( 'The Storage Block associated with Metadata Block {0:} ' 'is missing from storage. Re-uploading the associated ' 'data will restore access to any files using the block.') else: raise RuntimeError( 'Failed to Head Block {0:} in Vault {1}:. ' 'Error ({2:}): {3:}'.format(block.block_id, vault.vault_id, res.status_code, res.text))
def DeleteBlock(self, vault, block): """Delete the block from the vault. :param vault: vault to delete the block from :param block: the block to be deleted :returns: True on success Note: The block is not removed from the local Vault object """ url = api_v1.get_block_path(vault.vault_id, block.block_id) self.ReInit(self.sslenabled, url) self.__update_headers() self.__log_request_data(fn='Delete Block') res = requests.delete(self.Uri, headers=self.Headers) self.__log_response_data(res, jsondata=False, fn='Delete Block') if res.status_code == 204: return True else: raise RuntimeError( 'Failed to delete Vault. ' 'Error ({0:}): {1:}'.format(res.status_code, res.text))