Spring cleaning
Sam's pipeline has been running for a long time now. Since the beginning of 2018, her automated system has been diligently uploading her report to the gid-staging
bucket.
In City governments, record retention is a huge issue, and many government officials prefer not to keep records in existence past the mandated retention dates.
As time has passed, the City Council asked Sam to clean out old CSV files from previous years that have passed the retention period. 2018 is safe to delete.
Sam has initialized the client and assigned it to the s3
variable. Help her clean out all records for 2018 from S3!
This exercise is part of the course
Introduction to AWS Boto in Python
Exercise instructions
- List only objects that start with
'2018/final_'
in'gid-staging'
bucket. - Iterate over the objects, deleting each one.
- Print the keys of remaining objects in the bucket.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# List only objects that start with '2018/final_'
response = s3.____(Bucket='____',
____='2018/final_')
# Iterate over the objects
if 'Contents' in response:
for obj in response['____']:
# Delete the object
s3.____(Bucket='____', Key=obj['Key'])
# Print the keys of remaining objects in the bucket
response = s3.____(____='gid-staging')
for obj in response['Contents']:
print(obj['____'])