|
10 | 10 | # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
|
11 | 11 | # ANY KIND, either express or implied. See the License for the specific
|
12 | 12 | # language governing permissions and limitations under the License.
|
| 13 | + |
13 | 14 | from s3transfer.tasks import SubmissionTask, Task
|
14 | 15 |
|
15 | 16 |
|
@@ -69,3 +70,78 @@ def _main(self, client, bucket, key, extra_args):
|
69 | 70 |
|
70 | 71 | """
|
71 | 72 | client.delete_object(Bucket=bucket, Key=key, **extra_args)
|
| 73 | + |
| 74 | + |
| 75 | +class BatchDeleteSubmissionTask(SubmissionTask): |
| 76 | + """Task for submitting tasks to execute batch deletion of S3 objects""" |
| 77 | + |
| 78 | + def _submit(self, client, request_executor, transfer_future, **kwargs): |
| 79 | + """ |
| 80 | + :param client: The client associated with the transfer manager |
| 81 | +
|
| 82 | + :type config: s3transfer.manager.TransferConfig |
| 83 | + :param config: The transfer config associated with the transfer |
| 84 | + manager |
| 85 | +
|
| 86 | + :type osutil: s3transfer.utils.OSUtil |
| 87 | + :param osutil: The os utility associated to the transfer manager |
| 88 | +
|
| 89 | + :type request_executor: s3transfer.futures.BoundedExecutor |
| 90 | + :param request_executor: The request executor associated with the |
| 91 | + transfer manager |
| 92 | +
|
| 93 | + :type transfer_future: s3transfer.futures.TransferFuture |
| 94 | + :param transfer_future: The transfer future associated with the |
| 95 | + transfer request that tasks are being submitted for |
| 96 | + """ |
| 97 | + call_args = transfer_future.meta.call_args |
| 98 | + |
| 99 | + # Extract the objects to delete from the call_args |
| 100 | + bucket = call_args.bucket |
| 101 | + objects = call_args.objects |
| 102 | + extra_args = call_args.extra_args or {} |
| 103 | + |
| 104 | + self._transfer_coordinator.submit( |
| 105 | + request_executor, |
| 106 | + BatchDeleteObjectsTask( |
| 107 | + transfer_coordinator=self._transfer_coordinator, |
| 108 | + main_kwargs={ |
| 109 | + 'client': client, |
| 110 | + 'bucket': bucket, |
| 111 | + 'objects': objects, |
| 112 | + 'extra_args': extra_args, |
| 113 | + }, |
| 114 | + is_final=True, |
| 115 | + ), |
| 116 | + ) |
| 117 | + |
| 118 | + |
| 119 | +class BatchDeleteObjectsTask(Task): |
| 120 | + def _main(self, client, bucket, objects, extra_args): |
| 121 | + """ |
| 122 | + Delete multiple objects in a single API call |
| 123 | +
|
| 124 | + :param client: The S3 client to use when calling DeleteObjects |
| 125 | +
|
| 126 | + :type bucket: str |
| 127 | + :param bucket: The name of the bucket. |
| 128 | +
|
| 129 | + :type objects: list |
| 130 | + :param objects: The list of objects to delete. |
| 131 | +
|
| 132 | + :type extra_args: dict |
| 133 | + :param extra_args: Extra arguments to pass to the DeleteObjects call. |
| 134 | + """ |
| 135 | + # Create a copy of extra_args to avoid modifying the original |
| 136 | + extra_args_copy = extra_args.copy() |
| 137 | + |
| 138 | + # Create the request body |
| 139 | + delete_request = { |
| 140 | + 'Objects': objects, |
| 141 | + 'Quiet': False, |
| 142 | + } |
| 143 | + |
| 144 | + response = client.delete_objects( |
| 145 | + Bucket=bucket, Delete=delete_request, **extra_args_copy |
| 146 | + ) |
| 147 | + return response |
0 commit comments