boto3中的attributeerror'str'对象没有属性'tags' [英] attributeerror 'str' object has no attribute 'tags' in boto3

查看:121
本文介绍了boto3中的attributeerror'str'对象没有属性'tags'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我想通过python3中的标签过滤快照,如下所示:

Here I want to filter snapshots by the tags in python3 as below :

res = c.describe_snapshots(OwnerIds=['012345678900'],Filters=[{'Name': 'tag:Name', 'Value': ['nonprod*']}])

for s in res:
    If 'nonprod' in s.tags :
       if s.tags == 'nonprod':
          s.delete()
          print ("snapshotlist1: %s" % s.id)
    elif 'prod' in c.tags
        if s.tags == 'prod':
          print ("snapshotlist2: %s" % s.id)

在python3中获取错误是"attributeerror'str'对象没有属性'tags'"

getting error in python3 is "attributeerror 'str' object has no attribute 'tags'"

推荐答案

describe_snapshots returns output in the form of:

{
    'Snapshots': [
        {
            # others not shown
            'Tags': [
                {
                    'Key': 'string',
                    'Value': 'string'
                },
            ]
        },
    ],
    'NextToken': 'string'
}

因此,您应该在循环的开头:

Thus you should have in the beginning of the loop:

for s in res['Snapshots']:

还必须遍历所有标签,因为Tags是一个列表:

Also you have to iterate over all tags, as Tags is a list:

for s in res['Snapshots']:
    for tag in s['Tags']:
        if tag['Key'] == 'nonprod':
            print("snapshotlist1: %s" % s['SnapshotId'])
        elif tag['Key'] == 'prod':
            print("snapshotlist2: %s" % s['SnapshotId'])        

这篇关于boto3中的attributeerror'str'对象没有属性'tags'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆