Skip to content

Commit 25f51e9

Browse files
authored
Merge pull request #47 from Cosmo-Tech/LAL/s3_bucket_delete
add s3_bucket_delete command
2 parents 2ffea74 + eafdc23 commit 25f51e9

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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)

cosmotech/coal/cli/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from cosmotech.coal.cli.commands.legacy.legacy import legacy
1313
from cosmotech.coal.cli.commands.s3_bucket_download import s3_bucket_download
1414
from cosmotech.coal.cli.commands.s3_bucket_upload import s3_bucket_upload
15+
from cosmotech.coal.cli.commands.s3_bucket_delete import s3_bucket_delete
1516
from cosmotech.coal.cli.commands.store.store import store
1617
from cosmotech.coal.cli.utils.click import click
1718
from cosmotech.coal.cli.utils.decorators import web_help
@@ -23,7 +24,7 @@ def print_version(ctx, param, value):
2324
return
2425
click.echo(f"Cosmo Tech Data Interface {__version__}")
2526
ctx.exit()
26-
27+
2728

2829
@click.group("csm-data")
2930
@click_log.simple_verbosity_option(LOGGER,
@@ -49,6 +50,7 @@ def main():
4950
main.add_command(store, "store")
5051
main.add_command(s3_bucket_download, "s3-bucket-download")
5152
main.add_command(s3_bucket_upload, "s3-bucket-upload")
53+
main.add_command(s3_bucket_delete, "s3-bucket-delete")
5254
main.add_command(adx_send_scenariodata, "adx-send-scenariodata")
5355

5456
if __name__ == "__main__":

docs/csm-data/s3-bucket-delete.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
hide:
3+
- toc
4+
description: "Command help: `csm-data s3-bucket-delete`"
5+
---
6+
# s3-bucket-delete
7+
8+
!!! info "Help command"
9+
```text
10+
--8<-- "generated/commands_help/csm-data/s3-bucket-delete.txt"
11+
```

0 commit comments

Comments
 (0)