本文介绍了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 返回以下形式的输出:
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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!