python自动删除minio过期文件

#!/usr/local/bin/python3
# -*- coding:utf-8 -*-

# ====================================================
# Author: changbo - 541330702@qq.com
# Last modified: 2022-01-07
# Filename: miniodeloldfile.py
# Description:  Del old minio file
# http://www.cnblogs.com/changbo
# ====================================================

import logging
from minio import Minio
import time, os
import datetime
import threading

logging.basicConfig(
    level=logging.INFO,
    filename='Del.log',
    filemode='a',
    format='%(asctime)s %(name)s %(levelname)s--%(message)s'
)

nowtime = time.strftime("%Y-%m-%d", time.localtime())
minioClient = Minio(
        'x.x.x.x:9000',
        access_key='xxx',
        secret_key='xxxx',
        secure=False
    )


# 确定要删除的文件
def del_bucket_files(bucketname):
    try:
        object = minioClient.list_objects(bucketname, prefix=None, recursive=True)
        for obj in object:
            minioFile = 'minio/' + obj.bucket_name + '/' + obj.object_name
            date1 = time.strptime(str(obj.last_modified).split(' ')[0], "%Y-%m-%d")
            date2 = time.strptime(nowtime, "%Y-%m-%d")
            date4 = datetime.datetime(date2[0], date2[1], date2[2])
            date3 = datetime.datetime(date1[0], date1[1], date1[2])
            if date3 != date4:
                if int(str(date4-date3).split(' ')[0]) >= 7:
                    print(str(obj.last_modified).split(' ')[0], minioFile)
                    os.system('/root/prometheus-2.26.0.linux-amd64/mc rm ' + minioFile)
    except Exception as e:
        print(e)


if __name__ == '__main__':
    thread1 = threading.Thread(target=del_bucket_files, args=("bucketname1",))
    thread1.start()
    thread2 = threading.Thread(target=del_bucket_files, args=("bucketname2",))
    thread2.start()
    thread3 = threading.Thread(target=del_bucket_files, args=("bucketname3",))
thread3.start()

 

上一篇:浅谈Java泛型中的extends和super关键字(转)


下一篇:SpringCloud微服务实战——搭建企业级开发框架(二十九):集成对象存储服务MinIO+七牛云+阿里云+腾讯云