示例#1
0
def _update_entity(entity,
                   if_match,
                   encryption_required=False,
                   key_encryption_key=None,
                   encryption_resolver=None):
    '''
    Constructs an update entity request.
    :param entity:
        The entity to insert. Could be a dict or an entity object.
    :param object key_encryption_key:
        The user-provided key-encryption-key. Must implement the following methods:
        wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
        get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
        get_kid()--returns a string key id for this key-encryption-key.
    :param function(partition_key, row_key, property_name) encryption_resolver:
        A function that takes in an entities partition key, row key, and property name and returns
        a boolean that indicates whether that property should be encrypted.
    '''
    _validate_not_none('if_match', if_match)
    _validate_entity(entity, key_encryption_key is not None)
    _validate_encryption_required(encryption_required, key_encryption_key)

    request = HTTPRequest()
    request.method = 'PUT'
    request.headers = {
        _DEFAULT_CONTENT_TYPE_HEADER[0]: _DEFAULT_CONTENT_TYPE_HEADER[1],
        _DEFAULT_ACCEPT_HEADER[0]: _DEFAULT_ACCEPT_HEADER[1],
        'If-Match': _to_str(if_match),
    }
    if key_encryption_key:
        entity = _encrypt_entity(entity, key_encryption_key,
                                 encryption_resolver)
    request.body = _get_request_body(_convert_entity_to_json(entity))

    return request
示例#2
0
def _update_entity(entity, if_match, encryption_required=False,
                   key_encryption_key=None, encryption_resolver=None):
    '''
    Constructs an update entity request.
    :param entity:
        The entity to insert. Could be a dict or an entity object.
    :param object key_encryption_key:
        The user-provided key-encryption-key. Must implement the following methods:
        wrap_key(key)--wraps the specified key using an algorithm of the user's choice.
        get_key_wrap_algorithm()--returns the algorithm used to wrap the specified symmetric key.
        get_kid()--returns a string key id for this key-encryption-key.
    :param function(partition_key, row_key, property_name) encryption_resolver:
        A function that takes in an entities partition key, row key, and property name and returns
        a boolean that indicates whether that property should be encrypted.
    '''
    _validate_not_none('if_match', if_match)
    _validate_entity(entity, key_encryption_key is not None)
    _validate_encryption_required(encryption_required, key_encryption_key)

    request = HTTPRequest()
    request.method = 'PUT'
    request.headers = {
        _DEFAULT_CONTENT_TYPE_HEADER[0]: _DEFAULT_CONTENT_TYPE_HEADER[1],
        _DEFAULT_ACCEPT_HEADER[0]: _DEFAULT_ACCEPT_HEADER[1],
        'If-Match': _to_str(if_match),
    }
    if key_encryption_key:
        entity = _encrypt_entity(entity, key_encryption_key, encryption_resolver)
    request.body = _get_request_body(_convert_entity_to_json(entity))

    return request
示例#3
0
def _insert_or_replace_entity(entity, require_encryption=False,
                              key_encryption_key=None, encryption_resolver=None):
    '''
    Constructs an insert or replace entity request.
    '''
    _validate_entity(entity, key_encryption_key is not None)
    _validate_encryption_required(require_encryption, key_encryption_key)

    request = HTTPRequest()
    request.method = 'PUT'
    request.headers = {
        _DEFAULT_CONTENT_TYPE_HEADER[0]: _DEFAULT_CONTENT_TYPE_HEADER[1],
        _DEFAULT_ACCEPT_HEADER[0]: _DEFAULT_ACCEPT_HEADER[1],
    }

    if key_encryption_key:
        entity = _encrypt_entity(entity, key_encryption_key, encryption_resolver)
    request.body = _get_request_body(_convert_entity_to_json(entity))

    return request
示例#4
0
def _insert_or_replace_entity(entity,
                              require_encryption=False,
                              key_encryption_key=None,
                              encryption_resolver=None):
    '''
    Constructs an insert or replace entity request.
    '''
    _validate_entity(entity, key_encryption_key is not None)
    _validate_encryption_required(require_encryption, key_encryption_key)

    request = HTTPRequest()
    request.method = 'PUT'
    request.headers = {
        _DEFAULT_CONTENT_TYPE_HEADER[0]: _DEFAULT_CONTENT_TYPE_HEADER[1],
        _DEFAULT_ACCEPT_HEADER[0]: _DEFAULT_ACCEPT_HEADER[1],
    }

    if key_encryption_key:
        entity = _encrypt_entity(entity, key_encryption_key,
                                 encryption_resolver)
    request.body = _get_request_body(_convert_entity_to_json(entity))

    return request
    def _put_blob(self, container_name, blob_name, blob, content_settings=None,
                  metadata=None, validate_content=False, lease_id=None, if_modified_since=None,
                  if_unmodified_since=None, if_match=None, if_none_match=None,
                  timeout=None):
        '''
        Creates a blob or updates an existing blob.

        See create_blob_from_* for high level
        functions that handle the creation and upload of large blobs with
        automatic chunking and progress notifications.

        :param str container_name:
            Name of existing container.
        :param str blob_name:
            Name of blob to create or update.
        :param bytes blob:
            Content of blob as bytes (size < 64MB). For larger size, you
            must call put_block and put_block_list to set content of blob.
        :param ~azure.storage.blob.models.ContentSettings content_settings:
            ContentSettings object used to set properties on the blob.
        :param metadata:
            Name-value pairs associated with the blob as metadata.
        :param bool validate_content:
            If true, calculates an MD5 hash of the blob content. The storage
            service checks the hash of the content that has arrived
            with the hash that was sent. This is primarily valuable for detecting
            bitflips on the wire if using http instead of https as https (the default)
            will already validate. Note that this MD5 hash is not stored with the
            blob.
        :param str lease_id:
            Required if the blob has an active lease.
        :param datetime if_modified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only
            if the resource has been modified since the specified time.
        :param datetime if_unmodified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only if
            the resource has not been modified since the specified date/time.
        :param str if_match:
            An ETag value, or the wildcard character (*). Specify this header to perform
            the operation only if the resource's ETag matches the value specified.
        :param str if_none_match:
            An ETag value, or the wildcard character (*). Specify this header
            to perform the operation only if the resource's ETag does not match
            the value specified. Specify the wildcard character (*) to perform
            the operation only if the resource does not exist, and fail the
            operation if it does exist.
        :param int timeout:
            The timeout parameter is expressed in seconds.
        :return: ETag and last modified properties for the new Block Blob
        :rtype: :class:`~azure.storage.blob.models.ResourceProperties`
        '''
        _validate_not_none('container_name', container_name)
        _validate_not_none('blob_name', blob_name)
        _validate_encryption_required(self.require_encryption, self.key_encryption_key)

        request = HTTPRequest()
        request.method = 'PUT'
        request.host_locations = self._get_host_locations()
        request.path = _get_path(container_name, blob_name)
        request.query = {'timeout': _int_to_str(timeout)}
        request.headers = {
            'x-ms-blob-type': _to_str(self.blob_type),
            'x-ms-lease-id': _to_str(lease_id),
            'If-Modified-Since': _datetime_to_utc_string(if_modified_since),
            'If-Unmodified-Since': _datetime_to_utc_string(if_unmodified_since),
            'If-Match': _to_str(if_match),
            'If-None-Match': _to_str(if_none_match)
        }
        _add_metadata_headers(metadata, request)
        if content_settings is not None:
            request.headers.update(content_settings._to_headers())
        blob = _get_data_bytes_only('blob', blob)
        if self.key_encryption_key:
            encryption_data, blob = _encrypt_blob(blob, self.key_encryption_key)
            request.headers['x-ms-meta-encryptiondata'] = encryption_data
        request.body = blob

        if validate_content:
            computed_md5 = _get_content_md5(request.body)
            request.headers['Content-MD5'] = _to_str(computed_md5)

        return self._perform_request(request, _parse_base_properties)
    def create_blob_from_stream(
            self, container_name, blob_name, stream, count=None,
            content_settings=None, metadata=None, validate_content=False,
            progress_callback=None, max_connections=2, lease_id=None,
            if_modified_since=None, if_unmodified_since=None, if_match=None,
            if_none_match=None, timeout=None, use_byte_buffer=False):
        '''
        Creates a new blob from a file/stream, or updates the content of
        an existing blob, with automatic chunking and progress
        notifications.

        :param str container_name:
            Name of existing container.
        :param str blob_name:
            Name of blob to create or update.
        :param io.IOBase stream:
            Opened file/stream to upload as the blob content.
        :param int count:
            Number of bytes to read from the stream. This is optional, but
            should be supplied for optimal performance.
        :param ~azure.storage.blob.models.ContentSettings content_settings:
            ContentSettings object used to set blob properties.
        :param metadata:
            Name-value pairs associated with the blob as metadata.
        :type metadata: dict(str, str)
        :param bool validate_content:
            If true, calculates an MD5 hash for each chunk of the blob. The storage
            service checks the hash of the content that has arrived with the hash
            that was sent. This is primarily valuable for detecting bitflips on
            the wire if using http instead of https as https (the default) will
            already validate. Note that this MD5 hash is not stored with the
            blob.
        :param progress_callback:
            Callback for progress with signature function(current, total) where
            current is the number of bytes transfered so far, and total is the
            size of the blob, or None if the total size is unknown.
        :type progress_callback: func(current, total)
        :param int max_connections:
            Maximum number of parallel connections to use when the blob size exceeds
            64MB. Note that parallel upload requires the stream to be seekable.
        :param str lease_id:
            Required if the blob has an active lease.
        :param datetime if_modified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only
            if the resource has been modified since the specified time.
        :param datetime if_unmodified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only if
            the resource has not been modified since the specified date/time.
        :param str if_match:
            An ETag value, or the wildcard character (*). Specify this header to perform
            the operation only if the resource's ETag matches the value specified.
        :param str if_none_match:
            An ETag value, or the wildcard character (*). Specify this header
            to perform the operation only if the resource's ETag does not match
            the value specified. Specify the wildcard character (*) to perform
            the operation only if the resource does not exist, and fail the
            operation if it does exist.
        :param int timeout:
            The timeout parameter is expressed in seconds. This method may make
            multiple calls to the Azure service and the timeout will apply to
            each call individually.
        :param bool use_byte_buffer:
            If True, this will force usage of the original full block buffering upload path.
            By default, this value is False and will employ a memory-efficient,
            streaming upload algorithm under the following conditions:
            The provided stream is seekable, 'require_encryption' is False, and
            MAX_BLOCK_SIZE >= MIN_LARGE_BLOCK_UPLOAD_THRESHOLD.
            One should consider the drawbacks of using this approach. In order to achieve
            memory-efficiency, a IOBase stream or file-like object is segmented into logical blocks
            using a SubStream wrapper. In order to read the correct data, each SubStream must acquire
            a lock so that it can safely seek to the right position on the shared, underlying stream.
            If max_connections > 1, the concurrency will result in a considerable amount of seeking on
            the underlying stream. For the most common inputs such as a file-like stream object, seeking
            is an inexpensive operation and this is not much of a concern. However, for other variants of streams
            this may not be the case. The trade-off for memory-efficiency must be weighed against the cost of seeking
            with your input stream.
            The SubStream class will attempt to buffer up to 4 MB internally to reduce the amount of
            seek and read calls to the underlying stream. This is particularly beneficial when uploading larger blocks.
        :return: ETag and last modified properties for the Block Blob
        :rtype: :class:`~azure.storage.blob.models.ResourceProperties`
        '''
        _validate_not_none('container_name', container_name)
        _validate_not_none('blob_name', blob_name)
        _validate_not_none('stream', stream)
        _validate_encryption_required(self.require_encryption, self.key_encryption_key)

        # Adjust count to include padding if we are expected to encrypt.
        adjusted_count = count
        if (self.key_encryption_key is not None) and (adjusted_count is not None):
            adjusted_count += (16 - (count % 16))

        if adjusted_count is not None and (adjusted_count < self.MAX_SINGLE_PUT_SIZE):
            if progress_callback:
                progress_callback(0, count)

            data = stream.read(count)
            resp = self._put_blob(
                container_name=container_name,
                blob_name=blob_name,
                blob=data,
                content_settings=content_settings,
                metadata=metadata,
                validate_content=validate_content,
                lease_id=lease_id,
                if_modified_since=if_modified_since,
                if_unmodified_since=if_unmodified_since,
                if_match=if_match,
                if_none_match=if_none_match,
                timeout=timeout)

            if progress_callback:
                progress_callback(count, count)

            return resp
        else:
            cek, iv, encryption_data = None, None, None

            use_original_upload_path = use_byte_buffer or self.require_encryption or \
                                       self.MAX_BLOCK_SIZE < self.MIN_LARGE_BLOCK_UPLOAD_THRESHOLD or \
                                       hasattr(stream, 'seekable') and not stream.seekable() or \
                                       not hasattr(stream, 'seek') or not hasattr(stream, 'tell')

            if use_original_upload_path:
                if self.key_encryption_key:
                    cek, iv, encryption_data = _generate_blob_encryption_data(self.key_encryption_key)

                block_ids = _upload_blob_chunks(
                    blob_service=self,
                    container_name=container_name,
                    blob_name=blob_name,
                    blob_size=count,
                    block_size=self.MAX_BLOCK_SIZE,
                    stream=stream,
                    max_connections=max_connections,
                    progress_callback=progress_callback,
                    validate_content=validate_content,
                    lease_id=lease_id,
                    uploader_class=_BlockBlobChunkUploader,
                    timeout=timeout,
                    content_encryption_key=cek,
                    initialization_vector=iv
                )
            else:
                block_ids = _upload_blob_substream_blocks(
                    blob_service=self,
                    container_name=container_name,
                    blob_name=blob_name,
                    blob_size=count,
                    block_size=self.MAX_BLOCK_SIZE,
                    stream=stream,
                    max_connections=max_connections,
                    progress_callback=progress_callback,
                    validate_content=validate_content,
                    lease_id=lease_id,
                    uploader_class=_BlockBlobChunkUploader,
                    timeout=timeout,
                )

            return self._put_block_list(
                container_name=container_name,
                blob_name=blob_name,
                block_list=block_ids,
                content_settings=content_settings,
                metadata=metadata,
                validate_content=validate_content,
                lease_id=lease_id,
                if_modified_since=if_modified_since,
                if_unmodified_since=if_unmodified_since,
                if_match=if_match,
                if_none_match=if_none_match,
                timeout=timeout,
                encryption_data=encryption_data
            )
示例#7
0
    def _put_blob(self, container_name, blob_name, blob, content_settings=None,
                  metadata=None, validate_content=False, lease_id=None, if_modified_since=None,
                  if_unmodified_since=None, if_match=None, if_none_match=None,
                  timeout=None):
        '''
        Creates a blob or updates an existing blob.

        See create_blob_from_* for high level
        functions that handle the creation and upload of large blobs with
        automatic chunking and progress notifications.

        :param str container_name:
            Name of existing container.
        :param str blob_name:
            Name of blob to create or update.
        :param bytes blob:
            Content of blob as bytes (size < 64MB). For larger size, you
            must call put_block and put_block_list to set content of blob.
        :param ~azure.storage.blob.models.ContentSettings content_settings:
            ContentSettings object used to set properties on the blob.
        :param metadata:
            Name-value pairs associated with the blob as metadata.
        :param bool validate_content:
            If true, calculates an MD5 hash of the blob content. The storage
            service checks the hash of the content that has arrived
            with the hash that was sent. This is primarily valuable for detecting
            bitflips on the wire if using http instead of https as https (the default)
            will already validate. Note that this MD5 hash is not stored with the
            blob.
        :param str lease_id:
            Required if the blob has an active lease.
        :param datetime if_modified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only
            if the resource has been modified since the specified time.
        :param datetime if_unmodified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only if
            the resource has not been modified since the specified date/time.
        :param str if_match:
            An ETag value, or the wildcard character (*). Specify this header to perform
            the operation only if the resource's ETag matches the value specified.
        :param str if_none_match:
            An ETag value, or the wildcard character (*). Specify this header
            to perform the operation only if the resource's ETag does not match
            the value specified. Specify the wildcard character (*) to perform
            the operation only if the resource does not exist, and fail the
            operation if it does exist.
        :param int timeout:
            The timeout parameter is expressed in seconds.
        :return: ETag and last modified properties for the new Block Blob
        :rtype: :class:`~azure.storage.blob.models.ResourceProperties`
        '''
        _validate_not_none('container_name', container_name)
        _validate_not_none('blob_name', blob_name)
        _validate_encryption_required(self.require_encryption, self.key_encryption_key)

        request = HTTPRequest()
        request.method = 'PUT'
        request.host_locations = self._get_host_locations()
        request.path = _get_path(container_name, blob_name)
        request.query = {'timeout': _int_to_str(timeout)}
        request.headers = {
            'x-ms-blob-type': _to_str(self.blob_type),
            'x-ms-lease-id': _to_str(lease_id),
            'If-Modified-Since': _datetime_to_utc_string(if_modified_since),
            'If-Unmodified-Since': _datetime_to_utc_string(if_unmodified_since),
            'If-Match': _to_str(if_match),
            'If-None-Match': _to_str(if_none_match)
        }
        _add_metadata_headers(metadata, request)
        if content_settings is not None:
            request.headers.update(content_settings._to_headers())
        blob = _get_data_bytes_only('blob', blob)
        if self.key_encryption_key:
            encryption_data, blob = _encrypt_blob(blob, self.key_encryption_key)
            request.headers['x-ms-meta-encryptiondata'] = encryption_data
        request.body = blob

        if validate_content:
            computed_md5 = _get_content_md5(request.body)
            request.headers['Content-MD5'] = _to_str(computed_md5)

        return self._perform_request(request, _parse_base_properties)
示例#8
0
    def create_blob_from_stream(
            self, container_name, blob_name, stream, count=None,
            content_settings=None, metadata=None, validate_content=False,
            progress_callback=None, max_connections=2, lease_id=None,
            if_modified_since=None, if_unmodified_since=None, if_match=None,
            if_none_match=None, timeout=None, use_byte_buffer=False):
        '''
        Creates a new blob from a file/stream, or updates the content of
        an existing blob, with automatic chunking and progress
        notifications.

        :param str container_name:
            Name of existing container.
        :param str blob_name:
            Name of blob to create or update.
        :param io.IOBase stream:
            Opened file/stream to upload as the blob content.
        :param int count:
            Number of bytes to read from the stream. This is optional, but
            should be supplied for optimal performance.
        :param ~azure.storage.blob.models.ContentSettings content_settings:
            ContentSettings object used to set blob properties.
        :param metadata:
            Name-value pairs associated with the blob as metadata.
        :type metadata: dict(str, str)
        :param bool validate_content:
            If true, calculates an MD5 hash for each chunk of the blob. The storage
            service checks the hash of the content that has arrived with the hash
            that was sent. This is primarily valuable for detecting bitflips on
            the wire if using http instead of https as https (the default) will
            already validate. Note that this MD5 hash is not stored with the
            blob. Also note that if enabled, the memory-efficient upload algorithm
            will not be used, because computing the MD5 hash requires buffering
            entire blocks, and doing so defeats the purpose of the memory-efficient algorithm.
        :param progress_callback:
            Callback for progress with signature function(current, total) where
            current is the number of bytes transfered so far, and total is the
            size of the blob, or None if the total size is unknown.
        :type progress_callback: func(current, total)
        :param int max_connections:
            Maximum number of parallel connections to use when the blob size exceeds
            64MB. Note that parallel upload requires the stream to be seekable.
        :param str lease_id:
            Required if the blob has an active lease.
        :param datetime if_modified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only
            if the resource has been modified since the specified time.
        :param datetime if_unmodified_since:
            A DateTime value. Azure expects the date value passed in to be UTC.
            If timezone is included, any non-UTC datetimes will be converted to UTC.
            If a date is passed in without timezone info, it is assumed to be UTC.
            Specify this header to perform the operation only if
            the resource has not been modified since the specified date/time.
        :param str if_match:
            An ETag value, or the wildcard character (*). Specify this header to perform
            the operation only if the resource's ETag matches the value specified.
        :param str if_none_match:
            An ETag value, or the wildcard character (*). Specify this header
            to perform the operation only if the resource's ETag does not match
            the value specified. Specify the wildcard character (*) to perform
            the operation only if the resource does not exist, and fail the
            operation if it does exist.
        :param int timeout:
            The timeout parameter is expressed in seconds. This method may make
            multiple calls to the Azure service and the timeout will apply to
            each call individually.
        :param bool use_byte_buffer:
            If True, this will force usage of the original full block buffering upload path.
            By default, this value is False and will employ a memory-efficient,
            streaming upload algorithm under the following conditions:
            The provided stream is seekable, 'require_encryption' is False, and
            MAX_BLOCK_SIZE >= MIN_LARGE_BLOCK_UPLOAD_THRESHOLD.
            One should consider the drawbacks of using this approach. In order to achieve
            memory-efficiency, a IOBase stream or file-like object is segmented into logical blocks
            using a SubStream wrapper. In order to read the correct data, each SubStream must acquire
            a lock so that it can safely seek to the right position on the shared, underlying stream.
            If max_connections > 1, the concurrency will result in a considerable amount of seeking on
            the underlying stream. For the most common inputs such as a file-like stream object, seeking
            is an inexpensive operation and this is not much of a concern. However, for other variants of streams
            this may not be the case. The trade-off for memory-efficiency must be weighed against the cost of seeking
            with your input stream.
            The SubStream class will attempt to buffer up to 4 MB internally to reduce the amount of
            seek and read calls to the underlying stream. This is particularly beneficial when uploading larger blocks.
        :return: ETag and last modified properties for the Block Blob
        :rtype: :class:`~azure.storage.blob.models.ResourceProperties`
        '''
        _validate_not_none('container_name', container_name)
        _validate_not_none('blob_name', blob_name)
        _validate_not_none('stream', stream)
        _validate_encryption_required(self.require_encryption, self.key_encryption_key)

        # Adjust count to include padding if we are expected to encrypt.
        adjusted_count = count
        if (self.key_encryption_key is not None) and (adjusted_count is not None):
            adjusted_count += (16 - (count % 16))

        # Do single put if the size is smaller than MAX_SINGLE_PUT_SIZE
        if adjusted_count is not None and (adjusted_count < self.MAX_SINGLE_PUT_SIZE):
            if progress_callback:
                progress_callback(0, count)

            data = stream.read(count)
            resp = self._put_blob(
                container_name=container_name,
                blob_name=blob_name,
                blob=data,
                content_settings=content_settings,
                metadata=metadata,
                validate_content=validate_content,
                lease_id=lease_id,
                if_modified_since=if_modified_since,
                if_unmodified_since=if_unmodified_since,
                if_match=if_match,
                if_none_match=if_none_match,
                timeout=timeout)

            if progress_callback:
                progress_callback(count, count)

            return resp
        else:  # Size is larger than MAX_SINGLE_PUT_SIZE, must upload with multiple put_block calls
            cek, iv, encryption_data = None, None, None

            use_original_upload_path = use_byte_buffer or validate_content or self.require_encryption or \
                                       self.MAX_BLOCK_SIZE < self.MIN_LARGE_BLOCK_UPLOAD_THRESHOLD or \
                                       hasattr(stream, 'seekable') and not stream.seekable() or \
                                       not hasattr(stream, 'seek') or not hasattr(stream, 'tell')

            if use_original_upload_path:
                if self.key_encryption_key:
                    cek, iv, encryption_data = _generate_blob_encryption_data(self.key_encryption_key)

                block_ids = _upload_blob_chunks(
                    blob_service=self,
                    container_name=container_name,
                    blob_name=blob_name,
                    blob_size=count,
                    block_size=self.MAX_BLOCK_SIZE,
                    stream=stream,
                    max_connections=max_connections,
                    progress_callback=progress_callback,
                    validate_content=validate_content,
                    lease_id=lease_id,
                    uploader_class=_BlockBlobChunkUploader,
                    timeout=timeout,
                    content_encryption_key=cek,
                    initialization_vector=iv
                )
            else:
                block_ids = _upload_blob_substream_blocks(
                    blob_service=self,
                    container_name=container_name,
                    blob_name=blob_name,
                    blob_size=count,
                    block_size=self.MAX_BLOCK_SIZE,
                    stream=stream,
                    max_connections=max_connections,
                    progress_callback=progress_callback,
                    validate_content=validate_content,
                    lease_id=lease_id,
                    uploader_class=_BlockBlobChunkUploader,
                    timeout=timeout,
                )

            return self._put_block_list(
                container_name=container_name,
                blob_name=blob_name,
                block_list=block_ids,
                content_settings=content_settings,
                metadata=metadata,
                validate_content=validate_content,
                lease_id=lease_id,
                if_modified_since=if_modified_since,
                if_unmodified_since=if_unmodified_since,
                if_match=if_match,
                if_none_match=if_none_match,
                timeout=timeout,
                encryption_data=encryption_data
            )
    def update_message(self, queue_name, message_id, pop_receipt, visibility_timeout,
                       content=None, timeout=None):
        '''
        Updates the visibility timeout of a message. You can also use this
        operation to update the contents of a message.

        This operation can be used to continually extend the invisibility of a 
        queue message. This functionality can be useful if you want a worker role 
        to "lease" a queue message. For example, if a worker role calls get_messages 
        and recognizes that it needs more time to process a message, it can 
        continually extend the message's invisibility until it is processed. If 
        the worker role were to fail during processing, eventually the message 
        would become visible again and another worker role could process it.

        If the key-encryption-key field is set on the local service object, this method will
        encrypt the content before uploading.

        :param str queue_name:
            The name of the queue containing the message to update.
        :param str message_id:
            The message id identifying the message to update.
        :param str pop_receipt:
            A valid pop receipt value returned from an earlier call
            to the :func:`~get_messages` or :func:`~update_message` operation.
        :param int visibility_timeout:
            Specifies the new visibility timeout value, in seconds,
            relative to server time. The new value must be larger than or equal
            to 0, and cannot be larger than 7 days. The visibility timeout of a
            message cannot be set to a value later than the expiry time. A
            message can be updated until it has been deleted or has expired.
        :param obj content:
            Message content. Allowed type is determined by the encode_function 
            set on the service. Default is str.
        :param int timeout:
            The server timeout, expressed in seconds.
        :return: 
            A list of :class:`~azure.storage.queue.models.QueueMessage` objects. For convenience,
            this object is also populated with the content, although it is not returned by the service.
        :rtype: list(:class:`~azure.storage.queue.models.QueueMessage`)
        '''

        _validate_encryption_required(self.require_encryption, self.key_encryption_key)

        _validate_not_none('queue_name', queue_name)
        _validate_not_none('message_id', message_id)
        _validate_not_none('pop_receipt', pop_receipt)
        _validate_not_none('visibility_timeout', visibility_timeout)
        request = HTTPRequest()
        request.method = 'PUT'
        request.host_locations = self._get_host_locations()
        request.path = _get_path(queue_name, True, message_id)
        request.query = {
            'popreceipt': _to_str(pop_receipt),
            'visibilitytimeout': _int_to_str(visibility_timeout),
            'timeout': _int_to_str(timeout)
        }

        if content is not None:
            request.body = _get_request_body(_convert_queue_message_xml(content, self.encode_function,
                                                                        self.key_encryption_key))

        return self._perform_request(request, _parse_queue_message_from_headers)
    def put_message(self, queue_name, content, visibility_timeout=None,
                    time_to_live=None, timeout=None):
        '''
        Adds a new message to the back of the message queue. 

        The visibility timeout specifies the time that the message will be 
        invisible. After the timeout expires, the message will become visible. 
        If a visibility timeout is not specified, the default value of 0 is used.

        The message time-to-live specifies how long a message will remain in the 
        queue. The message will be deleted from the queue when the time-to-live 
        period expires.

        If the key-encryption-key field is set on the local service object, this method will
        encrypt the content before uploading.

        :param str queue_name:
            The name of the queue to put the message into.
        :param obj content:
            Message content. Allowed type is determined by the encode_function 
            set on the service. Default is str. The encoded message can be up to 
            64KB in size.
        :param int visibility_timeout:
            If not specified, the default value is 0. Specifies the
            new visibility timeout value, in seconds, relative to server time.
            The value must be larger than or equal to 0, and cannot be
            larger than 7 days. The visibility timeout of a message cannot be
            set to a value later than the expiry time. visibility_timeout
            should be set to a value smaller than the time-to-live value.
        :param int time_to_live:
            Specifies the time-to-live interval for the message, in
            seconds. The maximum time-to-live allowed is 7 days. If this
            parameter is omitted, the default time-to-live is 7 days.
        :param int timeout:
            The server timeout, expressed in seconds.
        :return:
            A :class:`~azure.storage.queue.models.QueueMessage` object.
            This object is also populated with the content although it is not
            returned from the service.
        :rtype: :class:`~azure.storage.queue.models.QueueMessage`
        '''

        _validate_encryption_required(self.require_encryption, self.key_encryption_key)

        _validate_not_none('queue_name', queue_name)
        _validate_not_none('content', content)
        request = HTTPRequest()
        request.method = 'POST'
        request.host_locations = self._get_host_locations()
        request.path = _get_path(queue_name, True)
        request.query = {
            'visibilitytimeout': _to_str(visibility_timeout),
            'messagettl': _to_str(time_to_live),
            'timeout': _int_to_str(timeout)
        }

        request.body = _get_request_body(_convert_queue_message_xml(content, self.encode_function,
                                                                    self.key_encryption_key))

        message_list = self._perform_request(request, _convert_xml_to_queue_messages,
                                             [self.decode_function, False,
                                              None, None, content])
        return message_list[0]
示例#11
0
    async def put_message(self,
                          queue_name,
                          content,
                          visibility_timeout=None,
                          time_to_live=None,
                          timeout=None):
        '''
        Adds a new message to the back of the message queue. 

        The visibility timeout specifies the time that the message will be 
        invisible. After the timeout expires, the message will become visible. 
        If a visibility timeout is not specified, the default value of 0 is used.

        The message time-to-live specifies how long a message will remain in the 
        queue. The message will be deleted from the queue when the time-to-live 
        period expires.

        If the key-encryption-key field is set on the local service object, this method will
        encrypt the content before uploading.

        :param str queue_name:
            The name of the queue to put the message into.
        :param obj content:
            Message content. Allowed type is determined by the encode_function 
            set on the service. Default is str. The encoded message can be up to 
            64KB in size.
        :param int visibility_timeout:
            If not specified, the default value is 0. Specifies the
            new visibility timeout value, in seconds, relative to server time.
            The value must be larger than or equal to 0, and cannot be
            larger than 7 days. The visibility timeout of a message cannot be
            set to a value later than the expiry time. visibility_timeout
            should be set to a value smaller than the time-to-live value.
        :param int time_to_live:
            Specifies the time-to-live interval for the message, in
            seconds. The maximum time-to-live allowed is 7 days. If this
            parameter is omitted, the default time-to-live is 7 days.
        :param int timeout:
            The server timeout, expressed in seconds.
        :return:
            A :class:`~azure.storage.queue.models.QueueMessage` object.
            This object is also populated with the content although it is not
            returned from the service.
        :rtype: :class:`~azure.storage.queue.models.QueueMessage`
        '''

        _validate_encryption_required(self.require_encryption,
                                      self.key_encryption_key)

        _validate_not_none('queue_name', queue_name)
        _validate_not_none('content', content)
        request = HTTPRequest()
        request.method = 'POST'
        request.host_locations = self._get_host_locations()
        request.path = _get_path(queue_name, True)
        self._set_basic_headers(request)
        request.query = {
            'visibilitytimeout': _to_str(visibility_timeout),
            'messagettl': _to_str(time_to_live),
            'timeout': _int_to_str(timeout)
        }

        request.body = _get_request_body(
            _convert_queue_message_xml(content, self.encode_function,
                                       self.key_encryption_key))

        message_list = await self._perform_request(
            request, _convert_xml_to_queue_messages,
            [self.decode_function, False, None, None, content])
        return message_list[0]
示例#12
0
    async def update_message(self,
                             queue_name,
                             message_id,
                             pop_receipt,
                             visibility_timeout,
                             content=None,
                             timeout=None):
        '''
        Updates the visibility timeout of a message. You can also use this
        operation to update the contents of a message.

        This operation can be used to continually extend the invisibility of a 
        queue message. This functionality can be useful if you want a worker role 
        to "lease" a queue message. For example, if a worker role calls get_messages 
        and recognizes that it needs more time to process a message, it can 
        continually extend the message's invisibility until it is processed. If 
        the worker role were to fail during processing, eventually the message 
        would become visible again and another worker role could process it.

        If the key-encryption-key field is set on the local service object, this method will
        encrypt the content before uploading.

        :param str queue_name:
            The name of the queue containing the message to update.
        :param str message_id:
            The message id identifying the message to update.
        :param str pop_receipt:
            A valid pop receipt value returned from an earlier call
            to the :func:`~get_messages` or :func:`~update_message` operation.
        :param int visibility_timeout:
            Specifies the new visibility timeout value, in seconds,
            relative to server time. The new value must be larger than or equal
            to 0, and cannot be larger than 7 days. The visibility timeout of a
            message cannot be set to a value later than the expiry time. A
            message can be updated until it has been deleted or has expired.
        :param obj content:
            Message content. Allowed type is determined by the encode_function 
            set on the service. Default is str.
        :param int timeout:
            The server timeout, expressed in seconds.
        :return: 
            A list of :class:`~azure.storage.queue.models.QueueMessage` objects. For convenience,
            this object is also populated with the content, although it is not returned by the service.
        :rtype: list(:class:`~azure.storage.queue.models.QueueMessage`)
        '''

        _validate_encryption_required(self.require_encryption,
                                      self.key_encryption_key)

        _validate_not_none('queue_name', queue_name)
        _validate_not_none('message_id', message_id)
        _validate_not_none('pop_receipt', pop_receipt)
        _validate_not_none('visibility_timeout', visibility_timeout)
        request = HTTPRequest()
        request.method = 'PUT'
        request.host_locations = self._get_host_locations()
        self._set_basic_headers(request)
        request.path = _get_path(queue_name, True, message_id)
        request.query = {
            'popreceipt': _to_str(pop_receipt),
            'visibilitytimeout': _int_to_str(visibility_timeout),
            'timeout': _int_to_str(timeout)
        }

        if content is not None:
            request.body = _get_request_body(
                _convert_queue_message_xml(content, self.encode_function,
                                           self.key_encryption_key))

        return await self._perform_request(request,
                                           _parse_queue_message_from_headers)