-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflare_r2_delete.py
59 lines (46 loc) · 1.99 KB
/
cloudflare_r2_delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Cloudflare R2 Delete Script
This script provides functionality to delete files and directories from Cloudflare R2 storage.
It can be run as a standalone script or imported as a module in other Python scripts.
Usage:
As a script: python cloudflare_r2_delete.py <path>
As a module: from cloudflare_r2_delete import delete
"""
import argparse
from typing import List
from cloudflare_r2 import CloudflareR2
def delete(cloudflare_r2: CloudflareR2, path: str) -> None:
"""
Delete a file or folder within a Cloudflare R2 bucket.
Args:
cloudflare_r2 (CloudflareR2): The CloudflareR2 instance class
path (str): The path of the file or folder to delete (e.g. "path/to/file" or "path/to/folder")
"""
path_components: List[str] = path.split('/')
if path_components[-1] == '':
path_components.pop()
folder_path: str = '/'.join(path_components)
objects = cloudflare_r2.s3.list_objects(Bucket=cloudflare_r2.bucket_name, Prefix=folder_path)
for obj in objects.get('Contents', []):
print(f'Deleting {obj["Key"]}')
cloudflare_r2.s3.delete_object(Bucket=cloudflare_r2.bucket_name, Key=obj['Key'])
print(f'Deleting folder {folder_path}/')
cloudflare_r2.s3.delete_object(Bucket=cloudflare_r2.bucket_name, Key=folder_path + '/')
else:
print(f'Deleting file {path}')
cloudflare_r2.s3.delete_object(Bucket=cloudflare_r2.bucket_name, Key=path)
def main() -> None:
"""
Main function to handle command-line arguments and initiate the delete process.
"""
parser = argparse.ArgumentParser(description="Delete file or directory from Cloudflare R2")
parser.add_argument("path", help="Path to file or directory to delete")
args = parser.parse_args()
try:
cf = CloudflareR2()
delete(cf, args.path)
except ValueError as e:
print(f"Error: {str(e)}")
print("Please ensure all required environment variables are set.")
if __name__ == "__main__":
main()