Upload File into S3 and Send Notification Using SNS Python boto3

Posted on by Sumit Kumar
import pandas as pd
import boto3
from io import StringIO

data = [['Billu', 31], ['amit', 30], ['Mayank', 14],['prabhat', 30]]
df = pd.DataFrame(data, columns = ['Name', 'Age'])
df


ACCESS_KEY="AKIATHOPBKF36EHONBNK"
SECRET_KEY="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"


def upload_s3(df):
    i="test.csv"
    s3 = boto3.client("s3",aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY)
    csv_buf = StringIO()
    df.to_csv(csv_buf, header=True, index=False)
    csv_buf.seek(0)
    s3.put_object(Bucket="test-deltafrog-bucket", Body=csv_buf.getvalue(), Key='2021/'+i)
    
######################SNS###############
     
ses = boto3.client('sns', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY,region_name='us-east-1')
sns_topicname_arn="arn:aws:sns:us-east-1:222161883511:s3_upload_notification"
#Publish the message to the SNS topic    

def publishMessage(snsArn, msg) :
	client = boto3.client('sns', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY,region_name='us-east-1')
	client.publish (TargetArn=snsArn,Message=msg)
    

upload_s3(df)    

msg="file has been uploaded into bucket test-deltafrog-bucket"

publishMessage(sns_topicname_arn, msg) 
Posted in AWS.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*