OSS文件查询删除

 可以根据文件名包含的指定字符串来查询文件以 及删除文件

import tkinter as tk
from tkinter import messagebox
from oss2 import Auth, Bucket

def list_files_with_character():
    # 用户输入的字符
    target_char = entry.get()

    # 清空文本框
    text_box.delete(1.0, tk.END)

    # 配置你的Access Key和Secret Key
    access_key_id = '123'
    access_key_secret = '456'

    # 指定Bucket名称和Endpoint
    bucket_name = '789'
    endpoint = 'oss-cn-hangzhou.aliyuncs.com'

    # 创建Auth对象
    auth = Auth(access_key_id, access_key_secret)

    # 创建Bucket对象
    bucket = Bucket(auth, endpoint, bucket_name)

    # 列出包含特定字符的对象
    prefix = ''  # 如果你想从特定前缀开始搜索,可以设置这个值
    marker = ''  # 用于分页的标记
    max_keys = 500  # 每次请求返回的最大对象数量
    total_matched = 0  # 匹配到的文件总数

    while True:
        # 获取对象列表
        result = bucket.list_objects(prefix=prefix, marker=marker, max_keys=max_keys)

        # 遍历对象
        for obj in result.object_list:
            if target_char in obj.key:
                total_matched += 1
                text_box.insert(tk.END, f'匹配到 {total_matched}: {obj.key}\n')

        # 如果没有更多的对象,则退出循环
        if not result.is_truncated:
            break

        # 更新标记以获取下一批对象
        marker = result.next_marker

    # 显示完成消息
    messagebox.showinfo("完成", f"找到 {total_matched} 个匹配的文件。")

def delete_files_with_character():
    # 用户输入的字符
    target_char = entry.get()

    # 弹出确认对话框
    confirm = messagebox.askyesno("确认", "再次确认要删除这些文件吗?")
    if confirm:
        # 配置你的Access Key和Secret Key
        access_key_id = '123'
        access_key_secret = '456'

        # 指定Bucket名称和Endpoint
        bucket_name = '789'
        endpoint = 'oss-cn-hangzhou.aliyuncs.com'

        # 创建Auth对象
        auth = Auth(access_key_id, access_key_secret)

        # 创建Bucket对象
        bucket = Bucket(auth, endpoint, bucket_name)

        # 清空文本框
        text_box.delete(1.0, tk.END)

        # 列出并删除包含特定字符的对象
        prefix = ''  # 如果你想从特定前缀开始搜索,可以设置这个值
        marker = ''  # 用于分页的标记
        max_keys = 500  # 每次请求返回的最大对象数量
        total_deleted = 0  # 被删除的文件总数

        while True:
            # 获取对象列表
            result = bucket.list_objects(prefix=prefix, marker=marker, max_keys=max_keys)

            # 遍历对象
            for obj in result.object_list:
                if target_char in obj.key:
                    total_deleted += 1
                    text_box.insert(tk.END, f'正在删除 {total_deleted}: {obj.key}\n')
                    text_box.update()  # 更新文本框显示
                    bucket.delete_object(obj.key)

            # 如果没有更多的对象,则退出循环
            if not result.is_truncated:
                break

            # 更新标记以获取下一批对象
            marker = result.next_marker

        # 显示完成消息
        messagebox.showinfo("完成", f"已删除 {total_deleted} 个文件。")
        text_box.insert(tk.END, f'\n删除文件总数: {total_deleted}')

# 创建主窗口
root = tk.Tk()
root.title("OSS屏幕文件删除器SINGSONG专用")

# 创建标签和输入框
label = tk.Label(root, text="请输入文件名中包含的字符:")
label.pack()

entry = tk.Entry(root)
entry.pack()

# 创建文本框用于显示删除进度
text_scrollbar = tk.Scrollbar(root)
text_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_box = tk.Text(root, height=70, width=100, bg='#D3D3D3', fg='blue', yscrollcommand=text_scrollbar.set)
# 配置滚动条
text_scrollbar.config(command=text_box.yview)
text_box.pack()

# 在文本框中插入警告信息
text_box.delete(1.0, tk.END)  # 清空文本框
text_box.insert(tk.END, "警告:删除文件是一个不可逆的操作。\n")
text_box.insert(tk.END, "警告:上面的输入框为空则删除全部文件。\n")
text_box.insert(tk.END, "一旦删除,文件将无法恢复。\n")
text_box.tag_add("warning", "1.0", "end")  # 添加标签
text_box.tag_config("warning", foreground="red", font=("Helvetica", 15, "bold"))  # 配置标签样式

# 创建查询按钮
query_button_1 = tk.Button(root, text="查询文件", command=list_files_with_character, bg="Green", fg='white')
query_button_1.pack(side=tk.RIGHT, padx=100)

# 创建删除按钮
delete_button = tk.Button(root, text="删除文件", command=delete_files_with_character, bg="red", fg='white')
delete_button.pack(side=tk.LEFT, padx=100)

# 运行主循环
root.mainloop()

 

posted @ 2024-07-11 17:09  *感悟人生*  阅读(35)  评论(0编辑  收藏  举报