ReceiveMessage
In this article:
ReceiveMessage#
Description#
Receives one or more messages from the specified queue. Supports the long polling mode.
Request Syntax#
POST / HTTP/1.1
Content-type: application/json
{
"QueueUrl": "string",
"MaxNumberOfMessages": integer,
"MessageSystemAttributeNames": [
"string"
],
"VisibilityTimeout": integer,
"WaitTimeSeconds": integer
}
Request Parameters#
Required parameters#
QueueUrl — The URL of the queue from which the messages are being requested.
Type: String
Required: Yes
Optional parameters#
MaxNumberOfMessages — The maximum number of messages to be received.
Type: Integer
Required: No
Range: 1 to 10
MessageSystemAttributeNames — The list of system attributes of the message being requested (see Supported attributes).
Type: Array of strings
Required: No
VisibilityTimeout — The time (in seconds) during which the received messages remain invisible to other consumers. If not specified, the value of the VisibilityTimeout queue attribute is used.
Type: Integer
Required: No
Range: 0 to 43,200 (12 hours)
WaitTimeSeconds — The wait time (in seconds) for the long polling mode. If not specified, the value of the ReceiveMessageWaitTimeSeconds queue attribute is used.
Type: Integer
Required: No
Range: 2 to 20
Supported attributes#
All — All available system attributes of the message.
ApproximateFirstReceiveTimestamp — The first time when the message is received from the queue, in Unix epoch format (in milliseconds).
ApproximateReceiveCount — The number of times that the message was received from the queue but not deleted.
SenderId — The ID of the message producer.
SentTimestamp — The time when the message was sent, in Unix epoch format (in milliseconds).
Response Syntax#
HTTP/1.1 200
Content-type: application/json
{
"Messages": [
{
"Attributes": {
"string": "string"
},
"Body": "string",
"MD5OfBody": "string",
"MessageId": "string",
"ReceiptHandle": "string"
}
]
}
Response Elements#
Messages — The list of received messages.
Type: List of Message objects
Errors#
QueueDoesNotExist — The specified queue does not exist.
Examples#
boto3
import boto3
session = boto3.Session(
aws_access_key_id="<AWS_ACCESS_KEY_ID>",
aws_secret_access_key="<AWS_SECRET_ACCESS_KEY>",
region_name="",
)
sqs_client = session.client(
'sqs',
endpoint_url='https://sqs.ru-msk.k2.cloud/'
)
response = sqs_client.receive_message(
QueueUrl='https://sqs.ru-msk.k2.cloud/123456789012/my-queue',
MaxNumberOfMessages=10
)
for message in response.get('Messages', []):
print(f"Message ID: {message['MessageId']}")
print(f"Body: {message['Body']}")
print(f"Receipt Handle: {message['ReceiptHandle']}")
boto3 - Long Polling
import boto3
session = boto3.Session(
aws_access_key_id="<AWS_ACCESS_KEY_ID>",
aws_secret_access_key="<AWS_SECRET_ACCESS_KEY>",
region_name="",
)
sqs_client = session.client(
'sqs',
endpoint_url='https://sqs.ru-msk.k2.cloud/'
)
response = sqs_client.receive_message(
QueueUrl='https://sqs.ru-msk.k2.cloud/123456789012/my-queue',
MaxNumberOfMessages=10,
WaitTimeSeconds=20,
MessageSystemAttributeNames=['All']
)
for message in response.get('Messages', []):
print(message)
aws-cli
aws sqs --endpoint https://sqs.ru-msk.k2.cloud/ receive-message \\
--queue-url https://sqs.ru-msk.k2.cloud/123456789012/my-queue \\
--max-number-of-messages 10 \\
--wait-time-seconds 20