Skip to content

Commit

Permalink
Create s3_delete_objects.py
Browse files Browse the repository at this point in the history
Script for cleaning the test bucket.
  • Loading branch information
albags committed Jul 16, 2024
1 parent 86908c8 commit d5c0003
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions s3_delete_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
This script cleans the test bucket.
"""

from concurrent.futures import ThreadPoolExecutor, as_completed
import json
from tqdm import tqdm
import boto3


# Load AWS credentials and S3 bucket name from config file
with open("credentials.json", encoding="utf-8") as config_file:
aws_credentials = json.load(config_file)

# Initialize S3 client
s3_client = boto3.resource(
"s3",
aws_access_key_id=aws_credentials["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=aws_credentials["AWS_SECRET_ACCESS_KEY"],
endpoint_url=aws_credentials["AWS_URL_ENDPOINT"],
region_name=aws_credentials["AWS_REGION"],
)


def delete_file_from_bucket(obj):
"""Delete object"""
obj.delete()
return True


BUCKET = s3_client.Bucket("test-upload")
PREFIX = ""

COUNT = 0

for i in BUCKET.objects.filter(Prefix=PREFIX):
COUNT = COUNT + 1

print(f"Total number of files {COUNT}\n")

# Create a ThreadPoolExecutor
with tqdm(desc="Deleting files", ncols=60, total=COUNT, unit="B", unit_scale=1) as pbar:
with ThreadPoolExecutor() as executor:
futures = [
executor.submit(delete_file_from_bucket, obj)
for obj in BUCKET.objects.filter(Prefix=PREFIX)
]
for future in as_completed(futures):
pbar.update(1)

0 comments on commit d5c0003

Please sign in to comment.