簡體   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