@@ -201,6 +201,11 @@ def get_all_bucket_objects(user_bucket_name):
201201 # list_objects_v2 can utmost return 1000 objects in a single response
202202 # if there are more objects, the response will have a key "IsTruncated" set to True
203203 # and a key "NextContinuationToken" which can be used to get the next set of objects
204+
205+ # TODO:
206+ # Currently, all objects are loaded into memory, which can be problematic for large datasets.
207+ # To optimize, convert this function into a generator that accepts a `batch_size` parameter (capped at 1,000)
208+ # and yields objects in batches.
204209 while response .get ("IsTruncated" ):
205210 response = s3_client .list_objects_v2 (
206211 Bucket = user_bucket_name ,
@@ -224,13 +229,16 @@ def delete_all_bucket_objects(user_id, user_bucket_name):
224229 if not object_list :
225230 return
226231
227- logger .info (
232+ logger .debug (
228233 f"Deleting all contents from '{ user_bucket_name } ' for user '{ user_id } ' before deleting the bucket"
229234 )
230235 keys = [{"Key" : obj .get ("Key" )} for obj in object_list ]
231236
232237 # According to the docs, up to 1000 objects can be deleted in a single request:
233238 # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.delete_objects
239+
240+ # TODO: When `get_all_bucket_objects` is converted to a generator,
241+ # we can remove this batching logic and retrieve objects in batches of 1,000 for deletion.
234242 limit = 1000
235243 for offset in range (0 , len (keys ), limit ):
236244 response = s3_client .delete_objects (
@@ -266,7 +274,7 @@ def delete_user_bucket(user_id: str) -> Union[str, None]:
266274 )
267275 return None
268276
269- logger .debug (f"Deleting bucket '{ user_bucket_name } ' for user '{ user_id } '" )
277+ logger .info (f"Deleting bucket '{ user_bucket_name } ' for user '{ user_id } '" )
270278 try :
271279 delete_all_bucket_objects (user_id , user_bucket_name )
272280 s3_client .delete_bucket (Bucket = user_bucket_name )
0 commit comments