Access Amazon SQS using Python's Boto

Discussion in 'Python' started by pradeep, Apr 30, 2013.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Amazon's Simple Queue Service is a highly scalable service to help cloud-based applications use a queuing system which is reliable & scalable. Amazon's queue has it's pros & cons, like the messages may not be received in the order they were queued, and you may receive duplicates, I had written an article on Amazon SQS where I had explained the features & limitations of the service.

    In this article we'll be looking at a Python library called Boto which makes accessing AWS services really easy. We'll go through installation of Boto and using it to perform various operations on Amazon SQS.

    Installing Boto



    Boto's git repository makes it very easy to install, just follow the commands below:

    Code:
    $ git clone git://github.com/boto/boto.git
    $ cd boto
    $ python setup.py install
    

    Accessing SQS with Boto



    The foremost step would be to create a connection to Amazon SQS before we can start performing other operations. Look and the code snippet below:

    Code:
    #!/usr/bin/env python
    
    from boto.sqs.connection import SQSConnection
    
    sqs_conn = SQSConnection('accessKey', 'SecRetKey')
    
    Now that we have setup a connection to the service, we can go about and perform SQS operations like creating queues, queueing, fetching & deleting data, delete queues, and fetch queue attributes. Follow the code snippet below, I have added comments wherever possible for easier understanding:

    Code:
    #!/usr/bin/env python
    
    from boto.sqs.connection import SQSConnection
    from boto.sqs.message import Message
    
    sqs_conn = SQSConnection('accessKey', 'SecRetKey')
    
    ## get the previously created queue
    my_queue = sqs_conn.get_queue('myTestQueue')
    
    ## queue a message/data
    message = Message()
    
    message.set_body('Data-1,Pradeep,Kolkata')
    status = my_queue.write(message)
    
    ## fetching queue items
    rcv_message = my_queue.read()
    if rcv_message != None:
        print rcv_message.get_body()
    
    ## fetching multiple queue items in one call
    messages = my_queue.get_messages()
    for msg in messages:
        print msg.get_body()
    
    ## deleting message
    my_queue.delete_message(rcv_message)
    
    ## deleting a queue
    sqs_conn.delete_queue(my_queue)
    
    ## get the queue attributes and dump the values
    queue_attr = my_queue.get_queue_attributes()
    
    print vars(queue_attr)
    
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice