1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Access Amazon SQS using Python's Boto

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

  1. pradeep

    pradeep Team Leader

    Apr 4, 2005
    Likes Received:
    Trophy Points:
    Kolkata, India
    Home Page:
    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:

    $ 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:

    #!/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:

    #!/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()
    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
    ## deleting a 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