Jea*_*lus 5 python amazon-ec2 boto amazon-web-services
我想列出附加到ec2的所有卷.
conn = EC2Connection()
attribute = get_instance_metadata()
region=attribute['local-hostname'].split('.')[1]
inst_id = attribute['instance-id']
aws = boto.ec2.connect_to_region(region)
volume=attribute['local-hostname']
volumes = str(aws.get_all_volumes(filters={'attachment.instance-id': inst_id}))
Run Code Online (Sandbox Code Playgroud)
使用我的代码我可以列出音量但结果是这样的:
[vol-35b0b5fa,卷:vol-6cbbbea3]
我需要这样的东西:
第一卷,35b0b5fa
第一卷,6cbbbea3
我不知道我该怎么做,我的研究对我没有帮助.
有人可以帮帮我吗?
小智 12
如果您使用的是Boto3库,则此处是列出所有连接卷的命令
import boto3
ec2 = boto3.resource('ec2', region_name='us-west-2')
volumes = ec2.volumes.all() # If you want to list out all volumes
volumes = ec2.volumes.filter(Filters=[{'Name': 'status', 'Values': ['in-use']}]) # if you want to list out only attached volumes
[volume for volume in volumes]
Run Code Online (Sandbox Code Playgroud)
boto 中的调用get_all_volumes
返回对象列表Volume
。如果您需要的只是卷的 ID,则可以使用对象id
的属性来获取Volume
:
import boto.ec2
ec2 = boto.ec2.connect_to_region(region_name)
volumes = ec2.get_all_volumes()
volume_ids = [v.id for v in volumes]
Run Code Online (Sandbox Code Playgroud)
该变量volume_ids
现在是一个字符串列表,其中每个字符串都是其中一卷的 ID。