QueueClient class

Definition

A client to interact with a specific Queue.

QueueClient(account_url, queue_name, credential=None, **kwargs)
Inheritance
builtins.object
azure.storage.queue._shared.base_client.StorageAccountHostsMixin
QueueClient

Parameters

account_url
str

The URL to the storage account. In order to create a client given the full URI to the queue, use the from_queue_url(queue_url, credential=None, **kwargs) classmethod.

queue_name
str

The name of the queue.

credential

The credentials with which to authenticate. This is optional if the account URL already has a SAS token. The value can be a SAS token string, an account shared access key, or an instance of a TokenCredentials class from azure.identity.

Examples

Create the queue client with url and credential.


   token_auth_queue = QueueClient.from_queue_url(
       queue_url=queue.url,
       credential=sas_token
   )

Methods

clear_messages

Deletes all messages from the specified queue.

create_queue

Creates a new queue in the storage account.

If a queue with the same name already exists, the operation fails with a ResourceExistsError.

delete_message

Deletes the specified message.

Normally after a client retrieves a message with the receive messages operation, the client is expected to process and delete the message. To delete the message, you must have the message object itself, or two items of data: id and pop_receipt. The id is returned from the previous receive_messages operation. The pop_receipt is returned from the most recent <xref:azure.storage.queue.receive_messages> or <xref:azure.storage.queue.update_message> operation. In order for the delete_message operation to succeed, the pop_receipt specified on the request must match the pop_receipt returned from the <xref:azure.storage.queue.receive_messages> or <xref:azure.storage.queue.update_message> operation.

delete_queue

Deletes the specified queue and any messages it contains.

When a queue is successfully deleted, it is immediately marked for deletion and is no longer accessible to clients. The queue is later removed from the Queue service during garbage collection.

Note that deleting a queue is likely to take at least 40 seconds to complete. If an operation is attempted against the queue while it was being deleted, an <xref:azure.storage.queue.HttpResponseError> will be thrown.

from_connection_string

Create QueueClient from a Connection String.

from_queue_url

A client to interact with a specific Queue.

get_queue_access_policy

Returns details about any stored access policies specified on the queue that may be used with Shared Access Signatures.

get_queue_properties

Returns all user-defined metadata for the specified queue.

The data returned does not include the queue's list of messages.

peek_messages

Retrieves one or more messages from the front of the queue, but does not alter the visibility of the message.

Only messages that are visible may be retrieved. When a message is retrieved for the first time with a call to <xref:azure.storage.queue.receive_messages>, its dequeue_count property is set to 1. If it is not deleted and is subsequently retrieved again, the dequeue_count property is incremented. The client may use this value to determine how many times a message has been retrieved. Note that a call to peek_messages does not increment the value of dequeue_count, but returns this value for the client to read.

If the key-encryption-key or resolver field is set on the local service object, the messages will be decrypted before being returned.

receive_messages

Removes one or more messages from the front of the queue.

When a message is retrieved from the queue, the response includes the message content and a pop_receipt value, which is required to delete the message. The message is not automatically deleted from the queue, but after it has been retrieved, it is not visible to other clients for the time interval specified by the visibility_timeout parameter.

If the key-encryption-key or resolver field is set on the local service object, the messages will be decrypted before being returned.

send_message

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.

set_queue_access_policy

Sets stored access policies for the queue that may be used with Shared Access Signatures.

When you set permissions for a queue, the existing permissions are replaced. To update the queue's permissions, call get_queue_access_policy(**kwargs) to fetch all access policies associated with the queue, modify the access policy that you wish to change, and then call this function with the complete set of data to perform the update.

When you establish a stored access policy on a queue, it may take up to 30 seconds to take effect. During this interval, a shared access signature that is associated with the stored access policy will throw an <xref:azure.storage.queue.HttpResponseError> until the access policy becomes active.

set_queue_metadata

Sets user-defined metadata on the specified queue.

Metadata is associated with the queue as name-value pairs.

update_message

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 receive_messages(**kwargs) 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.

clear_messages

Deletes all messages from the specified queue.

clear_messages(**kwargs)

Examples

Clears all messages.


   queue.clear_messages()

create_queue

Creates a new queue in the storage account.

If a queue with the same name already exists, the operation fails with a ResourceExistsError.

create_queue(**kwargs)

Returns

None or the result of cls(response)

Return type

None

Examples

Create a queue.


   queue.create_queue()

delete_message

Deletes the specified message.

Normally after a client retrieves a message with the receive messages operation, the client is expected to process and delete the message. To delete the message, you must have the message object itself, or two items of data: id and pop_receipt. The id is returned from the previous receive_messages operation. The pop_receipt is returned from the most recent <xref:azure.storage.queue.receive_messages> or <xref:azure.storage.queue.update_message> operation. In order for the delete_message operation to succeed, the pop_receipt specified on the request must match the pop_receipt returned from the <xref:azure.storage.queue.receive_messages> or <xref:azure.storage.queue.update_message> operation.

delete_message(message, pop_receipt=None, **kwargs)

Parameters

message
str or QueueMessage
Required

The message object or id identifying the message to delete.

pop_receipt
str
Required

A valid pop receipt value returned from an earlier call to the <xref:azure.storage.queue.receive_messages> or <xref:azure.storage.queue.update_message>.

Examples

Delete a message.


   # Get the message at the front of the queue
   msg = next(queue.receive_messages())

   # Delete the specified message
   queue.delete_message(msg)

delete_queue

Deletes the specified queue and any messages it contains.

When a queue is successfully deleted, it is immediately marked for deletion and is no longer accessible to clients. The queue is later removed from the Queue service during garbage collection.

Note that deleting a queue is likely to take at least 40 seconds to complete. If an operation is attempted against the queue while it was being deleted, an <xref:azure.storage.queue.HttpResponseError> will be thrown.

delete_queue(**kwargs)

Return type

None

Examples

Delete a queue.


   queue.delete_queue()

from_connection_string

Create QueueClient from a Connection String.

from_connection_string(conn_str, queue_name, credential=None, **kwargs)

Parameters

conn_str
str
Required

A connection string to an Azure Storage account.

queue_name
str
Required

The queue name.

credential
Required

The credentials with which to authenticate. This is optional if the account URL already has a SAS token, or the connection string already has shared access key values. The value can be a SAS token string, an account shared access key, or an instance of a TokenCredentials class from azure.identity.

credential
default value: None

Returns

A queue client.

Return type

Examples

Create the queue client from connection string.


   from azure.storage.queue import QueueClient
   queue = QueueClient.from_connection_string(self.connection_string, "myqueue1")

from_queue_url

A client to interact with a specific Queue.

from_queue_url(queue_url, credential=None, **kwargs)

Parameters

queue_url
str
Required

The full URI to the queue, including SAS token if used.

credential
Required

The credentials with which to authenticate. This is optional if the account URL already has a SAS token. The value can be a SAS token string, an account shared access key, or an instance of a TokenCredentials class from azure.identity.

credential
default value: None

Returns

A queue client.

Return type

get_queue_access_policy

Returns details about any stored access policies specified on the queue that may be used with Shared Access Signatures.

get_queue_access_policy(**kwargs)

Returns

A dictionary of access policies associated with the queue.

Return type

dict(str, AccessPolicy)

get_queue_properties

Returns all user-defined metadata for the specified queue.

The data returned does not include the queue's list of messages.

get_queue_properties(**kwargs)

Returns

User-defined metadata for the queue.

Return type

Examples

Get the properties on the queue.


   properties = queue.get_queue_properties().metadata

peek_messages

Retrieves one or more messages from the front of the queue, but does not alter the visibility of the message.

Only messages that are visible may be retrieved. When a message is retrieved for the first time with a call to <xref:azure.storage.queue.receive_messages>, its dequeue_count property is set to 1. If it is not deleted and is subsequently retrieved again, the dequeue_count property is incremented. The client may use this value to determine how many times a message has been retrieved. Note that a call to peek_messages does not increment the value of dequeue_count, but returns this value for the client to read.

If the key-encryption-key or resolver field is set on the local service object, the messages will be decrypted before being returned.

peek_messages(max_messages=None, **kwargs)

Parameters

max_messages
int
Required

A nonzero integer value that specifies the number of messages to peek from the queue, up to a maximum of 32. By default, a single message is peeked from the queue with this operation.

Returns

A list of QueueMessage objects. Note that next_visible_on and pop_receipt will not be populated as peek does not pop the message and can only retrieve already visible messages.

Return type

list(<xref:azure.storage.queue.QueueMessage>)

Examples

Peek messages.


   # Peek at one message at the front of the queue
   msg = queue.peek_messages()

   # Peek at the last 5 messages
   messages = queue.peek_messages(max_messages=5)

   # Print the last 5 messages
   for message in messages:
       print(message.content)

receive_messages

Removes one or more messages from the front of the queue.

When a message is retrieved from the queue, the response includes the message content and a pop_receipt value, which is required to delete the message. The message is not automatically deleted from the queue, but after it has been retrieved, it is not visible to other clients for the time interval specified by the visibility_timeout parameter.

If the key-encryption-key or resolver field is set on the local service object, the messages will be decrypted before being returned.

receive_messages(**kwargs)

Returns

Returns a message iterator of dict-like Message objects.

Return type

Examples

Receive messages from the queue.


   # Receive messages one-by-one
   messages = queue.receive_messages()
   for msg in messages:
       print(msg.content)

   # Receive messages by batch
   messages = queue.receive_messages(messages_per_page=5)
   for msg_batch in messages.by_page():
       for msg in msg_batch:
           print(msg.content)
           queue.delete_message(msg)

send_message

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.

send_message(content, **kwargs)

Parameters

content
obj
Required

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.

Returns

A QueueMessage object. This object is also populated with the content although it is not returned from the service.

Return type

Examples

Send messages.


   queue.send_message(u"message1")
   queue.send_message(u"message2", visibility_timeout=30)  # wait 30s before becoming visible
   queue.send_message(u"message3")
   queue.send_message(u"message4")
   queue.send_message(u"message5")

set_queue_access_policy

Sets stored access policies for the queue that may be used with Shared Access Signatures.

When you set permissions for a queue, the existing permissions are replaced. To update the queue's permissions, call get_queue_access_policy(**kwargs) to fetch all access policies associated with the queue, modify the access policy that you wish to change, and then call this function with the complete set of data to perform the update.

When you establish a stored access policy on a queue, it may take up to 30 seconds to take effect. During this interval, a shared access signature that is associated with the stored access policy will throw an <xref:azure.storage.queue.HttpResponseError> until the access policy becomes active.

set_queue_access_policy(signed_identifiers, **kwargs)

Parameters

signed_identifiers
dict(str, AccessPolicy)
Required

SignedIdentifier access policies to associate with the queue. This may contain up to 5 elements. An empty dict will clear the access policies set on the service.

Examples

Set an access policy on the queue.


   # Create an access policy
   from azure.storage.queue import AccessPolicy, QueueSasPermissions
   access_policy = AccessPolicy()
   access_policy.start = datetime.utcnow() - timedelta(hours=1)
   access_policy.expiry = datetime.utcnow() + timedelta(hours=1)
   access_policy.permission = QueueSasPermissions(read=True)
   identifiers = {'my-access-policy-id': access_policy}

   # Set the access policy
   queue.set_queue_access_policy(identifiers)

set_queue_metadata

Sets user-defined metadata on the specified queue.

Metadata is associated with the queue as name-value pairs.

set_queue_metadata(metadata=None, **kwargs)

Parameters

metadata
dict(str, str)
Required

A dict containing name-value pairs to associate with the queue as metadata.

Examples

Set metadata on the queue.


   metadata = {'foo': 'val1', 'bar': 'val2', 'baz': 'val3'}
   queue.set_queue_metadata(metadata=metadata)

update_message

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 receive_messages(**kwargs) 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.

update_message(message, pop_receipt=None, content=None, **kwargs)

Parameters

message
str or QueueMessage
Required

The message object or id identifying the message to update.

pop_receipt
str
Required

A valid pop receipt value returned from an earlier call to the receive_messages(**kwargs) or update_message(message, pop_receipt=None, content=None, **kwargs) operation.

content
obj
Required

Message content. Allowed type is determined by the encode_function set on the service. Default is str.

Returns

A QueueMessage object. For convenience, this object is also populated with the content, although it is not returned by the service.

Return type

Examples

Update a message.


   # Send a message
   queue.send_message(u"update me")

   # Receive the message
   messages = queue.receive_messages()

   # Update the message
   list_result = next(messages)
   message = queue.update_message(
       list_result.id,
       pop_receipt=list_result.pop_receipt,
       visibility_timeout=0,
       content=u"updated")