1

I am creating an AWS Lambda function using Python to create a file in an S3 bucket but it is only creating one row. Need iteration based creation.

Below is my code:

import json
import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):
    bucket ='xyz'

    eventToUpload = {}
    eventToUpload['ITEM_ID'] = 1234
    eventToUpload['Name'] = John
    eventToUpload['Office'] = NY

    fileName = 'testevent' + '.json'

    uploadByteStream = bytes(json.dumps(eventToUpload).encode('UTF-8'))

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

    print('Put Complete')

Above code is creating a json file in the S3 bucket but I want to iteration of say 5 so that file has 5 record.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

0

AWS S3 doesn't by default, nor does it support, append content to an existing file.

You can either:

  • Loop through your events on your Python script to create multiple files with an uniquely identified fileName prefix/suffix, say, ITEM_ID;
  • Read and parse the existing file beforehand, if any, then append your events to it and write/replace the file.

This question seems to have useful answers to your case as well.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rovelcio Junior
  • 661
  • 3
  • 5