2020-01-13 09:49:45 -08:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2020-04-22 17:36:41 -07:00
# SPDX-License-Identifier: Apache-2.0
2020-01-13 09:49:45 -08:00
"""
2020-03-18 14:47:10 -07:00
Purpose
2021-10-22 16:15:10 -07:00
Demonstrate basic queue operations in Amazon Simple Queue Service (Amazon SQS).
Learn how to create, get, and remove standard, FIFO, and dead-letter queues.
Usage is shown in the test/test_queue_wrapper.py file.
2020-01-13 09:49:45 -08:00
"""
2021-10-22 16:15:10 -07:00
# snippet-start:[python.example_code.sqs.queue_wrapper_imports]
2020-01-13 09:49:45 -08:00
import logging
import boto3
from botocore . exceptions import ClientError
logger = logging . getLogger ( __name__ )
2023-10-18 10:35:05 -07:00
sqs = boto3 . resource ( " sqs " )
2021-10-22 16:15:10 -07:00
# snippet-end:[python.example_code.sqs.queue_wrapper_imports]
2020-01-13 09:49:45 -08:00
2021-10-22 16:15:10 -07:00
# snippet-start:[python.example_code.sqs.CreateQueue]
2020-01-13 09:49:45 -08:00
def create_queue ( name , attributes = None ) :
"""
2020-04-24 11:19:31 -07:00
Creates an Amazon SQS queue.
2020-01-13 09:49:45 -08:00
:param name: The name of the queue. This is part of the URL assigned to the queue.
:param attributes: The attributes of the queue, such as maximum message size or
whether it ' s a FIFO queue.
:return: A Queue object that contains metadata about the queue and that can be used
to perform queue operations like sending and receiving messages.
"""
if not attributes :
attributes = { }
try :
2023-10-18 10:35:05 -07:00
queue = sqs . create_queue ( QueueName = name , Attributes = attributes )
2020-01-13 09:49:45 -08:00
logger . info ( " Created queue ' %s ' with URL= %s " , name , queue . url )
except ClientError as error :
logger . exception ( " Couldn ' t create queue named ' %s ' . " , name )
raise error
else :
return queue
2023-10-18 10:35:05 -07:00
2021-10-22 16:15:10 -07:00
# snippet-end:[python.example_code.sqs.CreateQueue]
2020-01-13 09:49:45 -08:00
2021-10-22 16:15:10 -07:00
# snippet-start:[python.example_code.sqs.GetQueueUrl]
2020-01-13 09:49:45 -08:00
def get_queue ( name ) :
"""
Gets an SQS queue by name.
:param name: The name that was used to create the queue.
:return: A Queue object.
"""
try :
queue = sqs . get_queue_by_name ( QueueName = name )
logger . info ( " Got queue ' %s ' with URL= %s " , name , queue . url )
except ClientError as error :
logger . exception ( " Couldn ' t get queue named %s . " , name )
raise error
else :
return queue
2023-10-18 10:35:05 -07:00
2021-10-22 16:15:10 -07:00
# snippet-end:[python.example_code.sqs.GetQueueUrl]
2020-01-13 09:49:45 -08:00
2021-10-22 16:15:10 -07:00
# snippet-start:[python.example_code.sqs.ListQueues]
2020-01-13 09:49:45 -08:00
def get_queues ( prefix = None ) :
"""
Gets a list of SQS queues. When a prefix is specified, only queues with names
that start with the prefix are returned.
:param prefix: The prefix used to restrict the list of returned queues.
:return: A list of Queue objects.
"""
if prefix :
queue_iter = sqs . queues . filter ( QueueNamePrefix = prefix )
else :
queue_iter = sqs . queues . all ( )
queues = list ( queue_iter )
if queues :
2023-10-18 10:35:05 -07:00
logger . info ( " Got queues: %s " , " , " . join ( [ q . url for q in queues ] ) )
2020-01-13 09:49:45 -08:00
else :
logger . warning ( " No queues found. " )
return queues
2023-10-18 10:35:05 -07:00
2021-10-22 16:15:10 -07:00
# snippet-end:[python.example_code.sqs.ListQueues]
2020-01-13 09:49:45 -08:00
2021-10-22 16:15:10 -07:00
# snippet-start:[python.example_code.sqs.DeleteQueue]
2020-01-13 09:49:45 -08:00
def remove_queue ( queue ) :
"""
2020-03-18 14:47:10 -07:00
Removes an SQS queue. When run against an AWS account, it can take up to
2020-01-13 09:49:45 -08:00
60 seconds before the queue is actually deleted.
:param queue: The queue to delete.
:return: None
"""
try :
queue . delete ( )
logger . info ( " Deleted queue with URL= %s . " , queue . url )
except ClientError as error :
logger . exception ( " Couldn ' t delete queue with URL= %s ! " , queue . url )
raise error
2023-10-18 10:35:05 -07:00
2021-10-22 16:15:10 -07:00
# snippet-end:[python.example_code.sqs.DeleteQueue]
2020-04-22 17:36:41 -07:00
2021-10-22 16:15:10 -07:00
# snippet-start:[python.example_code.sqs.Scenario_ManageQueues]
2020-04-22 17:36:41 -07:00
def usage_demo ( ) :
2021-10-22 16:15:10 -07:00
""" Shows how to create, list, and delete queues. """
2023-10-18 10:35:05 -07:00
print ( " - " * 88 )
2021-10-22 16:15:10 -07:00
print ( " Welcome to the Amazon Simple Queue Service (Amazon SQS) demo! " )
2023-10-18 10:35:05 -07:00
print ( " - " * 88 )
2021-10-22 16:15:10 -07:00
2023-10-18 10:35:05 -07:00
prefix = " sqs-usage-demo- "
2020-04-22 17:36:41 -07:00
river_queue = create_queue (
2023-10-18 10:35:05 -07:00
prefix + " peculiar-river " ,
{ " MaximumMessageSize " : str ( 1024 ) , " ReceiveMessageWaitTimeSeconds " : str ( 20 ) } ,
2020-04-22 17:36:41 -07:00
)
print ( f " Created queue with URL: { river_queue . url } . " )
lake_queue = create_queue (
2023-10-18 10:35:05 -07:00
prefix + " strange-lake.fifo " ,
2020-04-22 17:36:41 -07:00
{
2023-10-18 10:35:05 -07:00
" MaximumMessageSize " : str ( 4096 ) ,
" ReceiveMessageWaitTimeSeconds " : str ( 10 ) ,
" VisibilityTimeout " : str ( 300 ) ,
" FifoQueue " : str ( True ) ,
" ContentBasedDeduplication " : str ( True ) ,
} ,
2020-04-22 17:36:41 -07:00
)
print ( f " Created queue with URL: { lake_queue . url } . " )
2023-10-18 10:35:05 -07:00
stream_queue = create_queue ( prefix + " boring-stream " )
2020-04-22 17:36:41 -07:00
print ( f " Created queue with URL: { stream_queue . url } . " )
2023-10-18 10:35:05 -07:00
alias_queue = get_queue ( prefix + " peculiar-river " )
2020-04-22 17:36:41 -07:00
print ( f " Got queue with URL: { alias_queue . url } . " )
remove_queue ( stream_queue )
2023-10-18 10:35:05 -07:00
print (
f " Removed queue with URL: { stream_queue . url } . " ,
)
2020-04-22 17:36:41 -07:00
queues = get_queues ( prefix = prefix )
print ( f " Got { len ( queues ) } queues. " )
for queue in queues :
remove_queue ( queue )
print ( f " Removed queue with URL: { queue . url } . " )
2021-10-22 16:15:10 -07:00
print ( " Thanks for watching! " )
2023-10-18 10:35:05 -07:00
print ( " - " * 88 )
2021-10-22 16:15:10 -07:00
# snippet-end:[python.example_code.sqs.Scenario_ManageQueues]
2020-04-22 17:36:41 -07:00
2023-10-18 10:35:05 -07:00
if __name__ == " __main__ " :
2021-10-22 16:15:10 -07:00
usage_demo ( )