Skip to content

Commit 7550a58

Browse files
committed
Refactor prune_build to use s3_client for S3 object deletion
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.
1 parent de10fe1 commit 7550a58

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/cmd-cloud-prune

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,25 @@ def prune_images(s3, build, images_to_keep, dry_run, bucket, prefix):
388388
raise Exception("Some errors were encountered")
389389

390390

391-
def prune_build(bucket, prefix, build_id, dry_run, s3_client):
391+
def prune_build(s3_client, bucket, prefix, build_id, dry_run):
392392
build_prefix = os.path.join(prefix, f"{build_id}/")
393393
if dry_run:
394394
print(f"Would delete all resources in {bucket}/{build_prefix}.")
395395
else:
396396
try:
397-
bucket.objects.filter(Prefix=build_prefix).delete()
398-
print(f"Pruned {build_id} completely from s3")
397+
# List all objects under the specified prefix
398+
objects_to_delete = s3_client.list_objects_v2(Bucket=bucket, Prefix=build_prefix)
399+
if 'Contents' in objects_to_delete:
400+
# Extract the object keys and format them for deletion
401+
delete_keys = [{'Key': obj['Key']} for obj in objects_to_delete['Contents']]
402+
# Delete objects
403+
s3_client.delete_objects(Bucket=bucket, Delete={'Objects': delete_keys})
404+
print(f"Pruned {build_id} completely from {bucket}/{build_prefix}.")
405+
else:
406+
print(f"No objects found to delete in {bucket}/{build_prefix}.")
399407
except botocore.exceptions.ClientError as e:
400408
if e.response['Error']['Code'] == 'NoSuchKey':
401-
print(f"{bucket}/{build_prefix} already pruned.")
409+
print(f"{bucket}/{build_prefix} already pruned or doesn't exist.")
402410
else:
403411
raise Exception(f"Error pruning {build_id}: {e.response['Error']['Message']}")
404412

0 commit comments

Comments
 (0)