Improve the "emptying/deleting" process of the S3-bucket.

This commit is contained in:
Lampros Smyrnaios 2024-03-11 13:34:38 +02:00
parent dd394f18a0
commit ce3e149a95
1 changed files with 31 additions and 10 deletions

View File

@ -117,30 +117,51 @@ public class S3ObjectStore {
} }
public void emptyBucket(String bucketName, boolean shouldDeleteBucket) throws Exception { public void emptyBucket(String bucketName, boolean shouldDeleteBucket)
{
logger.warn("Going to " + (shouldDeleteBucket ? "delete" : "empty") + " bucket \"" + bucketName + "\"!"); logger.warn("Going to " + (shouldDeleteBucket ? "delete" : "empty") + " bucket \"" + bucketName + "\"!");
// First list the objects of the bucket. // First list the objects of the bucket.
Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build()); Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
int countDeletedFiles = 0;
int countFilesNotDeleted = 0;
long totalSize = 0;
Item item;
// Then, delete the objects. // Then, delete the objects.
for ( Result<Item> resultItem : results ) { for ( Result<Item> resultItem : results ) {
try { try {
if ( !deleteFile(resultItem.get().objectName(), bucketName) ) { item = resultItem.get();
logger.error("Cannot proceed with bucket deletion, since only an empty bucket can be removed!");
return;
}
} catch (Exception e) { } catch (Exception e) {
logger.warn("Could not remove " + resultItem.get().objectName()); logger.error("Could not get the item-object of one of the S3-Objects returned from the bucket!", e);
countFilesNotDeleted ++;
continue;
} }
totalSize += item.size();
if ( !deleteFile(item.objectName(), bucketName) ) { // The reason and for what object, is already logged.
logger.error("Cannot proceed with bucket deletion, since only an empty bucket can be removed!");
countFilesNotDeleted ++;
} else
countDeletedFiles ++;
} }
if ( shouldDeleteBucket ) { if ( shouldDeleteBucket ) {
// Lastly, delete the empty bucket. if ( countFilesNotDeleted == 0 ) {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); // Lastly, delete the empty bucket. We need to do this last, as in case it's not empty, we get an error!
} try {
minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
logger.info("Bucket \"" + bucketName + "\" was deleted!");
} catch (Exception e) {
logger.error("Bucket \"" + bucketName + "\" could not be deleted!", e);
}
} else
logger.error("Cannot execute the \"removeBucket\" command for bucket \"" + bucketName + "\", as " + countFilesNotDeleted + " files failed to be deleted!");
} else
logger.info("Bucket \"" + bucketName + "\" was emptied!");
logger.info("Bucket " + bucketName + " was " + (shouldDeleteBucket ? "deleted!" : "emptied!")); logger.info(countDeletedFiles + " files were deleted, amounting to " + ((totalSize/1024)/1024) + " MB.");
} }