获得Dict回应 - Boto3

时间:2017-06-20 22:28:52

标签: amazon-web-services amazon-ec2 boto3

我正在尝试获取以下输出的SnapshotId但没有成功。我可以获得AMI描述的值和AMI_ID的值。

{
    'Images': [
        {
            'Architecture': 'i386'|'x86_64',
            'CreationDate': 'string',
            'ImageId': 'string',
            'ImageLocation': 'string',
            'ImageType': 'machine'|'kernel'|'ramdisk',
            'Public': True|False,
            'KernelId': 'string',
            'OwnerId': 'string',
            'Platform': 'Windows',
            'ProductCodes': [
                {
                    'ProductCodeId': 'string',
                    'ProductCodeType': 'devpay'|'marketplace'
                },
            ],
            'RamdiskId': 'string',
            'State': 'pending'|'available'|'invalid'|'deregistered'|'transient'|'failed'|'error',
            'BlockDeviceMappings': [
                {
                    'DeviceName': 'string',
                    'VirtualName': 'string',
                    'Ebs': {
                        'Encrypted': True|False,
                        'DeleteOnTermination': True|False,
                        'Iops': 123,
                        'SnapshotId': 'string',
                        'VolumeSize': 123,
                        'VolumeType': 'standard'|'io1'|'gp2'|'sc1'|'st1'
                    },
                    'NoDevice': 'string'
                },
            ],
            'Description': 'string',
            'EnaSupport': True|False,
            'Hypervisor': 'ovm'|'xen',
            'ImageOwnerAlias': 'string',
            'Name': 'string',
            'RootDeviceName': 'string',
            'RootDeviceType': 'ebs'|'instance-store',
            'SriovNetSupport': 'string',
            'StateReason': {
                'Code': 'string',
                'Message': 'string'
            },
            'Tags': [
                {
                    'Key': 'string',
                    'Value': 'string'
                },
            ],
            'VirtualizationType': 'hvm'|'paravirtual'
        },
    ]
}

使用以下代码:

import boto3

client = boto3.client('ec2', region_name='us-east-1')

def verifica_imagem(imagem):
    amiresponse = client.describe_images(
        Filters=[
            {
                'Name': 'description',
                'Values': [
                    imagem,
                ]
            },
        ],
        DryRun=False
    )

    try:
        data = str(amiresponse['Images'][0]['Description'])
        ami_id = str(amiresponse['Images'][0]['ImageId'])
        snapshot_id = str(amiresponse['Images'][0]['SnapshotId'])
    except:
        print "AMI not exists! Exiting...."
        return 1

verifica_imagem('IMAGE_XXXXXXX')

我无法理解如何使用SnapshotId的密钥。我试过了:

snapshot_id = str(amiresponse['Images']['BlockDeviceMappings']['Ebs'][0]['SnapshotId'])但是也不行。

1 个答案:

答案 0 :(得分:1)

ImagesBlockDeviceMappings的值为arrayEbsdict
使用此方法获取SnapshotId

的值
snapshot_id = amiresponse['Images'][0]['BlockDeviceMappings'][0]['Ebs']['SnapshotId']