问题
I have a python lambda with nested for loop
def lambda_handler(event, context):
acc_ids = json.loads(os.environ.get('ACC_ID'))
with open('/tmp/newcsv.csv', mode='w', newline='') as csv_file:
fieldnames = ['DomainName', 'Subject', 'Status', 'RenewalEligibility', 'InUseBy']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for acc_id in acc_ids:
try:
//do something
for region in regions_to_scan:
try:
// do something
if something:
for x in list:
// get values for row
writer.writerow('write rows here')
except Exception as e:
print(e)
continue
except Exception as e:
print(e)
continue
s3 = boto3.client('s3')
response = s3.upload_file('/tmp/newcsv.csv', 'my-bucket', 'newcsv.csv')
This is not uploading the file in S3, from other conversations I made sure that the uploading of the file is after the writing context is done. Can someone evaluate my script?
回答1:
Turns out the loop and the lambda were correct. The timeout on the lambda function was set to 10 secs and it never reached the code to upload the file. Increasing the timeout on lambda fixed my problem
来源:https://stackoverflow.com/questions/62542225/nested-loops-in-python-and-csv-file