繁体   English   中英

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

[英]Adding TTL to DynamoDB records via Python3 & Boto3

我具有以下Pyhton3 / Boto3脚本,该脚本连接到AWS DynamoDB表,并尝试通过遍历它们的方式在其所有记录上设置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表
  2. 将属性添加到每个记录并保存。

属性格式:

以秒为单位添加TTL。

19天为19 * 24 * 60 * 60 => 19天/ 24小时/ 60分钟/ 60秒。

1641600秒

import time; 
object.ttlattribute = 1641600 + long(time.time())

希望能帮助到你。

编辑1:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html#GettingStarted.Python.03.03

查看有关如何使用boto在DynamoDB上执行所有操作的链接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM