简体   繁体   English

无法使用 python3 使用 boto3 删除快照

[英]Unable to delete Snapshot using boto3 using python3

I using boto3 on the python3 for delete the snapshop, Getting below error while trying to remove it (This syntax was work in the python2+boto only):我在 python3 上使用boto3删除 snapshop,尝试删除它时出现以下错误(此语法仅适用于 python2+boto):

Tracebak (most recent call last):
  File "./snapshotcleanup.py"m line 158, in <module>
    s.delete()
 AttributeError: 'dict' object has no attribute 'delete'

Code :代码 :

connection = myinternalclient (User, pass)  
// Custom function for connection, you may consider ec2 = boto3.client('ec2') 



res = connection.describe_snapshots(OwnersIds=[XX], Filters=[{'Name' : 'tag:Name', 'Value' : ["nonimp*"]'}])

for s in res['Snapshots']:
    for tag in s['Tags']:
      if 'nonprod' in tag.value():
         s.delete()
          print("[Deleted Snapshot]: %s" % s['SnapshotId'])

Is this syntax not in the boto3 ?这个语法不在 boto3 中吗?

To delete the snapshot, you can use delete_snapshot method.要删除快照,您可以使用delete_snapshot方法。

For example:例如:

ec2 = boto3.client('ec2')

for s in res['Snapshots']:
    for tag in s['Tags']:
        if tag['Value'] == 'nonprod':
            ec2.delete_snapshot(SnapshotId=s['SnapshotId'])        
            print("[Deleted Snapshot]: %s" % s['SnapshotId'])           

Please double check the code as mistakes are possible, as one can delete wrong snapshots by accident.请仔细检查代码,因为可能会出错,因为您可能会意外删除错误的快照。

The above assumes that the tags have the form ( Key is not checked in the code above):以上假设标签具有以下形式(上面的代码中未检查Key ):

{
    'Key': 'env',
    'Value': 'nonprod'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM