Monday, October 28, 2019

Interacting with AWS SQS Buffer Using Python: Example

First of all, make sure you have AWS CLI installed on your local machine by checking the existence  of both files "~/.aws/config" and "~/.aws/credentials":

 ~/.aws/credentials:

[default]
aws_access_key_id = XXX
aws_secret_access_key = XXXXXX

~/.aws/config:

[default]
region = us-east-2
output = text

In case you did not find them, check out the links at the bottom of this article. Then, create an SQS queue by executing the following Python's script:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto3
sqs = boto3.client('sqs')
sqs.create_queue(QueueName='translation_requests.fifo',
                 Attributes={'FifoQueue': 'true'})

Afterward, you should find the SQS queue named 'translation_requests.fifo' listed on the AWS console as follows:


Now that your queue has been created, create a Python list containing a list of phrases inside a Python file named 'phrase_list.py' as follows:

phrases = [
    "Hello!",
    "How are you?",
    "What’s up?",
    "I’m fine. And you?",
    "Please.",
    "Thank you.",
    "Thank you very much.",
    "You’re Welcome",
    "Goodbye.",
    "See you soon.",
    "Cheers!",
    "Excuse me.",
    "I’m sorry.",
    "What’s your name?",
    "My name is",
    "Nice to meet you.",
    "Where are you from?",
    "I’m from",
    "I’d like to introduce my friend",
    "How old are you?",
    "I’m 33 years old.",
    "What do you do for a living?",
    "I’m a doctor",
    "What do you do for fun?",
    "What are your hobbies?",
    "Do you speak English?",
    "Could you please speak a little slower?",
    "Could you write that down?",
    "Could you repeat that?",
    "How much?",
    "Can I pay by credit card?",
    "Here you go.",
    "What time do you open?",
    "Do you have anything cheaper?",
    "It’s too expensive.",
    "Here’s my passport.",
    "Do I have to change trains?",
    "Do I need a seat reservation?",
    "Is this seat taken?",
    "Could you call me a taxi?",
    "Could you let me know when to get off?",
    "Could you recommend a good restaurant?",
    "What would you recommend?",
    "What are some local specialties?",
    "What is the special of the day?",
    "Could I see the menu, please?",
    "That was delicious!",
    "This isn’t what I ordered.",
    "Can I buy you a drink?",
    "Let’s have another!",
    "Where can I find tourist information?",
    "Do you have a map?",
    "Can you show me that on the map?",
    "What is the entrance fee?",
    "What is that building?",
    "What is there to see around here?",
    "I have a reservation.",
    "Could I see the room?",
    "I’d like to stay for five nights.",
    "Is breakfast included?",
    "Could I get a different room?",
    "Is there a restaurant here?",
    "Is there pharmacy nearby?",
    "Can I use your phone?"
]

Then, create a python script to push the previous list of phrases into your SQS queue as follows:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto3
import json
import random
import time
import uuid

from phrase_list import phrases

sqs = boto3.client('sqs')

QUEUE_URL = sqs.get_queue_url(QueueName='translation_requests.fifo'
                              )['QueueUrl']


def send_requests_to_sqs():
    message = {'phrase': random.choice(phrases),
               'lang_code': random.choice(['de', 'es', 'fr'])}

    json_message = json.dumps(message)
    sqs.send_message(QueueUrl=QUEUE_URL, MessageBody=json_message,
                     MessageGroupId='Translations',
                     MessageDeduplicationId=str(uuid.uuid4()))
    print('Message sent to SQS.')


i = 0
while i < 50:
    i = i + 1
    time.sleep(2)
    send_requests_to_sqs()

Once executed, check the number of available messages inside your queue on the AWS console:


Thereafter, write another Python script to pull messages from the queue and then translate them using the TranslateText API as follows:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto3
import json
import time

sqs = boto3.client('sqs')
translate = boto3.client('translate')

QUEUE_URL = sqs.get_queue_url(QueueName='translation_requests.fifo'
                              )['QueueUrl']


def consume_messages():

    # Load messages from the SQS queue

    response = sqs.receive_message(QueueUrl=QUEUE_URL,
                                   MaxNumberOfMessages=1)['Messages'][0]
    receipt_handle = response['ReceiptHandle']
    message_body = json.loads(response['Body'])

    # Translate the message to the language

    translation = translate.translate_text(Text=message_body['phrase'],
            SourceLanguageCode='en',
            TargetLanguageCode=message_body['lang_code'])
    print('Translated "' + message_body['phrase'] + '" to: "' \
        + translation['TranslatedText'] + '"')
    sqs.delete_message(QueueUrl=QUEUE_URL, ReceiptHandle=receipt_handle)


i = 0
while i < 1000:
    i += 1
    try:
        consume_messages()
        time.sleep(1)
    except:
        i = 1000
        print ('oops')

Now that you have consumed the messages stored in the queue, you can proceed to delete the queue by running the following Python script:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto3

sqs = boto3.client('sqs')

QUEUE_URL = sqs.get_queue_url(QueueName='translation_requests.fifo'
                              )['QueueUrl']

sqs.delete_queue(QueueUrl=QUEUE_URL)

--------------------------------------------------------------------------------------------------------------------------

You could have emptied the queue rather than deleted it by running the following code:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import boto3

sqs = boto3.client('sqs')

QUEUE_URL = sqs.get_queue_url(QueueName='translation_requests.fifo'
                              )['QueueUrl']

sqs.purge_queue(QueueUrl=QUEUE_URL)

--------------------------------------------------------------------------------------------------------------------------

Useful links:

1. Download and install the AWS CLI
2. Configure the AWS CLI
3. Install Boto3

No comments:

Post a Comment