Skip to content

Commit

Permalink
Refactor prune_build to use s3_client for S3 object deletion
Browse files Browse the repository at this point in the history
Updated prune_build function to handle S3 object deletion using boto3's s3_client.
Replaced resource-based object deletion with client-based approach using list_objects_v2 and delete_objects.
Added handling for cases where no objects are found under the specified prefix.
Improved error handling for missing keys (NoSuchKey) and other potential ClientErrors.
  • Loading branch information
gursewak1997 committed Oct 3, 2024
1 parent de10fe1 commit 7550a58
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/cmd-cloud-prune
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,25 @@ def prune_images(s3, build, images_to_keep, dry_run, bucket, prefix):
raise Exception("Some errors were encountered")


def prune_build(bucket, prefix, build_id, dry_run, s3_client):
def prune_build(s3_client, bucket, prefix, build_id, dry_run):
build_prefix = os.path.join(prefix, f"{build_id}/")
if dry_run:
print(f"Would delete all resources in {bucket}/{build_prefix}.")
else:
try:
bucket.objects.filter(Prefix=build_prefix).delete()
print(f"Pruned {build_id} completely from s3")
# List all objects under the specified prefix
objects_to_delete = s3_client.list_objects_v2(Bucket=bucket, Prefix=build_prefix)
if 'Contents' in objects_to_delete:
# Extract the object keys and format them for deletion
delete_keys = [{'Key': obj['Key']} for obj in objects_to_delete['Contents']]
# Delete objects
s3_client.delete_objects(Bucket=bucket, Delete={'Objects': delete_keys})
print(f"Pruned {build_id} completely from {bucket}/{build_prefix}.")
else:
print(f"No objects found to delete in {bucket}/{build_prefix}.")
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'NoSuchKey':
print(f"{bucket}/{build_prefix} already pruned.")
print(f"{bucket}/{build_prefix} already pruned or doesn't exist.")
else:
raise Exception(f"Error pruning {build_id}: {e.response['Error']['Message']}")

Expand Down

0 comments on commit 7550a58

Please sign in to comment.