|
| 1 | +# Copyright (C) - 2023 - 2024 - Cosmo Tech |
| 2 | +# This document and all information contained herein is the exclusive property - |
| 3 | +# including all intellectual property rights pertaining thereto - of Cosmo Tech. |
| 4 | +# Any use, reproduction, translation, broadcasting, transmission, distribution, |
| 5 | +# etc., to any person is prohibited unless it has been previously and |
| 6 | +# specifically authorized by written means by Cosmo Tech. |
| 7 | + |
| 8 | +from typing import Optional |
| 9 | + |
| 10 | +import boto3 |
| 11 | + |
| 12 | +from cosmotech.coal.cli.utils.click import click |
| 13 | +from cosmotech.coal.cli.utils.decorators import web_help |
| 14 | +from cosmotech.coal.utils.logger import LOGGER |
| 15 | + |
| 16 | + |
| 17 | +@click.command() |
| 18 | +@click.option("--bucket-name", |
| 19 | + envvar="CSM_DATA_BUCKET_NAME", |
| 20 | + help="The bucket on S3 to delete", |
| 21 | + metavar="BUCKET", |
| 22 | + type=str, |
| 23 | + show_envvar=True, |
| 24 | + required=True) |
| 25 | +@click.option("--prefix-filter", |
| 26 | + "file_prefix", |
| 27 | + envvar="CSM_DATA_BUCKET_PREFIX", |
| 28 | + help="A prefix by which all deleted files should start in the bucket", |
| 29 | + metavar="PREFIX", |
| 30 | + type=str, |
| 31 | + show_envvar=True) |
| 32 | +@click.option("--use-ssl/--no-ssl", |
| 33 | + default=True, |
| 34 | + help="Use SSL to secure connection to S3", |
| 35 | + type=bool, |
| 36 | + is_flag=True) |
| 37 | +@click.option("--s3-url", |
| 38 | + "endpoint_url", |
| 39 | + help="URL to connect to the S3 system", |
| 40 | + type=str, |
| 41 | + required=True, |
| 42 | + show_envvar=True, |
| 43 | + metavar="URL", |
| 44 | + envvar="AWS_ENDPOINT_URL") |
| 45 | +@click.option("--access-id", |
| 46 | + "access_id", |
| 47 | + help="Identity used to connect to the S3 system", |
| 48 | + type=str, |
| 49 | + required=True, |
| 50 | + show_envvar=True, |
| 51 | + metavar="ID", |
| 52 | + envvar="AWS_ACCESS_KEY_ID") |
| 53 | +@click.option("--secret-key", |
| 54 | + "secret_key", |
| 55 | + help="Secret tied to the ID used to connect to the S3 system", |
| 56 | + type=str, |
| 57 | + required=True, |
| 58 | + show_envvar=True, |
| 59 | + metavar="ID", |
| 60 | + envvar="AWS_SECRET_ACCESS_KEY") |
| 61 | +@click.option("--ssl-cert-bundle", |
| 62 | + help="Path to an alternate CA Bundle to validate SSL connections", |
| 63 | + type=str, |
| 64 | + show_envvar=True, |
| 65 | + metavar="PATH", |
| 66 | + envvar="CSM_S3_CA_BUNDLE") |
| 67 | +@web_help("csm-data/s3-bucket-delete") |
| 68 | +def s3_bucket_delete( |
| 69 | + bucket_name: str, |
| 70 | + file_prefix: str, |
| 71 | + endpoint_url: str, |
| 72 | + access_id: str, |
| 73 | + secret_key: str, |
| 74 | + use_ssl: bool = True, |
| 75 | + ssl_cert_bundle: Optional[str] = None, |
| 76 | +): |
| 77 | + """Delete S3 bucket content to a given folder |
| 78 | +
|
| 79 | +Will delete everything in the bucket unless a prefix is set, then only file following the given prefix will be deleted |
| 80 | +
|
| 81 | +Make use of the boto3 library to access the bucket |
| 82 | +
|
| 83 | +More information is available on this page: |
| 84 | +[https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html) |
| 85 | +""" |
| 86 | + boto3_parameters = { |
| 87 | + "use_ssl": use_ssl, |
| 88 | + "endpoint_url": endpoint_url, |
| 89 | + "aws_access_key_id": access_id, |
| 90 | + "aws_secret_access_key": secret_key, |
| 91 | + } |
| 92 | + if ssl_cert_bundle: |
| 93 | + boto3_parameters["verify"] = ssl_cert_bundle |
| 94 | + |
| 95 | + s3_resource = boto3.resource("s3", |
| 96 | + **boto3_parameters) |
| 97 | + |
| 98 | + bucket = s3_resource.Bucket(bucket_name) |
| 99 | + |
| 100 | + remove_prefix = False |
| 101 | + if file_prefix: |
| 102 | + bucket_files = bucket.objects.filter(Prefix=file_prefix) |
| 103 | + if file_prefix.endswith("/"): |
| 104 | + remove_prefix = True |
| 105 | + else: |
| 106 | + bucket_files = bucket.objects.all() |
| 107 | + for _file in bucket_files: |
| 108 | + if not (path_name := str(_file.key)).endswith("/"): |
| 109 | + target_file = path_name |
| 110 | + if remove_prefix: |
| 111 | + target_file = target_file.removeprefix(file_prefix) |
| 112 | + LOGGER.info(f"Deleting {path_name}") |
| 113 | + bucket.delete_key(_file.key) |
0 commit comments