pyaws-s3 1.0.27__tar.gz → 1.0.28__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyaws-s3
3
- Version: 1.0.27
3
+ Version: 1.0.28
4
4
  Summary: A Python package for AWS S3 utilities
5
5
  Author-email: Giuseppe Zileni <giuseppe.zileni@gmail.com>
6
6
  Keywords: aws,s3,utilities
@@ -446,28 +446,52 @@ class S3Client:
446
446
  logger.error(f"Error uploading file: {str(e)}")
447
447
  raise Exception(f"Error uploading file: {str(e)}")
448
448
 
449
- async def delete_all(self, filter : str | None = None) -> None:
449
+ async def delete_all(self, **kwargs) -> None:
450
450
  """
451
451
  Delete all files from an S3 bucket.
452
452
 
453
453
  Args:
454
454
  filter (str | None): Optional filter to delete specific files. If None, all files will be deleted.
455
+ prefix (str | None): Optional prefix to filter files by their key. If None, all files will be deleted.
456
+
455
457
  Raises:
456
458
  Exception: If there is an error deleting the files.
457
459
  """
458
460
  try:
459
461
  s3_client = self._get_s3_client()
462
+
463
+ prefix = kwargs.get("prefix", None)
464
+ filter = kwargs.get("filter", None)
465
+
466
+ if prefix is not None and not prefix.endswith('/'):
467
+ prefix += '/'
460
468
 
461
469
  # List all objects in the bucket
462
- objects = s3_client.list_objects_v2(Bucket=self.bucket_name)
470
+ objects = None
471
+ if prefix is None:
472
+ # List all objects in the bucket
473
+ objects = s3_client.list_objects_v2(Bucket=self.bucket_name)
474
+ else:
475
+ # List objects with the specified prefix
476
+ logger.info(f"Listing objects with prefix: {prefix}")
477
+ objects = s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=prefix)
463
478
 
464
479
  # Check if the bucket contains any objects
465
480
  if 'Contents' in objects:
466
481
  for obj in objects['Contents']:
467
- if filter in obj['Key']:
468
- # Delete each object
482
+ if filter is not None:
483
+ if filter in obj['Key']:
484
+ # Delete each object
485
+ s3_client.delete_object(Bucket=self.bucket_name, Key=obj['Key'])
486
+ print(f"Deleted {obj['Key']}")
487
+ else:
488
+ logger.info(f"Skipping {obj['Key']} as it does not match the filter: {filter}")
469
489
  s3_client.delete_object(Bucket=self.bucket_name, Key=obj['Key'])
470
490
  print(f"Deleted {obj['Key']}")
491
+ else:
492
+ logger.info("No objects found in the bucket.")
493
+
494
+ print("All files deleted successfully.")
471
495
  except Exception as e:
472
496
  logger.error(f"Error deleting files: {str(e)}")
473
497
  raise Exception(f"Error deleting files: {str(e)}")
@@ -619,13 +643,14 @@ class S3Client:
619
643
  except Exception as e:
620
644
  logger.error(f"Error downloading file: {str(e)}")
621
645
  raise Exception(f"Error downloading file: {str(e)}")
622
-
646
+
623
647
  def list_files(self, *args: Any, **kwargs : Any) -> list[str]:
624
648
  """
625
649
  List all files in the S3 bucket.
626
650
 
627
651
  Args:
628
652
  filter (str | None): Optional filter to list specific files. If None, all files will be listed.
653
+ prefix (str | None): Optional prefix to filter files by their key. If None, all files will be listed.
629
654
  Raises:
630
655
  Exception: If there is an error listing the files.
631
656
  Returns:
@@ -639,9 +664,14 @@ class S3Client:
639
664
  if prefix is None:
640
665
  raise Exception("Prefix is None")
641
666
 
667
+ if prefix is not None and not prefix.endswith('/'):
668
+ prefix += '/'
669
+
642
670
  filter = kwargs.get("filter", None)
643
671
 
644
672
  s3_client = self._get_s3_client()
673
+
674
+ logger.info(f"Listing objects with prefix: {prefix}")
645
675
  objects = s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=prefix)
646
676
 
647
677
  # Check if the bucket contains any objects
@@ -649,7 +679,6 @@ class S3Client:
649
679
  if 'Contents' in objects:
650
680
  for obj in objects['Contents']:
651
681
  if obj['Key']:
652
- # Log the object key
653
682
  if filter is not None:
654
683
  if filter in obj['Key']:
655
684
  logger.info(f"Object: {obj['Key']}")
@@ -661,12 +690,14 @@ class S3Client:
661
690
  logger.error(f"Error listing files: {str(e)}")
662
691
  raise Exception(f"Error listing files: {str(e)}")
663
692
 
664
- def delete_file(self, *args : Any) -> None:
693
+ def delete_file(self, *args : Any, **kwargs) -> None:
665
694
  """
666
695
  Delete a file from the S3 bucket.
667
696
 
668
697
  Args:
669
698
  object_name (str): The name of the S3 object to delete.
699
+ prefix (str | None): Optional prefix to filter files by their key. If None, all files will be deleted.
700
+ filter (str | None): Optional filter to delete specific files. If None, the specified
670
701
  Raises:
671
702
  Exception: If there is an error deleting the file.
672
703
  """
@@ -675,8 +706,35 @@ class S3Client:
675
706
  if object_name is None:
676
707
  raise Exception("Object name is None")
677
708
 
709
+ prefix = kwargs.get("prefix", None)
710
+ if prefix is not None and not prefix.endswith('/'):
711
+ prefix += '/'
712
+
713
+ filter = kwargs.get("filter", None)
714
+
678
715
  s3_client = self._get_s3_client()
679
- s3_client.delete_object(Bucket=self.bucket_name, Key=object_name)
716
+
717
+ objects = None
718
+ if prefix is None:
719
+ # List all objects in the bucket
720
+ objects = s3_client.list_objects_v2(Bucket=self.bucket_name)
721
+ else:
722
+ # List objects with the specified prefix
723
+ logger.info(f"Listing objects with prefix: {prefix}")
724
+ objects = s3_client.list_objects_v2(Bucket=self.bucket_name, Prefix=prefix)
725
+
726
+ # Check if the bucket contains any objects
727
+ if 'Contents' in objects:
728
+ for obj in objects['Contents']:
729
+ if filter is not None:
730
+ if filter in obj['Key']:
731
+ # Delete each object
732
+ s3_client.delete_object(Bucket=self.bucket_name, Key=obj['Key'])
733
+ print(f"Deleted {obj['Key']}")
734
+ else:
735
+ # If no objects match the filter, delete the specified object
736
+ s3_client.delete_object(Bucket=self.bucket_name, Key=object_name)
737
+ print(f"Deleted {object_name}")
680
738
  except Exception as e:
681
739
  logger.error(f"Error deleting file: {str(e)}")
682
740
  raise Exception(f"Error deleting file: {str(e)}")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyaws-s3
3
- Version: 1.0.27
3
+ Version: 1.0.28
4
4
  Summary: A Python package for AWS S3 utilities
5
5
  Author-email: Giuseppe Zileni <giuseppe.zileni@gmail.com>
6
6
  Keywords: aws,s3,utilities
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pyaws-s3"
7
- version = "1.0.27"
7
+ version = "1.0.28"
8
8
  description = "A Python package for AWS S3 utilities"
9
9
  authors = [
10
10
  { name="Giuseppe Zileni", email="giuseppe.zileni@gmail.com" }
File without changes
File without changes
File without changes