gpt4 book ai didi

python(boto3)程序删除aws中的旧快照

转载 作者:太空狗 更新时间:2023-10-30 02:25:42 29 4
gpt4 key购买 nike

我已经编写了一个程序来删除旧快照。但是现在我的问题是如果快照附加了 ami 那么它不会被删除并且程序也会停止。它显示以下消息:

botocore.exceptions.ClientError: An error occurred (InvalidSnapshot.InUse) when calling the DeleteSnapshot operation: The snapshot snap-12345678 is currently in use by ami-12345

我希望程序单独跳过那些快照并继续删除其他快照。下面是我的代码:

import boto3
import datetime
client = boto3.client('ec2',region_name='us-west-1')
snapshots = client.describe_snapshots(OwnerIds=['12345678'])
for snapshot in snapshots['Snapshots']:
a= snapshot['StartTime']
b=a.date()
c=datetime.datetime.now().date()
d=c-b
if d.days>10:
id = snapshot['SnapshotId']
client.delete_snapshot(SnapshotId=id)

最佳答案

我自己解决了。这是代码:

    import boto3
import datetime
client = boto3.client('ec2',region_name='us-west-1')
snapshots = client.describe_snapshots(OwnerIds=['12345678'])
for snapshot in snapshots['Snapshots']:
a= snapshot['StartTime']
b=a.date()
c=datetime.datetime.now().date()
d=c-b
try:
if d.days>10:
id = snapshot['SnapshotId']
client.delete_snapshot(SnapshotId=id)
except Exception,e:
if 'InvalidSnapshot.InUse' in e.message:
print "skipping this snapshot"
continue

关于python(boto3)程序删除aws中的旧快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48124269/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com