简体   繁体   English

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

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

I have the following Pyhton3/Boto3 script that connects to a AWS DynamoDB table and attempts to set a 19-day TTL on all its records by looping through them: 我具有以下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'])

I'm struggling with how to actually add the 19-day TTL to all my records... any ideas? 我正在努力将19天的TTL实际添加到我的所有记录中...有什么想法吗?

Here is the process for adding TTL to a DynamoDBTable, 这是将TTL添加到DynamoDBTable的过程,

  1. Add TTL to the dynamodb table 将TTL添加到dynamodb表
  2. Add the attribute to each record and save it. 将属性添加到每个记录并保存。

Attribute format: 属性格式:

Add the TTL in seconds. 以秒为单位添加TTL。

For 19 days, it is 19*24*60*60 => 19 days / 24 hours/ 60 minutes/ 60 seconds. 19天为19 * 24 * 60 * 60 => 19天/ 24小时/ 60分钟/ 60秒。

1641600 seconds 1641600秒

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

Hope it helps. 希望能帮助到你。

EDIT1: 编辑1:

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

Check out the link on how to perform all operations on DynamoDB using boto. 查看有关如何使用boto在DynamoDB上执行所有操作的链接。

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

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