s3func 0.8.3__tar.gz → 0.8.4__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.
@@ -115,3 +115,4 @@ s3func/tests/s3_config.toml
115
115
  s3func/tests/auth_file.sqlite
116
116
 
117
117
  .ruff*
118
+ *.csv
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: s3func
3
- Version: 0.8.3
3
+ Version: 0.8.4
4
4
  Summary: Simple functions for working with S3
5
5
  Project-URL: Documentation, https://mullenkamp.github.io/s3func/
6
6
  Project-URL: Source, https://github.com/mullenkamp/s3func
@@ -6,4 +6,4 @@ from s3func.locking import S3Lock, B2Lock
6
6
  from s3func.http_url import HttpSession
7
7
  from s3func import s3, http_url, utils, b2
8
8
 
9
- __version__ = '0.8.3'
9
+ __version__ = '0.8.4'
@@ -595,18 +595,38 @@ class B2Session:
595
595
 
596
596
  return b2resp
597
597
 
598
- def delete_objects(self, keys: List[dict]):
598
+ def delete_objects(self, keys: Union[List[str], List[dict]], purge: bool = True):
599
599
  """
600
- keys must be a list of dictionaries. The dicts must have the keys named key and version_id derived from the list_object_versions method. The B2 API has no bulk deletion call, so the deletes will be performed serially.
600
+ Delete multiple objects from a B2 bucket.
601
+
602
+ Parameters
603
+ ----------
604
+ keys : list of str or list of dict
605
+ Either a list of key strings (e.g. ['foo.bin', 'bar.bin']) or a list of
606
+ dicts with 'key' and optionally 'version_id' fields.
607
+ purge : bool
608
+ If True (default), all versions of each object are deleted. If False,
609
+ version_id is required in each dict and only those specific versions
610
+ are deleted.
601
611
 
602
612
  Returns
603
613
  -------
604
614
  None
605
615
  """
616
+ # Normalize input: strings -> dicts
617
+ if keys and isinstance(keys[0], str):
618
+ keys = [{'key': k} for k in keys]
619
+
606
620
  with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
607
621
  futures = []
608
622
  for key_dict in keys:
609
- future = executor.submit(self.delete_object, key_dict['key'], key_dict['version_id'])
623
+ key_name = key_dict.get('key') or key_dict.get('Key')
624
+ version_id = key_dict.get('version_id') or key_dict.get('VersionId')
625
+ if purge and not version_id:
626
+ # delete_object with version_id=None lists and deletes all versions
627
+ future = executor.submit(self.delete_object, key_name, None)
628
+ else:
629
+ future = executor.submit(self.delete_object, key_name, version_id)
610
630
  futures.append(future)
611
631
  concurrent.futures.wait(futures)
612
632
 
@@ -352,14 +352,50 @@ class S3Session:
352
352
 
353
353
  return s3resp
354
354
 
355
- def delete_objects(self, keys: List[dict]):
355
+ def delete_objects(self, keys: Union[List[str], List[dict]], purge: bool = True):
356
356
  """
357
- keys must be a list of dictionaries. The dicts must have the keys named key and version_id derived from the list_object_versions method. This function will automatically separate the list into 1000 count list chunks (required by the delete_objects request).
357
+ Delete multiple objects from an S3 bucket.
358
+
359
+ Parameters
360
+ ----------
361
+ keys : list of str or list of dict
362
+ Either a list of key strings (e.g. ['foo.bin', 'bar.bin']) or a list of
363
+ dicts with 'key' and optionally 'version_id' fields.
364
+ purge : bool
365
+ If True (default), all versions of each object are deleted by listing
366
+ versions first. If False, objects are deleted without version IDs
367
+ (which only adds a delete marker on versioned buckets).
358
368
 
359
369
  Returns
360
370
  -------
361
371
  None
362
372
  """
373
+ # Normalize input: strings -> dicts
374
+ if keys and isinstance(keys[0], str):
375
+ keys = [{'key': k} for k in keys]
376
+
377
+ # If purge, resolve version IDs for keys that don't have them
378
+ if purge:
379
+ resolved = []
380
+ for k in keys:
381
+ key_name = k.get('key') or k.get('Key')
382
+ version_id = k.get('version_id') or k.get('VersionId')
383
+ if version_id:
384
+ resolved.append(k)
385
+ else:
386
+ try:
387
+ resp = self.list_object_versions(prefix=key_name)
388
+ found = False
389
+ for obj in resp.iter_objects():
390
+ if obj['key'] == key_name:
391
+ resolved.append({'key': obj['key'], 'version_id': obj['version_id']})
392
+ found = True
393
+ if not found:
394
+ resolved.append(k)
395
+ except Exception:
396
+ resolved.append(k)
397
+ keys = resolved
398
+
363
399
  # S3 Delete Objects requires a specific XML payload
364
400
  # <Delete><Object><Key>...</Key><VersionId>...</VersionId></Object>...</Delete>
365
401
  url = urllib.parse.urljoin(self._endpoint_url, f"{self.bucket}")
@@ -367,7 +403,6 @@ class S3Session:
367
403
  for keys_chunk in utils.chunks(keys, 1000):
368
404
  # Build XML
369
405
  root = ET.Element('Delete', xmlns="http://s3.amazonaws.com/doc/2006-03-01/")
370
- # Quiet mode? Original used Quiet=True
371
406
  quiet = ET.SubElement(root, 'Quiet')
372
407
  quiet.text = 'true'
373
408
 
@@ -387,16 +422,11 @@ class S3Session:
387
422
 
388
423
  body = ET.tostring(root, encoding='utf-8')
389
424
 
390
- # Subresource ?delete
391
425
  query_params = {'delete': ''}
392
426
 
393
- # Content-MD5 is often required for DeleteObjects for integrity, but let's try without first or add if needed.
394
- # SigV4 protects integrity too.
395
-
396
427
  md5 = base64.b64encode(hashlib.md5(body).digest()).decode('utf-8')
397
428
  headers = {'Content-MD5': md5, 'Content-Type': 'application/xml'}
398
429
 
399
- # Delete objects response is small, preload it
400
430
  self.request('POST', url, headers=headers, fields=query_params, body=body, preload_content=True)
401
431
 
402
432
  def copy_object(
@@ -100,6 +100,19 @@ def test_mock_s3_delete_objects(s3_session):
100
100
  assert not found
101
101
 
102
102
 
103
+ def test_mock_s3_delete_objects_string_keys(s3_session):
104
+ """delete_objects accepts a list of key strings."""
105
+ keys = [f"delete-str-{uuid.uuid4().hex}-{i}" for i in range(3)]
106
+ for key in keys:
107
+ s3_session.put_object(key, b"data")
108
+
109
+ s3_session.delete_objects(keys)
110
+
111
+ listed = {obj['key'] for obj in s3_session.list_objects().iter_objects()}
112
+ for key in keys:
113
+ assert key not in listed
114
+
115
+
103
116
  def test_mock_s3_copy_object(s3_session):
104
117
  src_key = "src-key"
105
118
  dest_key = "dest-key"
@@ -119,22 +119,8 @@ def test_s3_copy_object(s3_session, s3_test_key):
119
119
 
120
120
 
121
121
  def test_s3_delete_objects(s3_session, s3_test_key):
122
- """Delete test objects and verify removal."""
123
- import urllib3
124
-
125
- # Try versioned delete first, fall back to simple delete
126
- try:
127
- obj_keys = []
128
- for js in s3_session.list_object_versions().iter_objects():
129
- if js['key'] == s3_test_key:
130
- obj_keys.append({'key': js['key'], 'version_id': js['version_id']})
131
- if obj_keys:
132
- s3_session.delete_objects(obj_keys)
133
- except urllib3.exceptions.HTTPError as e:
134
- if '501' in str(e):
135
- s3_session.delete_object(s3_test_key)
136
- else:
137
- raise
122
+ """Delete test objects (with purge) and verify removal."""
123
+ s3_session.delete_objects([s3_test_key])
138
124
 
139
125
  found_key = False
140
126
  for js in s3_session.list_objects().iter_objects():
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes