|
1 |
| -"""Amazon S3 CopDeletey Module (PRIVATE).""" |
| 1 | +"""Amazon S3 Delete Module (PRIVATE).""" |
2 | 2 |
|
3 |
| -import concurrent.futures |
4 | 3 | import datetime
|
5 | 4 | import itertools
|
6 | 5 | import logging
|
7 |
| -import time |
8 | 6 | from typing import Any, Dict, List, Optional, Union
|
9 |
| -from urllib.parse import unquote_plus as _unquote_plus |
10 | 7 |
|
11 | 8 | import boto3
|
12 | 9 |
|
13 |
| -from awswrangler import _utils, exceptions |
| 10 | +from awswrangler import _utils |
| 11 | +from awswrangler._threading import _get_executor |
| 12 | +from awswrangler.distributed import ray_get, ray_remote |
14 | 13 | from awswrangler.s3._fs import get_botocore_valid_kwargs
|
15 | 14 | from awswrangler.s3._list import _path2list
|
16 | 15 |
|
|
20 | 19 | def _split_paths_by_bucket(paths: List[str]) -> Dict[str, List[str]]:
|
21 | 20 | buckets: Dict[str, List[str]] = {}
|
22 | 21 | bucket: str
|
23 |
| - key: str |
24 | 22 | for path in paths:
|
25 |
| - bucket, key = _utils.parse_path(path=path) |
| 23 | + bucket = _utils.parse_path(path=path)[0] |
26 | 24 | if bucket not in buckets:
|
27 | 25 | buckets[bucket] = []
|
28 |
| - buckets[bucket].append(key) |
| 26 | + buckets[bucket].append(path) |
29 | 27 | return buckets
|
30 | 28 |
|
31 | 29 |
|
| 30 | +@ray_remote |
32 | 31 | def _delete_objects(
|
33 |
| - bucket: str, |
34 |
| - keys: List[str], |
35 |
| - boto3_session: boto3.Session, |
| 32 | + boto3_session: Optional[boto3.Session], |
| 33 | + paths: List[str], |
36 | 34 | s3_additional_kwargs: Optional[Dict[str, Any]],
|
37 |
| - attempt: int = 1, |
38 | 35 | ) -> None:
|
39 |
| - client_s3: boto3.client = _utils.client(service_name="s3", session=boto3_session) |
40 |
| - _logger.debug("len(keys): %s", len(keys)) |
41 |
| - batch: List[Dict[str, str]] = [{"Key": key} for key in keys] |
| 36 | + client_s3: boto3.client = _utils.client( |
| 37 | + service_name="s3", |
| 38 | + session=boto3_session, |
| 39 | + ) |
| 40 | + _logger.debug("len(paths): %s", len(paths)) |
42 | 41 | if s3_additional_kwargs:
|
43 | 42 | extra_kwargs: Dict[str, Any] = get_botocore_valid_kwargs(
|
44 | 43 | function_name="list_objects_v2", s3_additional_kwargs=s3_additional_kwargs
|
45 | 44 | )
|
46 | 45 | else:
|
47 | 46 | extra_kwargs = {}
|
| 47 | + bucket = _utils.parse_path(path=paths[0])[0] |
| 48 | + batch: List[Dict[str, str]] = [{"Key": _utils.parse_path(path)[1]} for path in paths] |
48 | 49 | res = client_s3.delete_objects(Bucket=bucket, Delete={"Objects": batch}, **extra_kwargs)
|
49 | 50 | deleted: List[Dict[str, Any]] = res.get("Deleted", [])
|
50 | 51 | for obj in deleted:
|
51 | 52 | _logger.debug("s3://%s/%s has been deleted.", bucket, obj.get("Key"))
|
52 |
| - errors: List[Dict[str, Any]] = res.get("Errors", []) |
53 |
| - internal_errors: List[str] = [] |
54 |
| - for error in errors: |
55 |
| - _logger.debug("error: %s", error) |
56 |
| - if "Code" not in error or error["Code"] != "InternalError": |
57 |
| - raise exceptions.ServiceApiError(errors) |
58 |
| - internal_errors.append(_unquote_plus(error["Key"])) |
59 |
| - if len(internal_errors) > 0: |
60 |
| - if attempt > 5: # Maximum of 5 attempts (Total of 15 seconds) |
61 |
| - raise exceptions.ServiceApiError(errors) |
62 |
| - time.sleep(attempt) # Incremental delay (linear) |
63 |
| - _delete_objects( |
64 |
| - bucket=bucket, |
65 |
| - keys=internal_errors, |
66 |
| - boto3_session=boto3_session, |
67 |
| - s3_additional_kwargs=s3_additional_kwargs, |
68 |
| - attempt=(attempt + 1), |
69 |
| - ) |
70 |
| - |
71 |
| - |
72 |
| -def _delete_objects_concurrent( |
73 |
| - bucket: str, |
74 |
| - keys: List[str], |
75 |
| - s3_additional_kwargs: Optional[Dict[str, Any]], |
76 |
| - boto3_primitives: _utils.Boto3PrimitivesType, |
77 |
| -) -> None: |
78 |
| - boto3_session = _utils.boto3_from_primitives(primitives=boto3_primitives) |
79 |
| - return _delete_objects( |
80 |
| - bucket=bucket, keys=keys, boto3_session=boto3_session, s3_additional_kwargs=s3_additional_kwargs |
81 |
| - ) |
82 | 53 |
|
83 | 54 |
|
84 | 55 | def delete_objects(
|
@@ -146,29 +117,18 @@ def delete_objects(
|
146 | 117 | last_modified_end=last_modified_end,
|
147 | 118 | s3_additional_kwargs=s3_additional_kwargs,
|
148 | 119 | )
|
149 |
| - if len(paths) < 1: |
150 |
| - return |
151 |
| - buckets: Dict[str, List[str]] = _split_paths_by_bucket(paths=paths) |
152 |
| - for bucket, keys in buckets.items(): |
153 |
| - chunks: List[List[str]] = _utils.chunkify(lst=keys, max_length=1_000) |
154 |
| - if len(chunks) == 1: |
155 |
| - _delete_objects( |
156 |
| - bucket=bucket, keys=chunks[0], boto3_session=boto3_session, s3_additional_kwargs=s3_additional_kwargs |
157 |
| - ) |
158 |
| - elif use_threads is False: |
159 |
| - for chunk in chunks: |
160 |
| - _delete_objects( |
161 |
| - bucket=bucket, keys=chunk, boto3_session=boto3_session, s3_additional_kwargs=s3_additional_kwargs |
162 |
| - ) |
163 |
| - else: |
164 |
| - cpus: int = _utils.ensure_cpu_count(use_threads=use_threads) |
165 |
| - with concurrent.futures.ThreadPoolExecutor(max_workers=cpus) as executor: |
166 |
| - list( |
167 |
| - executor.map( |
168 |
| - _delete_objects_concurrent, |
169 |
| - itertools.repeat(bucket), |
170 |
| - chunks, |
171 |
| - itertools.repeat(s3_additional_kwargs), |
172 |
| - itertools.repeat(_utils.boto3_to_primitives(boto3_session=boto3_session)), |
173 |
| - ) |
174 |
| - ) |
| 120 | + paths_by_bucket: Dict[str, List[str]] = _split_paths_by_bucket(paths) |
| 121 | + |
| 122 | + chunks = [] |
| 123 | + for _, paths in paths_by_bucket.items(): |
| 124 | + chunks += _utils.chunkify(lst=paths, max_length=1_000) |
| 125 | + |
| 126 | + executor = _get_executor(use_threads=use_threads) |
| 127 | + ray_get( |
| 128 | + executor.map( |
| 129 | + _delete_objects, |
| 130 | + boto3_session, |
| 131 | + chunks, |
| 132 | + itertools.repeat(s3_additional_kwargs), |
| 133 | + ) |
| 134 | + ) |
0 commit comments