| 1 |
.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto3-sqs-using-queues: |
| 12 |
|
| 13 |
########################## |
| 14 |
Using queues in Amazon SQS |
| 15 |
########################## |
| 16 |
|
| 17 |
This Python example shows you how to: |
| 18 |
|
| 19 |
* Get a list of all of your message queues |
| 20 |
|
| 21 |
* Obtain the URL for a particular queue |
| 22 |
|
| 23 |
* Create and delete queues |
| 24 |
|
| 25 |
The scenario |
| 26 |
============ |
| 27 |
|
| 28 |
In this example, Python code is used to work with queues. The code uses the AWS SDK for Python to use |
| 29 |
queues using these methods of the AWS.SQS client class: |
| 30 |
|
| 31 |
* `list_queues <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.list_queues>`_. |
| 32 |
|
| 33 |
* `create_queue <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.create_queue>`_. |
| 34 |
|
| 35 |
* `get_queue_url <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.get_queue_url>`_. |
| 36 |
|
| 37 |
* `delete_queue <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.delete_queue>`_. |
| 38 |
|
| 39 |
For more information about Amazon SQS messages, see |
| 40 |
`How Queues Work <http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-how-it-works.html>`_ |
| 41 |
in the *Amazon Simple Queue Service Developer Guide*. |
| 42 |
|
| 43 |
List your queues |
| 44 |
================ |
| 45 |
|
| 46 |
The example below shows how to: |
| 47 |
|
| 48 |
* List queues using |
| 49 |
`list_queues <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.list_queues>`_. |
| 50 |
|
| 51 |
Example |
| 52 |
------- |
| 53 |
|
| 54 |
.. code-block:: python |
| 55 |
|
| 56 |
import boto3 |
| 57 |
|
| 58 |
# Create SQS client |
| 59 |
sqs = boto3.client('sqs') |
| 60 |
|
| 61 |
# List SQS queues |
| 62 |
response = sqs.list_queues() |
| 63 |
|
| 64 |
print(response['QueueUrls']) |
| 65 |
|
| 66 |
.. _aws-boto3-sqs-create-queue: |
| 67 |
|
| 68 |
Create a queue |
| 69 |
============== |
| 70 |
|
| 71 |
The example below shows how to: |
| 72 |
|
| 73 |
* Create a queue using |
| 74 |
`create_queue <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.create_queue>`_. |
| 75 |
|
| 76 |
Example |
| 77 |
------- |
| 78 |
|
| 79 |
.. code-block:: python |
| 80 |
|
| 81 |
import boto3 |
| 82 |
|
| 83 |
# Create SQS client |
| 84 |
sqs = boto3.client('sqs') |
| 85 |
|
| 86 |
# Create a SQS queue |
| 87 |
response = sqs.create_queue( |
| 88 |
QueueName='SQS_QUEUE_NAME', |
| 89 |
Attributes={ |
| 90 |
'DelaySeconds': '60', |
| 91 |
'MessageRetentionPeriod': '86400' |
| 92 |
} |
| 93 |
) |
| 94 |
|
| 95 |
print(response['QueueUrl']) |
| 96 |
|
| 97 |
Get the URL for a queue |
| 98 |
======================= |
| 99 |
|
| 100 |
The example below shows how to: |
| 101 |
|
| 102 |
* Get the URL for a queue using |
| 103 |
`get_queue_url <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.get_queue_url>`_. |
| 104 |
|
| 105 |
Example |
| 106 |
------- |
| 107 |
|
| 108 |
.. code-block:: python |
| 109 |
|
| 110 |
import boto3 |
| 111 |
|
| 112 |
# Create SQS client |
| 113 |
sqs = boto3.client('sqs') |
| 114 |
|
| 115 |
# Get URL for SQS queue |
| 116 |
response = sqs.get_queue_url(QueueName='SQS_QUEUE_NAME') |
| 117 |
|
| 118 |
print(response['QueueUrl']) |
| 119 |
|
| 120 |
Delete a queue |
| 121 |
============== |
| 122 |
|
| 123 |
The example below shows how to: |
| 124 |
|
| 125 |
* Delete a queue using |
| 126 |
`delete_queue <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.delete_queue>`_. |
| 127 |
|
| 128 |
Example |
| 129 |
------- |
| 130 |
|
| 131 |
.. code-block:: python |
| 132 |
|
| 133 |
import boto3 |
| 134 |
|
| 135 |
# Create SQS client |
| 136 |
sqs = boto3.client('sqs') |
| 137 |
|
| 138 |
# Delete SQS queue |
| 139 |
sqs.delete_queue(QueueUrl='SQS_QUEUE_URL') |