Skip to content

Commit fdbd09c

Browse files
Merge pull request #88732 from mhopkins-msft/python-base64
Added info about Base64 for binary data
2 parents c3c4a5d + 09fa405 commit fdbd09c

File tree

2 files changed

+46
-59
lines changed

2 files changed

+46
-59
lines changed

articles/storage/queues/storage-python-how-to-use-queue-storage.md

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
---
2-
title: How to use Queue storage from Python - Azure Storage
2+
title: How to use Azure Queue storage from Python - Azure Storage
33
description: Learn how to use the Azure Queue service from Python to create and delete queues, and insert, get, and delete messages.
44
author: mhopkins-msft
55
ms.service: storage
66

77
ms.author: mhopkins
8-
ms.date: 12/14/2018
8+
ms.date: 09/17/2019
99
ms.service: storage
1010
ms.subservice: queues
1111
ms.topic: conceptual
1212
ms.reviewer: cbrooks
1313
---
1414

15-
# How to use Queue storage from Python
15+
# How to use Azure Queue storage from Python
16+
1617
[!INCLUDE [storage-selector-queue-include](../../../includes/storage-selector-queue-include.md)]
1718

1819
[!INCLUDE [storage-try-azure-tools-queues](../../../includes/storage-try-azure-tools-queues.md)]
1920

2021
## Overview
21-
This guide shows you how to perform common scenarios using the Azure Queue storage service. The samples are written in Python and use the [Microsoft Azure Storage SDK for Python]. The scenarios covered include **inserting**, **peeking**, **getting**, and **deleting** queue messages, as well as **creating and deleting queues**. For more information on queues, refer to the [Next Steps] section.
22+
23+
This guide shows you how to perform common scenarios using the Azure Queue storage service. The samples are written in Python and use the [Microsoft Azure Storage SDK for Python]. The scenarios covered include inserting, peeking, getting, and deleting queue messages, as well as creating and deleting queues. For more information on queues, refer to the [Next steps](#next-steps) section.
2224

2325
[!INCLUDE [storage-queue-concepts-include](../../../includes/storage-queue-concepts-include.md)]
2426

2527
[!INCLUDE [storage-create-account-include](../../../includes/storage-create-account-include.md)]
2628

27-
## Download and Install Azure Storage SDK for Python
29+
## Download and install Azure Storage SDK for Python
2830

29-
The [Azure Storage SDK for Python](https://github.com/azure/azure-storage-python) requires Python 2.7, 3.3, 3.4, 3.5, or 3.6.
31+
The [Azure Storage SDK for Python](https://github.com/azure/azure-storage-python) requires Python version 2.7, 3.3, or later.
3032

3133
### Install via PyPi
3234

@@ -47,52 +49,51 @@ To view and run a sample application that shows how to use Python with Azure Que
4749

4850
To run the sample application, make sure you have installed both the `azure-storage-queue` and `azure-storage-common` packages.
4951

50-
## How To: Create a Queue
52+
## Create a queue
5153

52-
The **QueueService** object lets you work with queues. The following code creates a **QueueService** object. Add the following near the top of any Python file in which you wish to programmatically access Azure Storage:
54+
The [QueueService](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice) object lets you work with queues. The following code creates a `QueueService` object. Add the following near the top of any Python file in which you wish to programmatically access Azure Storage:
5355

5456
```python
5557
from azure.storage.queue import QueueService
5658
```
5759

58-
The following code creates a **QueueService** object using the storage account name and account key. Replace 'myaccount' and 'mykey' with your account name and key.
60+
The following code creates a `QueueService` object using the storage account name and account key. Replace *myaccount* and *mykey* with your account name and key.
5961

6062
```python
6163
queue_service = QueueService(account_name='myaccount', account_key='mykey')
6264

6365
queue_service.create_queue('taskqueue')
6466
```
6567

66-
## How To: Insert a Message into a Queue
67-
To insert a message into a queue, use the **put\_message** method to
68-
create a new message and add it to the queue.
68+
## Insert a message into a queue
69+
70+
To insert a message into a queue, use the [put_message](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice#put-message-queue-name--content--visibility-timeout-none--time-to-live-none--timeout-none-) method to create a new message and add it to the queue.
6971

7072
```python
7173
queue_service.put_message('taskqueue', u'Hello World')
7274
```
7375

74-
## How To: Peek at the Next Message
75-
You can peek at the message in the front of a queue without removing it
76-
from the queue by calling the **peek\_messages** method. By default,
77-
**peek\_messages** peeks at a single message.
76+
Azure queue messages are stored as text. If you want to store binary data, setup Base64 encoding and decoding functions on the queue service object before putting a message in the queue.
77+
78+
```python
79+
# setup queue Base64 encoding and decoding functions
80+
queue_service.encode_function = QueueMessageFormat.binary_base64encode
81+
queue_service.decode_function = QueueMessageFormat.binary_base64decode
82+
```
83+
84+
## Peek at the next message
85+
86+
You can peek at the message in the front of a queue without removing it from the queue by calling the [peek_messages](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice#peek-messages-queue-name--num-messages-none--timeout-none-) method. By default, `peek_messages` peeks at a single message.
7887

7988
```python
8089
messages = queue_service.peek_messages('taskqueue')
8190
for message in messages:
8291
print(message.content)
8392
```
8493

85-
## How To: Dequeue Messages
86-
Your code removes a message from a queue in two steps. When you call
87-
**get\_messages**, you get the next message in a queue by default. A
88-
message returned from **get\_messages** becomes invisible to any other
89-
code reading messages from this queue. By default, this message stays
90-
invisible for 30 seconds. To finish removing the message from the queue,
91-
you must also call **delete\_message**. This two-step process of removing
92-
a message assures that when your code fails to process a message due to
93-
hardware or software failure, another instance of your code can get the
94-
same message and try again. Your code calls **delete\_message** right
95-
after the message has been processed.
94+
## Dequeue messages
95+
96+
Your code removes a message from a queue in two steps. When you call [get_messages](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice#get-messages-queue-name--num-messages-none--visibility-timeout-none--timeout-none-), you get the next message in a queue by default. A message returned from `get_messages` becomes invisible to any other code reading messages from this queue. By default, this message stays invisible for 30 seconds. To finish removing the message from the queue, you must also call [delete_message](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice#delete-message-queue-name--message-id--pop-receipt--timeout-none-). This two-step process of removing a message assures that when your code fails to process a message due to hardware or software failure, another instance of your code can get the same message and try again. Your code calls `delete_message` right after the message has been processed.
9697

9798
```python
9899
messages = queue_service.get_messages('taskqueue')
@@ -101,13 +102,7 @@ for message in messages:
101102
queue_service.delete_message('taskqueue', message.id, message.pop_receipt)
102103
```
103104

104-
There are two ways you can customize message retrieval from a queue.
105-
First, you can get a batch of messages (up to 32). Second, you can set a
106-
longer or shorter invisibility timeout, allowing your code more or less
107-
time to fully process each message. The following code example uses the
108-
**get\_messages** method to get 16 messages in one call. Then it processes
109-
each message using a for loop. It also sets the invisibility timeout to
110-
five minutes for each message.
105+
There are two ways you can customize message retrieval from a queue. First, you can get a batch of messages (up to 32). Second, you can set a longer or shorter invisibility timeout, allowing your code more or less time to fully process each message. The following code example uses the `get_messages` method to get 16 messages in one call. Then it processes each message using a for loop. It also sets the invisibility timeout to five minutes for each message.
111106

112107
```python
113108
messages = queue_service.get_messages(
@@ -117,12 +112,9 @@ for message in messages:
117112
queue_service.delete_message('taskqueue', message.id, message.pop_receipt)
118113
```
119114

120-
## How To: Change the Contents of a Queued Message
121-
You can change the contents of a message in-place in the queue. If the
122-
message represents a work task, you could use this feature to update the
123-
status of the work task. The code below uses the **update\_message**
124-
method to update a message. The visibility timeout is set to 0, meaning the
125-
message appears immediately and the content is updated.
115+
## Change the contents of a queued message
116+
117+
You can change the contents of a message in-place in the queue. If the message represents a work task, you could use this feature to update the status of the work task. The code below uses the [update_message](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice#update-message-queue-name--message-id--pop-receipt--visibility-timeout--content-none--timeout-none-) method to update a message. The visibility timeout is set to 0, meaning the message appears immediately and the content is updated.
126118

127119
```python
128120
messages = queue_service.get_messages('taskqueue')
@@ -131,30 +123,28 @@ for message in messages:
131123
'taskqueue', message.id, message.pop_receipt, 0, u'Hello World Again')
132124
```
133125

134-
## How To: Get the Queue Length
135-
You can get an estimate of the number of messages in a queue. The
136-
**get\_queue\_metadata** method asks the queue service to return metadata
137-
about the queue, and the **approximate_message_count**. The result is only
138-
approximate because messages can be added or removed after the
139-
queue service responds to your request.
126+
## Get the queue length
127+
128+
You can get an estimate of the number of messages in a queue. The [get_queue_metadata](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice#get-queue-metadata-queue-name--timeout-none-) method asks the queue service to return metadata about the queue, and the `approximate_message_count`. The result is only approximate because messages can be added or removed after the queue service responds to your request.
140129

141130
```python
142131
metadata = queue_service.get_queue_metadata('taskqueue')
143132
count = metadata.approximate_message_count
144133
```
145134

146-
## How To: Delete a Queue
147-
To delete a queue and all the messages contained in it, call the
148-
**delete\_queue** method.
135+
## Delete a queue
136+
137+
To delete a queue and all the messages contained in it, call the [delete_queue](/python/api/azure-storage-queue/azure.storage.queue.queueservice.queueservice#delete-queue-queue-name--fail-not-exist-false--timeout-none-) method.
149138

150139
```python
151140
queue_service.delete_queue('taskqueue')
152141
```
153142

154-
## Next Steps
155-
Now that you've learned the basics of Queue storage, follow these links
156-
to learn more.
143+
## Next steps
144+
145+
Now that you've learned the basics of queue storage, follow these links to learn more.
157146

147+
* [Azure Queues Python API reference](/python/api/azure-storage-queue)
158148
* [Python Developer Center](https://azure.microsoft.com/develop/python/)
159149
* [Azure Storage Services REST API](https://msdn.microsoft.com/library/azure/dd179355)
160150

includes/storage-queue-concepts-include.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ ms.topic: include
55
ms.date: 10/26/2018
66
ms.author: tamram
77
---
8-
## What is Queue Storage?
9-
Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account.
8+
## What is queue storage?
109

11-
Common uses of Queue storage include:
10+
Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account. Queue storage is often used to create a backlog of work to process asynchronously.
1211

13-
* Creating a backlog of work to process asynchronously
14-
* Passing messages from an Azure web role to an Azure worker role
12+
## Queue service concepts
1513

16-
## Queue Service Concepts
17-
The Queue service contains the following components:
14+
The Azure Queue service contains the following components:
1815

1916
![Queue1](./media/storage-queue-concepts-include/queue1.png)
2017

0 commit comments

Comments
 (0)