Saturday, 19 September 2020

 Writing a Json file content into S3 bucket using Lambda function


import json

import boto3


#This is a client to represent s3

s3 =boto3.client('s3')


def lambda_handler(event, context):

#Name of the bucket in a variable    

    bucket = 'bharat101'


#File to be uploaded is listed as a dictionary    

    myFile1={'Name':'Sarala','Colour':'green'}

    print(myFile1)


#Naming the file as .json and storing in a variable

    fileName = 'Sarala' + '.json'


#Using the dump method in json to convert the python objects to a json object conversta as string and writes to a file

    convert = json.dumps(myFile1)


    s3.put_object(Bucket=bucket, Key=fileName, Body=convert)

    print('finsihed upload')