attributeerror объект 'str' не имеет атрибута 'tags' в boto3 - PullRequest
1 голос
/ 07 августа 2020

Здесь я хочу отфильтровать снимки по тегам в python3, как показано ниже:

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 is «attributeerror 'str' объект не имеет атрибута 'tags'»

Ответы [ 2 ]

1 голос
/ 07 августа 2020

describe_snapshots возвращает результат в виде:

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

Таким образом, вы должны иметь в начале l oop:

for s in res['Snapshots']:

Также вам нужно перебрать все теги, так как Tags - это список:

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'])        
0 голосов
/ 12 августа 2020

Вот версия тоже работает:

Добавлен Value() метод сравнения значений в словаре:

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

        elif 'prod': in tag.values():
            print("snapshotlist2: %s" % s['SnapshotId'])   
...