python - 将 boto3 输出转换为方便的格式

标签 python datetime boto3

我使用 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/

相关文章:

python - 当用户输入字符串而不是整数时如何保护我的 python 代码?

python - 没有 Math 模块的 Python 3 中的 Ceil 和 floor 等效?

python - keras 中的自定义二进制交叉熵损失忽略没有非零值的列

sql - 如何从今天获得 30 天

python - 合并所有具有不同表的 SQLite 数据库

unix - 在unix shell中,如何将昨天的日期放入变量中?

nhibernate - NHibernate 中的 DateTime 精度和 NHibernate SchemeExport 中对 DateTime2 的支持

python - 获取 AWS 中实例的公共(public) ip

amazon-s3 - 使用 aws key boto3 S3 上传

python - 如何将内存中的 zip 文件上传到 S3 存储桶