使用python脚本批量删除阿里云oss中的mp4文件

复制代码
#encoding:utf-8

  '''
  oss中有一些mp4文件需要删除,首先定位出这些文件放在txt文本中
  然后通过python操作oss进行批量删除
  '''

import oss2

auth = oss2.Auth('key01', 'key02')

# Bucket处于新加坡区域
endpoint = 'http://oss-cn-beijing.aliyuncs.com' 
bucket1 = 'ck'

# service = oss2.Service(auth, endpoint, )

bucket = oss2.Bucket(auth, endpoint, bucket1)
f_handle = open('ck.txt','r')

# 循环找出mp4文件
while True:
    # 注意一定要strip()去掉换行,文件默认是\n换行,这样无法做判断
    video_file = f_handle.readline().strip()
    # print type(video_file)

    # 文件结束后跳出循环
    if video_file == '':
        break
    # 判断是否是mp4文件
    if video_file.endswith('.mp4'):
        exist = bucket.object_exists(video_file)

        if exist:
            print('%s object exist' % video_file)
            bucket.delete_object(video_file)
        else:
            print('object not eixst')
        
复制代码

读取的文件列表

posted @   reblue520  阅读(518)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示