| 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-messages: |
| 12 |
|
| 13 |
############################################ |
| 14 |
Sending and receiving messages in Amazon SQS |
| 15 |
############################################ |
| 16 |
|
| 17 |
This Python example shows you how to send, receive, and delete messages in a queue. |
| 18 |
|
| 19 |
The scenario |
| 20 |
============ |
| 21 |
|
| 22 |
In this example, Python code is used to send and receive messages. The code uses the AWS SDK for Python |
| 23 |
to send and receive messages by using these methods of the AWS.SQS client class: |
| 24 |
|
| 25 |
* `send_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.send_message>`_. |
| 26 |
|
| 27 |
* `receive_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message>`_. |
| 28 |
|
| 29 |
* `delete_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.delete_message>`_. |
| 30 |
|
| 31 |
For more information about Amazon SQS messages, see |
| 32 |
`Sending a Message to an Amazon SQS Queue <http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-send-message.html>`_ |
| 33 |
and `Receiving and Deleting a Message from an Amazon SQS Queue <http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-receive-delete-message.html>`_ |
| 34 |
in the *Amazon Simple Queue Service Developer Guide*. |
| 35 |
|
| 36 |
Prerequisite tasks |
| 37 |
================== |
| 38 |
|
| 39 |
To set up and run this example, you must first complete these tasks: |
| 40 |
|
| 41 |
* Create an Amazon SQS queue. For an example of creating an Amazon SQS |
| 42 |
queue, see :ref:`aws-boto3-sqs-create-queue`. |
| 43 |
|
| 44 |
.. _aws-boto3-sqs-send-message: |
| 45 |
|
| 46 |
Send a message to a queue |
| 47 |
========================= |
| 48 |
|
| 49 |
The example below shows how to: |
| 50 |
|
| 51 |
* Send a message to a queue using |
| 52 |
`send_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.send_message>`_. |
| 53 |
|
| 54 |
Example |
| 55 |
------- |
| 56 |
|
| 57 |
.. code-block:: python |
| 58 |
|
| 59 |
import boto3 |
| 60 |
|
| 61 |
# Create SQS client |
| 62 |
sqs = boto3.client('sqs') |
| 63 |
|
| 64 |
queue_url = 'SQS_QUEUE_URL' |
| 65 |
|
| 66 |
# Send message to SQS queue |
| 67 |
response = sqs.send_message( |
| 68 |
QueueUrl=queue_url, |
| 69 |
DelaySeconds=10, |
| 70 |
MessageAttributes={ |
| 71 |
'Title': { |
| 72 |
'DataType': 'String', |
| 73 |
'StringValue': 'The Whistler' |
| 74 |
}, |
| 75 |
'Author': { |
| 76 |
'DataType': 'String', |
| 77 |
'StringValue': 'John Grisham' |
| 78 |
}, |
| 79 |
'WeeksOn': { |
| 80 |
'DataType': 'Number', |
| 81 |
'StringValue': '6' |
| 82 |
} |
| 83 |
}, |
| 84 |
MessageBody=( |
| 85 |
'Information about current NY Times fiction bestseller for ' |
| 86 |
'week of 12/11/2016.' |
| 87 |
) |
| 88 |
) |
| 89 |
|
| 90 |
print(response['MessageId']) |
| 91 |
|
| 92 |
|
| 93 |
Receive and delete messages from a queue |
| 94 |
======================================== |
| 95 |
|
| 96 |
The example below shows how to: |
| 97 |
|
| 98 |
* Receive a message from a queue using |
| 99 |
`receive_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.receive_message>`_. |
| 100 |
|
| 101 |
* Delete a message from a queue using |
| 102 |
`delete_message <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sqs.html#SQS.Client.delete_message>`_. |
| 103 |
|
| 104 |
Example |
| 105 |
------- |
| 106 |
|
| 107 |
.. code-block:: python |
| 108 |
|
| 109 |
import boto3 |
| 110 |
|
| 111 |
# Create SQS client |
| 112 |
sqs = boto3.client('sqs') |
| 113 |
|
| 114 |
queue_url = 'SQS_QUEUE_URL' |
| 115 |
|
| 116 |
# Receive message from SQS queue |
| 117 |
response = sqs.receive_message( |
| 118 |
QueueUrl=queue_url, |
| 119 |
AttributeNames=[ |
| 120 |
'SentTimestamp' |
| 121 |
], |
| 122 |
MaxNumberOfMessages=1, |
| 123 |
MessageAttributeNames=[ |
| 124 |
'All' |
| 125 |
], |
| 126 |
VisibilityTimeout=0, |
| 127 |
WaitTimeSeconds=0 |
| 128 |
) |
| 129 |
|
| 130 |
message = response['Messages'][0] |
| 131 |
receipt_handle = message['ReceiptHandle'] |
| 132 |
|
| 133 |
# Delete received message from queue |
| 134 |
sqs.delete_message( |
| 135 |
QueueUrl=queue_url, |
| 136 |
ReceiptHandle=receipt_handle |
| 137 |
) |
| 138 |
print('Received and deleted message: %s' % message) |