「Python3」删除S3上过期的zip安装包

「Python3」删除S3上过期的zip安装包

五月 23, 2019
  1. 此脚本的作用是清除S3上过期的zip包
  2. 修改credentials.ini中秘钥bucket信息,修改下面local支持不同地区
  3. 执行方式,打开下面注释
  4. 注: 此脚本支持的目录结构是二级目录例如Bucket/gamexback/

credentials.ini配置文件

1
2
3
4
5
6
7
[aws_dev]
aws_access_key_id=XX
aws_secret_access_key=XX
aws_region=XX
bucket =itbackups
dir =gamexback
date =2018

删除安装包脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import boto3
import configparser

cfg = configparser.ConfigParser()
cfg.read('credentials.ini')
local = 'aws_dev'
buckets = cfg.get(local,'bucket')
dir = cfg.get(local,'dir')
date = cfg.get(local,'date')
session = boto3.session.Session(aws_access_key_id=cfg.get(local, 'aws_access_key_id'),
aws_secret_access_key=cfg.get(local, 'aws_secret_access_key'),
region_name=cfg.get(local, 'aws_region'))
client = session.client('s3')
s3 = session.resource('s3')
bucket = s3.Bucket(buckets)

objects_list = []
for obj in bucket.objects.filter(Prefix=dir):
objects_list.append({'Key':obj.key})

delete_list = []
for packages in objects_list:
if packages['Key'].__contains__(date):
delete_list.append(packages['Key'])

for i in delete_list:
print(i)
#执行删除命令
# bucket.delete_objects(Delete={'Objects': [{'Key': i,},],})