简体   繁体   中英

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:

#!/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?

Here is the process for adding TTL to a DynamoDBTable,

  1. Add TTL to the dynamodb table
  2. Add the attribute to each record and save it.

Attribute format:

Add the TTL in seconds.

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

1641600 seconds

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

Hope it helps.

EDIT1:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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