Python boto 清除 Aws 旧 Ebs 快照 "TypeError: unsupported operand type(s) for -: ' unicode' 和 'datetime.timedelta' "

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

我正在尝试编写一个 python 脚本来删除 14 天前的 ebs 快照。问题是我无法显示两周前的日期并将其与当前日期进行比较。下面是我的代码

import boto
import dateutil.relativedelta
from boto.exception import EC2ResponseError
from datetime import timedelta, datetime

conn = boto.ec2.connect_to_region(
    'us-east-1',
    aws_access_key_id = 'xxxxxxxxxxxxx',
    aws_secret_access_key = 'yyyyyyyyyyyyyyyyyy',
 )

snaps = conn.get_all_snapshots()

for list in snaps:
    old_date = list.start_time - timedelta(days=14)
    if list.start_time (is less than or equal to) old_date:
       print conn.delete_snapshots(list.id)

错误:

回溯(最近一次调用最后一次): 文件“/home/swaroop/Documents/My_python/display_snapshots.py”,第 28 行,位于 old_date = list.start_time - timedelta(天=14) 类型错误:不支持的操作数类型 -:“unicode”和“datetime.timedelta”

注意:快照显示日期格式如下:2013-12-19T11:11:43.000Z

谢谢

斯瓦鲁普。

最佳答案

在您的代码中 list.start_time 返回开始日期时间作为 unicode 字符串。

您需要将其转换为 datetime.timedelta

喜欢

from dateutil import parser

for snap in snaps:
    limit = datetime.now() - timedelta(days=14)
    if parser.parse(snap.start_time).date() <= limit.date():
        #do your filter stuff here
        print conn.delete_snapshot(snap.id)

注意:首先您可以尝试使用print snap.id,如果它返回有效结果,那么您可以print conn.delete_snapshot(snap.id)

关于Python boto 清除 Aws 旧 Ebs 快照 "TypeError: unsupported operand type(s) for -: ' unicode' 和 'datetime.timedelta' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21331883/

相关文章:

wordpress - 尝试 scp 到 EC2 实例,仅显示 sftp?

ios - IPV6 崩溃 iOS,无法弄清楚发生了什么

python - Flask、Postman 和 Mysql 多字段插入问题

angular - 由 aws API 制作的 HttpRequest 拦截器

amazon-web-services - AWS : Why isn't my lambda function URL returning CORS headers for my preflight requests?

amazon-web-services - AWS CloudWatch Logs 自定义指标未显示在 GUI 中

python - 两个 Web 服务器(带 Django 的 Amazon EC2 和 Google App Engine)之间的安全通信

python - 如何使用 Python 根据动态条件分隔数据框的行

Python 3.3 CSV.Writer 写入额外的空白行

python - 单次迭代中的最小值、最大值和平均值