Python boto列出卷名后跟“Volumes:”

时间:2014-01-21 11:06:02

标签: python amazon-web-services amazon-ec2 boto

我是python的新手,试图编写一个脚本来获取每日亚马逊ebs快照。下面是列出卷的脚本,并在for循环中将它们输入到snapshot命令。


#!/usr/bin/python
#Script for purging AWS Ebs Snapshots.

from boto.ec2 import EC2Connection
import time

My_access_key = "xxxxxxxxxxxxxxx"
My_secret_key = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"


conn = EC2Connection(My_access_key, My_secret_key)

# List out the volumes
vol_id = conn.get_all_volumes(volume_ids=None, filters=None)
print vol_id

for i in vol_id:
snapshot = conn.create_snapshot(i, 'Daily-Snapshot')
print "Creating Snapshot:", snapshot

问题是,当我列出这个列表的卷时“[卷:vol-a50057e8,卷:vol-ba693ef7]”

和create snapshot命令只将此作为有效输入“vol-a50057e8”。我试着修剪,但那不行。

谢谢, Swaroop。

2 个答案:

答案 0 :(得分:2)

volumes = conn.get_all_volumes(volume_ids=None, filters=None)
# what you get here is a list of volume objects (not just IDs of those)
for volume in volumes:
   # each volume object has a field "id" which contains what you need:
   snapshot = conn.create_snapshot(volume.id, "Daily-Snapshot")

答案 1 :(得分:0)

这只是Volume对象的“文本”表示 get_all_volumes返回Volume对象,因此您可以执行

for vol in conn.get_all_volumes(...):
   do_stuff(vol.id)

参考:http://docs.pythonboto.org/en/latest/ref/ec2.html#module-boto.ec2.volume