ALP*_*RGE 5 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'])
Run Code Online (Sandbox Code Playgroud)
文档: https: //docs.python.org/3.6/library/datetime.html
| 归档时间: |
|
| 查看次数: |
5229 次 |
| 最近记录: |