user_mda
user_mda

Reputation: 19388

Uploading CSV to S3 after for loop

I have a requirement where I need to create a CSV file with values read from a list and then upload it to S3. I do it as follows

 with open('/tmp/mycsv.csv', mode='w',  newline='') as csv_file:
     fieldnames = ['DomainName', 'Subject']
     writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
     writer.writeheader()
           
     for x in certificates:
         writer.writerow({'DomainName': 'hardcodeString1','Subject':'hardcodeString2'})
     s3 = boto3.client('s3')
     response = s3.upload_file('/tmp/mycsv.csv', 'my-bucket', 'mycsv.csv')

But the CSV file is not uploaded, If I remove the for loop, this works fine with hard coded values there is ano issues uploading the file. I even added a pause after writing row to see if it gets uploaded , there are no errors. What am I doing wrong?

Upvotes: 0

Views: 403

Answers (1)

Balaji Ambresh
Balaji Ambresh

Reputation: 5037

This is what you should be doing to ensure that the changes to the file are persisted:

with open('/tmp/mycsv.csv', mode='w',  newline='') as csv_file:
            fieldnames = ['DomainName', 'Subject']
            writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
            writer.writeheader()
           
            for x in certificates:
                writer.writerow({'DomainName': x[DomainName],'Subject':x[Subject]})
s3 = boto3.client('s3')
response = s3.upload_file('/tmp/mycsv.csv', 'my-bucket', 'mycsv.csv')

Upvotes: 4

Related Questions