예제 #1
0
    def put(self, storagemodel:object, modeldefinition = None) -> StorageQueueModel:
        """ insert queue message into storage """
        try:
            message = modeldefinition['queueservice'].put_message(storagemodel._queuename, storagemodel.getmessage())
            storagemodel.mergemessage(message)

        except Exception as e:
            storagemodel = None
            msg = 'can not save queue message:  queue {} with message {} because {!s}'.format(storagemodel._queuename, storagemodel.content, e)
            raise AzureStorageWrapException(msg=msg)
           
        finally:
            return storagemodel
예제 #2
0
    def __deletequeue__(self, modeldefinition:dict) -> bool:
        if (not modeldefinition['queueservice'] is None):
            try:
                modeldefinition['queueservice'].delete_queue(modeldefinition['queuename'])
                return True

            except Exception as e:
                msg = 'failed to delete {} with error {}'.format(modeldefinition['queuename'], e)
                raise AzureStorageWrapException(msg=msg)

        else:
            return True
        pass
예제 #3
0
    def upload(self, storagemodel: object, modeldefinition=None):
        """ insert blob message into storage """

        if (storagemodel.content is None) or (
                storagemodel.properties.content_settings.content_type is None):
            # No content to upload
            raise AzureStorageWrapException(
                storagemodel,
                "StorageBlobModel does not contain content nor content settings"
            )

        else:
            blobservice = modeldefinition['blobservice']
            container_name = modeldefinition['container']
            blob_name = storagemodel.name

            try:

                # refresh metadata
                storagemodel.__instance_to_metadata__()
                """ upload bytes """
                blobservice.create_blob_from_bytes(
                    container_name=container_name,
                    blob_name=blob_name,
                    blob=storagemodel.content,
                    metadata=storagemodel.metadata,
                    content_settings=storagemodel.properties.content_settings)

                storagemodel.properties = blobservice.get_blob_properties(
                    container_name=container_name,
                    blob_name=blob_name).properties

            except Exception as e:
                msg = 'can not save blob in container {} because {!s}'.format(
                    storagemodel._containername, e)
                raise AzureStorageWrapException(storagemodel, msg=msg)

        return storagemodel
예제 #4
0
    def __create__(self, modeldefinition: dict) -> bool:
        if (not modeldefinition['blobservice'] is None):
            try:
                modeldefinition['blobservice'].create_container(
                    modeldefinition['container'])
                return True

            except Exception as e:
                msg = 'failed to create {} with error {}'.format(
                    modeldefinition['container'], e)
                raise AzureStorageWrapException(msg=msg)

        else:
            return True
        pass
예제 #5
0
    def totext(self) -> str:
        """ 
        return blob content from StorageBlobModel instance to a string. Parameters are:
        """
        sreturn = ''
        if self.properties.content_settings.content_encoding is None:
            raise AzureStorageWrapException(
                self,
                'can not convert blob {!s} to text because content_encoding is not given'
                .format(self.name))
        else:
            sreturn = self.content.decode(
                self.properties.content_settings.content_encoding, 'ignore')

        return sreturn
예제 #6
0
    def delete(self, storagemodel:object, modeldefinition = None) -> bool:
        """ delete the message in queue """
        deleted = False
        if (storagemodel.id != '') and (storagemodel.pop_receipt != '') and (not storagemodel.id is None) and (not storagemodel.pop_receipt is None):
            try:
                modeldefinition['queueservice'].delete_message(storagemodel._queuename, storagemodel.id, storagemodel.pop_receipt)
                deleted = True

            except Exception as e:
                msg = 'can not delete queue message:  queue {} with message.id {!s} because {!s}'.format(storagemodel._queuename, storagemodel.id, e)
                raise AzureStorageWrapException(msg=msg)

        else:
            log.info('cant update queuemessage {} due to missing id and pop_receipt'.format(storagemodel._queuename))

        return deleted
예제 #7
0
    def delete(self, storagemodel: object, modeldefinition=None) -> bool:
        """ delete the blob from storage """
        deleted = False

        blobservice = modeldefinition['blobservice']
        container_name = modeldefinition['container']
        blob_name = storagemodel.name

        try:
            if blobservice.exists(container_name, blob_name):
                """ delete """
                blob = blobservice.delete_blob(container_name, blob_name)
                deleted = True

        except Exception as e:
            msg = 'can not delete blob {} from storage because {!s}'.format(
                blob_name, e)
            raise AzureStorageWrapException(storagemodel, msg=msg)

        return deleted
예제 #8
0
    def fromtext(self, text, encoding='utf-8', mimetype='text/plain'):
        """ 
        set blob content from given text in StorageBlobModel instance. Parameters are:
        - text (required): path to a local file 
        - encoding (optional): text encoding (default is utf-8)
        - mimetype (optional): set a mimetype. azurestoragewrap will guess it if not given 
        """
        if isinstance(text, str):
            text = text.encode(encoding, 'ignore')

            # Load text into self.__content__
            self.content = bytes(text)

            self.properties.content_settings = ContentSettings(
                content_type=mimetype, content_encoding=encoding)

        else:
            raise AzureStorageWrapException(
                self,
                'Can not load blob content, because given text is not from type string'
            )
예제 #9
0
    def peek(self, storagemodel:object, modeldefinition = None) -> StorageQueueModel:
        """ lookup the next message in queue """

        try:
            messages = modeldefinition['queueservice'].peek_messages(storagemodel._queuename, num_messages=1)

            """ parse retrieved message """
            for message in messages:
                storagemodel.mergemessage(message)

            """ no message retrieved ?"""
            if storagemodel.id is None:
                storagemodel = None

        except Exception as e:
            storagemodel = None
            msg = 'can not peek queue message:  queue {} with message {} because {!s}'.format(storagemodel._queuename, storagemodel.content, e)
            raise AzureStorageWrapException(msg=msg)

        finally:
            return storagemodel
예제 #10
0
    def exists(self, storagemodel: object, modeldefinition=None) -> bool:
        """ delete the blob from storage """
        exists = False

        blobservice = modeldefinition['blobservice']
        container_name = modeldefinition['container']
        blob_name = storagemodel.name

        try:
            blobs = self.list(storagemodel,
                              modeldefinition,
                              where=storagemodel.name)
            if len(blobs) == 1:
                storagemodel.__mergeblob__(blobs[0])
                exists = True

        except Exception as e:
            msg = 'can not retireve blob {} from storage because {!s}'.format(
                blob_name, e)
            raise AzureStorageWrapException(storagemodel, msg=msg)

        return exists
예제 #11
0
    def get(self, storagemodel:object, modeldefinition = None, hide = 0) -> StorageQueueModel:
        """ get the next message in queue """
        try:
            if hide > 0:
                messages = modeldefinition['queueservice'].get_messages(storagemodel._queuename, num_messages=1, visibility_timeout = hide)
            else:
                messages = modeldefinition['queueservice'].get_messages(storagemodel._queuename, num_messages=1)
                    
            """ parse retrieved message """
            for message in messages:
                storagemodel.mergemessage(message)

            """ no message retrieved ?"""
            if storagemodel.id is None:
                storagemodel = None

        except Exception as e:
            storagemodel = None
            msg = 'can not peek queue message:  queue {} with message {} because {!s}'.format(storagemodel._queuename, storagemodel.content, e)
            raise AzureStorageWrapException(msg=msg)

        finally:
            return storagemodel
예제 #12
0
    def list(self,
             storagemodel: object,
             modeldefinition=None,
             where=None) -> list:
        """ list blob messages in container """
        try:
            blobnames = []
            if where is None:
                generator = modeldefinition['blobservice'].list_blobs(
                    modeldefinition['container'])
            else:
                generator = modeldefinition['blobservice'].list_blobs(
                    modeldefinition['container'], prefix=where)

            for blob in generator:
                blobnames.append(blob)

        except Exception as e:
            msg = 'can not list blobs in container {} because {!s}'.format(
                storagemodel._containername, e)
            raise AzureStorageWrapException(storagemodel, msg=msg)

        finally:
            return blobnames