前言

  • 技术栈
Python 3.11.8
boto3  1.35.13
  • 对于 AWS S3 在 web 控制台点击显示版本才能展示的过期目录,直接在 web 控制台或者用普通的 aws cli 命令是删除不掉的
  • AWS 中国区工程师为 qbit 提供了以下删除 AWS S3 过期目录的脚步

代码

  • 脚本内容
import time
import boto3

def delete_s3_versions(bucket_name, prefix):
    print(f"Start to delete of object in {bucket_name}/{prefix} ...")
    startTime = time.time()

    s3 = boto3.client('s3')
    total = 0

    # 获取桶中的所有对象的版本(包括删除标记)
    paginator = s3.get_paginator('list_object_versions')
    # 迭代器 botocore.paginate.PageIterator
    pages = paginator.paginate(Bucket=bucket_name, Prefix=prefix)

    delete_objects = {'Objects': []}

    for page in pages:
        # 处理对象版本
        if 'Versions' in page:
            for version in page['Versions']:
                delete_objects['Objects'].append({
                    'Key': version['Key'],
                    'VersionId': version['VersionId']
                })
        # 处理删除标记
        if 'DeleteMarkers' in page:
            for marker in page['DeleteMarkers']:
                delete_objects['Objects'].append({
                    'Key': marker['Key'],
                    'VersionId': marker['VersionId']
                })
        # 批量删除,最多1000个对象
        if len(delete_objects['Objects']) > 0:            
            s3.delete_objects(Bucket=bucket_name, Delete=delete_objects)
            total += len(delete_objects['Objects'])
            print(f"deleted count: {total}, time cost: {(time.time() - startTime):.2f}s")
            delete_objects = {'Objects': []}

    print(f"Completed deletion all objects in {bucket_name}/{prefix}")
    print(f"deleted total: {total}, time total: {(time.time() - startTime):.2f}s")


if __name__ == "__main__":
    bucket_name = "your-s3-bucket-name"  # 替换为你的 S3 桶名称
    prefix = "your-directory-prefix/"    # 替换为你要清空的目录路径  不带/ 就是删除文件

    delete_s3_versions(bucket_name, prefix)
本文出自 qbit snap

qbit
268 声望279 粉丝