我使用 boto3 中的 describe_snapshots 函数获得以下输出
u'StartTime': datetime.datetime(2017, 4, 7, 4, 21, 42, tzinfo=tzutc())
我希望将其转换为正确的日期,以便我可以继续对快照进行排序并删除超过特定天数的快照。
是否有可用于实现此目的的 python 功能?
最佳答案
这几乎肯定已经是您需要的格式。日期时间对象很容易比较/排序。例如:
from datetime import datetime
import boto3
ec2 = boto3.client('ec2')
account_id = 'MY_ACCOUNT_ID'
response = ec2.describe_snapshots(OwnerIds=[account_id])
snapshots = response['Snapshots']
# January 1st, 2017
target_date = datetime(2017, 01, 01)
# Get the snapshots older than the target date
old_snapshots = [s for s in snapshots if s['StartTime'] < target_date]
# Sort the old snapshots
old_snapshots = sorted(old_snapshots, key=lambda s: s['StartTime'])
文档:https://docs.python.org/3.6/library/datetime.html
关于python - 将 boto3 输出转换为方便的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43875841/