aws-s3-使用boto3根据路径和间隔时间进行清理删除

使用python3.6以上

import boto3
import time
import datetime


# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html

s3 = boto3.resource('s3', region_name='cn-xxxxxxx-1')


def delete_file_from_bucket_one(bucket_name, prefix, reserve_paths,time_interval):
    bucket = s3.Bucket(bucket_name)
    if prefix:
        print(f"获取前缀为{prefix}的文件")
        objects = bucket.objects.filter(Prefix=prefix)
    else:
        print("没有前缀筛选,默认获取全部文件")
        objects = bucket.objects.all()
    prefixs=str(prefix).replace("/","_")
    times=time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
    filename=f"{bucket_name}_{prefixs}_{times}"
    with open(f'../../{filename}.txt', 'w') as f:
        for object_one in objects:
            filekey = object_one.key
            if "/" in filekey:
                filenames = str(filekey).split("/")
                filename = filenames[len(filenames) - 1]
                filepath = str(filekey).replace(filename, "")
 

你可能感兴趣的:(python,云存储云计算)