Search code examples
pythonamazon-web-servicesboto3amazon-sns

Read message from a file in python and publish it via sns topic


I have a requirement to read output from txt file and this is result from a python script execution. This .txt file output/content needs to be published via sns topic . Can someone help me on this please?

import boto3
sns = boto3.client('sns')
# Publish a simple message to the specified SNS topic
response = sns.publish(
TopicArn='arn:aws:sns:us-west-2:xxxxxxxxx',   
Message='Message',

)
# Print out the response
 print(response)

Solution

  • Read the .txt like this

    with open("/path/to/txt/file.txt", "r") as txt_file:
        output = txt_file.read() # might need .readlines() or .read().splitlines(). Whatever works for you
        Message = output