如何使用boto3获取所有快照? 以下是使用获取卷ID为已知快照的方式的代码
import boto3
ec2 = boto3.resource('ec2')
volume_id = "something"
list_of_snapshots = []
vol = ec2.Volume(id=volume_id)
snapshots = vol.snapshots.all()
try:
for snapshot in snapshots:
if snapshot:
list_of_snapshots.append(snapshot.id)
return list_of_snapshots[0]
except:
return None
答案 0 :(得分:0)
我是通过以下方式遇到的,
from boto3 import client,ec2,Session
# print(help(Session))
s=Session(
# 'ec2-instance-connect',
aws_access_key_id='youraccess_key_id',
aws_secret_access_key='your secret access key',
region_name='ap-south-1'
)
c=s.client('ec2')
Snapshots=c.describe_snapshots()
print(c.describe_snapshots()['Snapshots'])
#上面的打印是字典中所有快照的列表,其中包含键,例如snapshotid,volumesize等 打开('c:\ Users \ Murugaraju \ Desktop \ snapshots.txt','w')为f: f.write(str(快照))