I am trying to take the list of unused S3 buckets. For that I am taking latest updated object on each bucket and comparing the last modified date with a given date. I am receiving key error on contents. This is my code.
import json
import boto3
from datetime import datetime
import csv
def lambda_handler(event, context):
# connect to S3
s3 = boto3.client(service_name='s3',
region_name='my region')
# Declaring required variables
column_list = ["Bucket_Name", "Object_Name", "Last_Modified_Date"]
result_list = []
date_to_compare = datetime(2021, 12, 24).strftime('%Y-%m-%d')
# Last modified object for each s3 bucket
Bucket_list = s3.list_buckets()
for bucket in Bucket_list['Buckets']:
objectlist = s3.list_objects_v2(Bucket=bucket["Name"])
latest_updated = max(objectlist["Contents"],key=lambda x: x['LastModified'])
if(latest_updated['LastModified'].strftime('%Y-%m-%d') < date_to_compare):
values = [bucket["Name"], latest_updated["Key"],latest_updated["LastModified"].strftime('%Y-%m-%d')]
result_list.append(values)
file = open('s3_buckets_unused.csv', 'w ', newline='')
# writing the data into the file
with file:
write = csv.writer(file)
write.writerow(column_list)
write.writerows(result_list)
This works perfectly fine on my local system, but not on AWS Lambda. I receive the below error. Any insights?
Response { "errorMessage": "'Contents'", "errorType": "KeyError", "requestId": "e981bffb-61da-4bc3-8078-29c13018e659", "stackTrace": [ " File "/var/task/lambda_function.py", line 17, in lambda_handler\n latest_updated = max(objectlist["Contents"],key=lambda x: x['LastModified'])\n" ] }
CodePudding user response:
If a bucket is empty, it will have no Contents. You have to check for that:
import json
import boto3
from datetime import datetime
import csv
def lambda_handler(event, context):
# connect to S3
s3 = boto3.client(service_name='s3',
region_name='my region')
# Declaring required variables
column_list = ["Bucket_Name", "Object_Name", "Last_Modified_Date"]
result_list = []
date_to_compare = datetime(2021, 12, 24).strftime('%Y-%m-%d')
# Last modified object for each s3 bucket
Bucket_list = s3.list_buckets()
for bucket in Bucket_list['Buckets']:
objectlist = s3.list_objects_v2(Bucket=bucket["Name"])
if 'Contents' not in objectlist: continue
latest_updated = max(objectlist["Contents"],key=lambda x: x['LastModified'])
if(latest_updated['LastModified'].strftime('%Y-%m-%d') < date_to_compare):
values = [bucket["Name"], latest_updated["Key"],latest_updated["LastModified"].strftime('%Y-%m-%d')]
result_list.append(values)
file = open('s3_buckets_unused.csv', 'w ', newline='')
# writing the data into the file
with file:
write = csv.writer(file)
write.writerow(column_list)
write.writerows(result_list)
