通过 Python3 & Boto3 将 TTL 添加到 DynamoDB 记录

Adding TTL to DynamoDB records via Python3 & Boto3

我有以下 Pyhton3/Boto3 脚本连接到 AWS DynamoDB table 并尝试通过循环遍历所有记录来设置 19 天的 TTL:

#!/usr/bin/env python3
import boto3
import sys
from datetime import datetime, timedelta
from boto3.dynamodb.conditions import Key, Attr

client = boto3.resource('dynamodb')

ttl = 19    # The TTL in number of DAYS

myTable = client.Table('MyTable')

pe = "logId, id, created"
delCount = 0

try:
    response = integrationLogTable.scan(
        ProjectionExpression=pe,
        )
    while 'LastEvaluatedKey' in response:
        response = integrationLogTable.scan(
            ProjectionExpression=pe,
            ExclusiveStartKey=response['LastEvaluatedKey']
            )

        for i in response['Items']:
            # ???

except ClientError as e:
    print(e.response['Error']['Message'])

我正在为如何将 19 天 TTL 实际添加到我的所有记录而苦恼...有什么想法吗?

这是将 TTL 添加到 DynamoDBTable 的过程,

  1. 将 TTL 添加到 dynamodb table
  2. 将属性添加到每条记录并保存。

属性格式:

在几秒钟内添加 TTL。

For 19 days, it is 192460*60 => 19 days / 24 hours/ 60 minutes/ 60 seconds.

1641600 秒

import time
import sys

if sys.version_info < (3, 0):
    object.ttlattribute = 1641600 + long(time.time())
else:
    # In py3, long is renamed to int
    object.ttlattribute = 1641600 + int(time.time())

希望对您有所帮助。查看 this link 如何使用 boto 在 DynamoDB 上执行所有操作。