You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
from Util . OssUtil import *
from Util . PgUtil import *
from Util . CommonUtil import *
# 表里有哪些文件
dic = [ ]
sql = " select * from t_hy_task_files "
results = execute_query ( sql )
for result in results :
dic . append ( result [ 2 ] . replace ( ' http://hzkc.oss-cn-beijing.aliyuncs.com ' , ' ' ) )
# 清理oss,即删除OSS上的垃圾文件
# 设置前缀和分页参数
prefix = ' Images '
max_keys = 20 # 每页显示的最大文件数量
auth = oss2 . Auth ( accessKeyId , accessKeySecret )
bucket = oss2 . Bucket ( auth , endpoint , bucketName )
# 如果你想要手动控制分页, 可以使用marker参数
marker = ' '
is_truncated = True
delCount = 0
# 遍历文件
while is_truncated :
try :
# 使用list_objects方法获取对象列表
result = oss2 . ObjectIterator ( bucket , prefix = prefix , marker = marker , max_keys = max_keys )
for obj in result :
if obj . key . count ( ' / ' ) < 3 :
continue
if ' / ' + obj . key not in dic :
# 删除文件
#bucket.delete_object(obj.key)
# 一定要慎用
print ( obj . key + " 已被删除。 " ) # 打印文件名
delCount = delCount + 1
# 更新marker为下一页的起始点
marker = result . next_marker
# 检查是否还有更多的文件
is_truncated = result . is_truncated
except oss2 . exceptions . OSSError as e :
# 处理异常情况
print ( e )
printf ( " 恭喜,所有垃圾文件已被删除,共删除 " + str ( delCount ) + " 个文件 " )
# http://hzkc.oss-cn-beijing.aliyuncs.com/Images/System/4/20240514164847051.png